Excel Macros 简明教程

Excel Macros - UserForms

有时,您可能需要反复收集他人的信息。Excel VBA 为您提供了一种处理此任务的简单方法 - UserForm 。与您填写的任何其他表单一样,用户窗体让您可以轻松了解要提供哪些信息。用户窗体非常友好,因为它提供的控件不言自明,必要时还附有其他说明。

用户窗体的主要优点是,您可以节省您花在信息填充方式和内容上的时间。

Creating a UserForm

要创建用户窗体,请按照以下步骤进行 −

  1. 在功能区上单击“开发工具”选项卡。

  2. 单击 Visual Basic。将为工作簿打开一个 Visual Basic 窗口。

  3. Click Insert,

  4. 从下拉列表中选择 UserForm。

creating userform

用户窗体出现在窗口右侧。

userform appears

Understanding the UserForm

最大化 UserForm.xlsx – UserForm1 窗口。

你现在处于设计模式。您可以在用户窗体上插入控件,并为相应的操作编写代码。控件在工具箱中提供。用户窗体的属性在属性窗口中。UserForm1(用户窗体的标题)在项目资源管理器中的“窗体”下给出。

understanding userform
  1. 在属性窗口中将用户窗体的标题更改为 Project Report – Daily。

  2. 将用户窗体的名称更改为 ProjectReport。

projectreport

这些更改反映在用户窗体、属性和项目资源管理器中。

Controls in the ToolBox

用户窗体将具有不同的组件。当您单击任何组件时,系统会指示您提供什么信息以及如何提供信息,或者您会看到可供选择的选项。所有这些都是通过用户窗体的工具箱中的 ActiveX 控件提供的。

Excel 提供两种类型的控件——窗体控件和 ActiveX 控件。您需要了解这两种类型的控件之间的区别。

Form controls

窗体控件是与较早版本的 Excel(从 Excel 5.0 版本开始)兼容的 Excel 原始控件。窗体控件还设计为在 XLM 宏表上使用。

您可以使用窗体控件运行宏。您可以向控件分配现有宏,或编写或记录新宏。单击控件时,宏将运行。您已了解如何从窗体控件中插入命令按钮到工作表中来运行宏。然而,这些控件无法添加到用户窗体中。

ActiveX controls

ActiveX 控件可用于 VBA 用户窗体上。ActiveX 控件具有丰富的属性,您可以使用这些属性来自定义它们的外观、行为、字体和其他特性。

您在用户窗体工具箱中拥有以下 ActiveX 控件 −

  1. Pointer

  2. Label

  3. TextBox

  4. ComboBox

  5. ListBox

  6. CheckBox

  7. OptionButton

  8. Frame

  9. ToggleButton

  10. CommandButton

  11. TabStrip

  12. MultiPage

  13. ScrollBar

  14. SpinButton

  15. Image

除了这些控件外,Visual Basic 还为您提供了 MsgBox 函数,该函数可用于显示消息和/或提示用户采取操作。

在接下来的几节中,您将了解这些控件和 MsgBox。然后,您将能够选择需要哪些控件来设计您的用户窗体。

Label

您可以通过显示描述性文本(例如标题、说明和/或简要说明)来使用标签进行标识目的。

Example

label

TextBox

您可以使用文本框(一个矩形框)来键入、查看或编辑文本。您还可以将文本框用作呈现只读信息的静态文本字段。

Example

textbox

List Box

You can use a List Box to display a list of one or more items of text from which a user can choose. Use a list box for displaying large numbers of choices that vary in number or content.

  1. Insert a ListBox on the UserForm.

  2. Click on the ListBox.

  3. Type ProjectCodes for Name in the Properties window of the ListBox.

There are three types of List Boxes −

  1. Single-selection List box − A single-selection List Box enables only one choice. In this case, a list box resembles a group of option buttons, except that a list box can handle a large number of items more efficiently.

  2. Multiple selection List Box − A multiple selection List Box enables either one choice or contiguous (adjacent) choices.

  3. Extended-selection List Box − An extended-selection List Box enables one choice, contiguous choices and noncontiguous (or disjointed) choices.

You can select one of these types of List Boxes, from the Properties window.

listbox
  1. Right click on the UserForm.

  2. Select View Code from the dropdown list. The code window of UserForm opens.

  3. Click Initialize in the top right box of the code window.

  4. Type the following under Private Sub UserForm_Initialize().

ProjectCodes.List = Array ("Proj2016-1", "Proj2016-2", "Proj2016-3", "Proj20164", "Proj2016-5")
initialize
  1. 单击功能区上的“运行”选项卡。

  2. 从下拉列表中选择“运行 Sub/用户窗体”。

select run

Next, you can write code for actions on selecting an item in the list. Otherwise, you can just display the text that is selected, which is the case for filling the Project Code in the Report.

ComboBox

You can use ComboBox that combines a text box with a list box to create a dropdown list box. A combo box is more compact than a list box but requires the user to click the down arrow to display the list of items. Use a combo box to choose only one item from the list.

  1. Insert a ComboBox on the UserForm.

  2. Click the ComboBox.

  3. Type ProjectCodes2 for Name in the Properties window of the ComboBox.

combobox
  1. Right click on the UserForm.

  2. Select View Code from the dropdown list.

  3. The code window of UserForm opens.

Type the following as shown below.

ProjectCodes2.List = Array ("Proj2016-1", "Proj2016-2", "Proj2016-3", "Proj20164", "Proj2016-5")
code window
  1. 单击功能区上的“运行”选项卡。

  2. 从下拉列表中选择“运行 Sub/用户窗体”。

run tab

单击向下箭头显示项目列表。

click down arrow

单击所需的项目,例如 Project2016-5。选择框中将显示已选项。

required item

CheckBox

您可以使用复选框选中单击框中显示的一个或多个选项。这些选项会有标签,您可以清晰地看到已选选项。

复选框可以有两种状态 −

  1. 选择(已启用),表示为框内对勾

  2. 清除(已禁用),表示为空白框

在组合框中使用复选框选择选项可以节省空间。在此情况下,复选框还可以有第三个状态 −

  1. 混合,表示“开启”和“关闭”状态的组合,在框内显示一个黑点。这用于表示组合框中使用复选框进行的多选。

  2. 如下所示,在 UserForm 中插入复选框。

checkbox
  1. 单击功能区上的“运行”选项卡。

  2. 从下拉列表中选择“运行 Sub/用户窗体”。

  3. 单击框中已选选项。

boxes

OptionButton

您可以使用选项按钮(也称为单选按钮),在有限的互斥选项集中进行单选。选项按钮通常包含在组框或框架中。

选项按钮用一个小圆圈表示。选项按钮可以有以下两种状态之一 −

  1. 选择(已启用),表示为圆圈内的圆点

  2. 清除(已禁用),表示为空白

Frame

您可以使用框架控件(也称为组框),将相关控件组合到一个可视化单元中。通常,选项按钮、复选框或密切相关的控件会被分组到一个框架控件中。

框架控件由一个带有可选标签的矩形对象表示。

  1. 插入带有标题“选择”的框架。

  2. 在框架控件中插入带有标题“是”和“否”的两个选项按钮。选项“是”和“否”是互斥的。

frame
  1. 单击功能区上的“运行”选项卡。

  2. 从下拉列表中选择“运行 Sub/用户窗体”。

  3. 单击已选选项。

selected option

ToggleButton

你可以使用一个切换按钮指示一个状态,比如是或否,或一个模式,比如开或关。这个按钮在被点击时会在启用和禁用状态之间切换。

如下图所示在 UserForm 中插入一个切换按钮 −

togglebutton
  1. 单击功能区上的“运行”选项卡。

  2. 从下拉列表中选择运行子/UserForm。这个切换按钮默认处于启用状态。

default

点击切换按钮。切换按钮将被禁用。

toggle button

如果你再次点击切换按钮,它将被启用。

CommandButton

你可以使用一个命令按钮运行一个宏,当用户点击它时执行一些操作。你已经学会如何在工作表中使用一个命令按钮来运行一个宏。

命令按钮也被称为推送按钮。如下图所示在 UserForm 中插入一个命令按钮 −

commandbutton
  1. 右击命令按钮。

  2. 在 sub Commandbutton1_click 中输入以下代码。

ProjectCodes2.DropDown
commandbutton1
  1. 单击功能区上的“运行”选项卡。

  2. 从下拉列表中选择“运行 Sub/用户窗体”。

daily report

点击命令按钮。组合框的下拉列表打开,因为这是你在代码中编写的操作。

combo box

TabStrip

你可以在 UserForm 中插入一个类似于 Excel 标签页的标签页控件。

ScrollBar

你可以使用滚动条通过点击滚动箭头或拖动滚动框来浏览一系列的值。

通过在所需位置绘制并调整滚动条的长度,在 UserForm 中插入一个滚动条。

scrollbar
  1. 右键单击滚动条。

  2. 从下拉列表中选择查看代码。代码窗口打开。

  3. 在 sub ScrollBar1_Scroll() 下添加以下代码:

TextBox2.Text = "Scrolling Values"
scrolling value
  1. 单击功能区上的“运行”选项卡。

  2. 从下拉列表中选择“运行 Sub/用户窗体”。

scrollbar report

拖动滚动框。文本 - 滚动值将显示在文本框中,就像你指定为滚动条滚动操作一样。

text box

MsgBox ()

你可以在点击某物时使用 MsgBox() 函数显示一条消息。这可以是一条指导或一些信息、一个警告或一个错误警报。

例如,当开始滚动滚动框时,你可以显示一条消息来提示值正在滚动。

msgbox function

Message Box Icon Displays

你可以使用消息框图标显示来描绘具体的消息。你有各种消息框图标,以满足你的目的 −

  1. Type the following code under ScrollBar1_scroll.

MsgBox "Select Ok or Cancel", vbOKCancel, "OK  - Cancel Message"
MsgBox "It's an Error!", vbCritical, "Run time result"
MsgBox "Why this value", vbQuestion, "Run time result"
MsgBox "Value Been for a Long Time", vbInformation, "Run time result"
MsgBox "Oh Is it so", vbExclamation, "Run time result"
  1. 单击功能区上的“运行”选项卡。

  2. 从下拉列表中选择“运行 Sub/用户窗体”。

  3. Drag the scroll box.

You will get the following message boxes successively.

message boxes

Designing UserForm

Now, you have an understanding of the different controls that you can use on a UserForm. Select the controls, group them if required and arrange them on the UserForm as per some meaningful sequence. Write the required actions as code corresponding to the respective controls.

Refer to the VBA tutorial in this tutorials library for an example of UserForm.