分享

【引用】[VBA] vba控件常规使用

 你喜欢那个 2011-09-27

【引用】[VBA] vba控件常规使用  

2011-03-02 16:10:44|  分类: 默认分类 |  标签: |字号 订阅

UserForm 基础

如何显示 UserForm

以下是用于显示 UserForm 编程语法是:
UserFormName .Show
要显示名为 UserForm1, UserForm 使用以下代码:
UserForm1.Show
不显示它实际上还能加载 UserForm 装入内存。 复杂 UserForm 可能需要几秒钟以显示。 因为预先您能加载到内存, UserForm 可决定何时导致此开销。 要加载到内存 UserForm1 不显示它, 使用以下代码:
Load UserForm1
若要显示 UserForm, 必须使用以前已显示 显示 方法。

如何以暂时隐藏 UserForm

如果要暂时隐藏 UserForm, 使用 隐藏 方法。 可能想要隐藏 UserForm 如果应用程序涉及用户窗体之间移动。 要隐藏 UserForm, 使用以下代码:
UserForm1.Hide
 

如何从内存删除 UserForm

要从内存, 删除 UserForm 使用 Unload 语句。 要卸载, 名为 UserForm1, UserForm 使用以下代码:
Unload UserForm1
如果您卸载 UserForm, 是与 UserForm 或者, 是与 UserForm 上控件的事件过程中 (例如, 您单击 CommandButton 控件), 您可以使用 " 我 " 关键字代替的 UserForm 名称。 将关键字用于卸载 UserForm, " Me " 使用以下代码:
Unload Me

如何使用 UserForm 事件

支持许多预定义事件, 可以附加到 VBA 过程。 在事件发生时, 该附加到事件过程运行。 单个操作由用户执行可初始化多事件。 之间最经常对 UserForm 使用事件是 Initialize 事件、 Click 事件, 和 Terminate 事件。

注意 包含事件过程 Visual Basic 模块可能称为 " 后面 " UserForm 模块。 模块包含事件过程是不可见的 VisualBasic 编辑器 Project MicrosoftInternetExplorer 窗口 Modules 集合中。 您必须双击正文部分 UserForm 以查看 UserForm 代码模块。

如何捕获 UserForm 事件

要捕获 UserForm 事件, 请按照下列步骤操作:
1. Excel 中创建新工作簿。
2. 在 工具 菜单, 指向 宏 , 然后单击 VisualBasic 编辑器 。
3. 在 插入 菜单上, 单击要在工作簿中插入 UserForm UserForm 。
4. 双击以显示代码窗口对于 UserForm UserForm 。
5. 模块, 中键入如下代码:
Private Sub UserForm_Click()
Me.Height = Int(Rnd * 500)
Me.Width = Int(Rnd * 750)
End Sub
Private Sub UserForm_Initialize()
Me.Caption = "Events Events Events!"
Me.BackColor = RGB(10, 25, 100)
End Sub
Private Sub UserForm_Resize()
msg = "Width: " & Me.Width & Chr(10) & "Height: " & Me.Height
MsgBox prompt:=msg, Title:="Resize Event"
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
msg = "Now Unloading " & Me.Caption
MsgBox prompt:=msg, Title:="QueryClose Event"
End Sub
Private Sub UserForm_Terminate()
msg = "Now Unloading " & Me.Caption
MsgBox prompt:=msg, Title:="Terminate Event"
End Sub
6. 在 运行 菜单上, 单击 运行子过程 / 用户窗体 。
UserForm 首先加载, 时宏使用 Initialize 事件改为 " 事件事件事件 ! " 和 BackColor 属性以深蓝色的 UserForm Caption 属性。

当您单击 UserForm, 您初始化 Click 事件。 调整 UserForm Click 事件。 因为您创建 Resize 事件, 过程单击 UserForm 后收到两个消息框。 因为 Click 事件代码更改宽度属性和 Height 属性是 UserForm Resize 事件发生两次。

关闭 UserForm 初始化 QueryClose 事件。 QueryClose 事件显示消息框包含标题为 Initialize 事件, 您赋予 UserForm 代码中。 可以使用时要执行特定的操作集如果用户关闭 UserForm QueryClose 事件。

然后生成一个消息框, 指出标题为 UserForm 是 UserForm1 Terminate 事件。 从内存中删除 UserForm 并返回到其原始状态标题为 UserForm 后 Terminate 事件发生。

如何防止 UserForm 关闭通过关闭按钮

当您运行 UserForm, 关闭 按钮添加到 UserForm 窗口的右上角。 如果要防止 UserForm 关闭通过 关闭 按钮, 您必须捕获 QueryClose 事件。

QueryClose 事件 UserForm 是从内存中卸载之前发生。 使用 QueryClose 事件 CloseMode CloseMode 参数来确定如何 UserForm 关闭。 vbFormControlMenu 值为 CloseMode CloseMode 参数表示时, 单击 关闭 按钮。要保持活动, UserForm 将 Cancel 取消 对 QueryClose 事件参数为 True 。 要使用 QueryClose 事件来防止 UserForm 关闭通过 关闭 按钮, 请按照下列步骤:
1. Excel 中创建新工作簿。
2. 在 工具 菜单, 指向 宏 , 然后单击 VisualBasic 编辑器 。
3. 在 插入 菜单上, 单击要在工作簿中插入 UserForm UserForm 。
4. 将 CommandButton 控件添加到 UserForm。
5. 双击以显示代码窗口对于 UserForm UserForm 。
6. 在代码窗口, 键入如下代码:
Private Sub CommandButton1_Click()
Unload Me
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
IF CloseMode = vbFormControlMenu Then
Cancel = True
Me.Caption = "Click the CommandButton to close Me!"
End If
End Sub
7. 在 运行 菜单上, 单击 运行子过程 / 用户窗体 。
当您单击 关闭 按钮 UserForm 未关闭。 您必须单击 CommandButton 控件关闭 UserForm。

 


注意 : 代码包含在本文中不包含影响所有属性和对控件事件的示例。 如果您不得不, 请使用属性窗口要查看可供控件属性的列表。 要在 视图 菜单上, 查看列表的属性, 请单击 属性窗口

如何使用设计模式来编辑控件

当您使用 " VisualBasic 编辑器来设计一个对话框, 使用设计模式。 在设计模式, 您可编辑控件和可更改属性在属性窗口 UserForm 上的控制。 若要显示属性窗口, 在 视图 菜单上, 单击 属性窗口 。

当您处在设计模式 注意 控件不响应与事件。 当您运行一个对话框, 显示方式, 用户看到它, 程序处于运行模式。 当 UserForm 是从内存中卸载将不会保留更改, 对运行模式中控件的属性。

注意 控件请回复到事件在运行模式。

如何引用 UserForm 上控件

如何您引用控件编程取决 VisualBasic 模块表运行代码的类型。 如果代码从常规模块, 运行以下语法是:
UserFormName.Controlname.Property =
例如, 如果要设置名为 TextBox , 名为到值是 Bob , UserForm1 UserForm 上 TextBox 控件的 Text 属性使用以下代码:
UserForm1.TextBox1.Text = "Bob"
如果代码是通过事件的控件或者通过 UserForm, 启动过程中是您不需要引用名为 UserForm。 而, 使用以下代码:
TextBox1.Text = "Bob"
当向对象, 附加代码代码附加到之一为对象事件。 众多, 本文示例中, 将代码附加到 Click 事件是 CommandButton 对象。
 

标签控件

标签 控件主要用于描述 UserForm 上其他控件。 运行 UserForm 时 Label 控件不能编辑由用户。 使用 Caption 属性到设置或返回一个 Label 控件中文本。 用于格式化 Label 控件其他常用属性包括 字体 属性和 ForeColor 属性。

如何使用 WITH 语句设置 Label 控件格式

要使用 WITH 语句来更改属性的 Label 控件, 请按照下列步骤:
1. 启动 Excel, 并打开新空白工作簿。
2. 在 工具 菜单, 指向 宏 , 然后单击 VisualBasic 编辑器 。
3. 在 插入 菜单上, 单击要在工作簿中插入 UserForm UserForm 。
4. 将 Label 控件添加到 UserForm。
5. 将 CommandButton 控件添加到 UserForm。
6. 双击以打开代码窗口对于 UserForm CommandButton 控件。
7. 在代码窗口, 为 CommandButton 1 Click 事件键入下列代码:
Private Sub CommandButton1_Click()
With Label1
' Set the text of the label.
.Caption = "This is Label Example 1"
' Automatically size the label control.
.AutoSize = True
.WordWrap = False
' Set the font used by the Label control.
.Font.Name = "Times New Roman"
.Font.Size = 14
.Font.Bold = True
' Set the font color to blue.
.ForeColor = RGB(0, 0, 255)
End With
End Sub
8. 在 运行 菜单上, 单击 运行子过程 / 用户窗体 。
9. 单击 CommandButton 。
文本粗 TimesNewRoman 用字体大小是 14 中 Label 控件上显示 " Thisis 标签示例 1 "。
 

TextBox 控件

TextBox 控件经常用于收集来自用户输入。 Text 属性包含项, TextBox 控件中进行。

如何使用 TextBox 控件来验证密码

如果您设置 TextBox 控件, PasswordChar 属性的它成为 " masked - 编辑 " 控件。 由字符指定可视取代 TextBox 控件中键入的每个字符。 要使用 TextBox 控件来验证密码, 请按照下列步骤:
1. 启动 Excel, 并打开新空白工作簿。
2. 在 工具 菜单, 指向 宏 , 然后单击 VisualBasic 编辑器 。
3. 在 插入 菜单上, 单击要在工作簿中插入 UserForm UserForm 。
4. 将 TextBox 控件添加到 UserForm。
5. 在 视图 菜单上, 单击 属性 以显示属性窗口。
6. 对 TextBox 控件, PasswordChar 属性中键入 *

注意 您正将值改为星号。
7. 将 CommandButton 控件添加到 UserForm。
8. 双击以打开代码窗口对于 UserForm CommandButton 控件。
9. 在代码窗口, 为 CommandButton 1 Click 事件键入下列代码:
Private Sub CommandButton1_Click()
If TextBox1.Text <> "userform" Then
MsgBox "Password is Incorrect. Please reenter."
TextBox1.Text = ""
TextBox1.SetFocus
Else
MsgBox "Welcome!"
Unload Me
End If
End Sub
10. 在 运行 菜单上, 单击 运行子过程 / 用户窗体 。
11. 在 TextBox 控件中键入密码 userform 。
12. 单击 CommandButton 控件。
对于本例, 密码是 " userform "。 如果您键入正确密码, 您收到一个消息框, 指出密码不正确, 然后重新键入密码可清除 TextBox 控件, 并且。 当您键入正确密码, 收到欢迎消息, 并 UserForm 关闭。

 
 

CommandButton 控件

您可以使用 CommandButton 控制来启动 VBA 过程。 VBA 过程通常附加到 CommandButton 控件的 Click 事件。 要使用 CommandButton 控件 Click 事件发生, 时, 运行过程请按照步骤:
1. 启动 Excel, 并打开新空白工作簿。
2. 在 工具 菜单, 指向 宏 , 然后单击 VisualBasic 编辑器 。
3. 在 插入 菜单上, 单击要在工作簿中插入 UserForm UserForm 。
4. 将 CommandButton 控件添加到 UserForm。
5. 双击以显示代码窗口对于 UserForm CommandButton 控件。
6. 在代码窗口, 键入如下代码:
Private Sub CommandButton1_Click()
red = Int(Rnd * 255)
green = Int(Rnd * 255)
blue = Int(Rnd * 255)
CommandButton1.BackColor = RGB(red, green, blue)
End Sub
7. 在 运行 菜单上, 单击 运行子过程 / 用户窗体 。
CommandButton 1 控件的背景颜色更改每次您单击它。

 

ListBox 控件

ListBox 控件的目的是为了向用户显示要选择的项目列表。 您可以存储为 Excel 工作表上 ListBox 控件项目列表。 使用 RowSource 属性来填充工作表, 上 ListBox 控件与范围的单元格。 ListBox 控件在使用 MultiSelect 属性, 时可设置为接受多重选择。

如何从 ListBox 控件获取当前选定项

使用 Value 属性的 ListBox 控件可返回当前选定项。 要返回单项选择 ListBox 控件, 中当前选定项请按照下列步骤操作:
1. 启动 Excel, 并打开新空白工作簿。
2. 在单元格 A 1: A 5 Sheet, 键入了您要用于填充 ListBox 控件值。
3. 在 工具 菜单, 指向 宏 , 然后单击 VisualBasic 编辑器 。
4. 在 插入 菜单上, 单击要在工作簿中插入 UserForm UserForm 。
5. 将 ListBox 控件添加到 UserForm。
6. 双击 ListBox 控件以显示代码窗口对 ListBox 控件。
7. 在代码窗口, 为 ListBox 1 Click 事件键入下列代码:
Private Sub ListBox1_Click()
MsgBox ListBox1.Value
End Sub
8. 在 运行 菜单上, 单击 运行子过程 / 用户窗体 。
当单击列表, 中的项目与当前选定项目将出现一个消息框。

如何获取多选择 ListBox 控件中选定项

确定多选择 ListBox 控件, 中所选项目必须循环列表, 中所有项目并再查询 Selected 属性。 要返回多选择, ListBox 控件中当前选定项请按照下列步骤操作:
1. 启动 Excel, 并打开新空白工作簿。
2. 在单元格 A 1: A 5 Sheet, 键入了您要用于填充 ListBox 控件值。
3. 在 工具 菜单, 指向 宏 , 然后单击 VisualBasic 编辑器 。
4. 在 插入 菜单上, 单击要在工作簿中插入 UserForm UserForm 。
5. 将 ListBox 控件添加到 UserForm。
6. 在 视图 菜单上, 单击 属性 以查看属性窗口。
7. 键入值, 对于下列 ListBox 控件属性表示:
   Property    Value
----------- -----------------------
MultiSelect 1 - frmMultiSelectMulti
RowSource Sheet1!A1:A8
8. 将 CommandButton 控件添加到 UserForm。
9. 双击以显示代码窗口对于 UserForm CommandButton 控件。
10. 在代码窗口, 为 CommandButton 1 Click 事件键入下列代码:
Sub CommandButton1_Click ()
' Loop through the items in the ListBox.
For x = 0 to ListBox1.ListCount - 1
' If the item is selected...
If ListBox1.Selected(x) = True Then
' display the Selected item.
MsgBox ListBox1.List(x)
End If
Next x
End Sub
11. 在 运行 菜单上, 单击 运行子过程 / 用户窗体 。
12. 列表中选择一个或多个项目。
13. 单击 CommandButton 1 。
单击 CommandButton 1 , 后, 在 ListBox 控件中选择每个项目显示在一个单独的消息框。 UserForm 在消息框中, 出现所有选定项后自动关闭。

如何使用 RowSource 属性来填充工作表上以 ListBox 控件

要使用 RowSource 属性来填充工作表, 上 ListBox 控件从范围的单元格请按照下列步骤:
1. 启动 Excel, 并打开新空白工作簿。
2. 在单元格 A 1: A 5 Sheet, 键入了您要用于填充 ListBox 控件值。
3. 在 工具 菜单, 指向 宏 , 然后单击 VisualBasic 编辑器 。
4. 在 插入 菜单上, 单击要在工作簿中插入 UserForm UserForm 。
5. 将 ListBox 控件添加到 UserForm。
6. 将 CommandButton 控件添加到 UserForm。
7. 双击以显示代码窗口对于 UserForm CommandButton 控件。
8. 在代码窗口, 为 CommandButton 1 Click 事件键入下列代码:
Private Sub CommandButton1_Click()
ListBox1.RowSource = "=Sheet1!A1:A5"
End Sub
9. 在 运行 菜单上, 单击 运行子过程 / 用户窗体 。

注意 ListBox 1 不包含任何值。
10. 单击 CommandButton 1 。
ListBox 1 填充单元格 A 1: A 5 Sheet 中有值。

如何填充一个 ListBox 控件数组中有值

下例显示您如何填充以数组 ListBox 控件。 数组中每次为 ListBox 控件项必须分配值。 通常, 此过程要求您使用循环结构, 如 ForàNext 循环。 要填充以数组, ListBox 控件请按照下列步骤操作:
1. 启动 Excel, 并打开新空白工作簿。
2. 在 工具 菜单, 指向 宏 , 然后单击 VisualBasic 编辑器 。
3. 在 插入 菜单上, 单击要在工作簿中插入 UserForm UserForm 。
4. 将 ListBox 控件添加到 UserForm。
5. 在 插入 菜单上, 单击要插入模块表 模块 。
6. 在代码窗口, 键入如下代码:
Sub PopulateListBox()
Dim MyArray As Variant
Dim Ctr As Integer
MyArray = Array("Apples", "Oranges", "Peaches", "Bananas", "Pineapples")
For Ctr = LBound(MyArray) To UBound(MyArray)
UserForm1.ListBox1.AddItem MyArray(Ctr)
Next
UserForm1.Show
End Sub
7. 然后单击 运行 在 工具 菜单上,、 " PopulateListBox , 和 宏 。
PopulateListBox 过程建立简单数组, 并数组中通过使用 AddItem 方法添加到 ListBox 控件项目。 然后, UserForm 出现。

如何使用工作表上水平的单元格区域来填充一个 ListBox 控件

如果将 ListBox 控件的 RowSource 属性到水平区域的单元格, ListBox 控件中第一个值只会出现。

要通过使用 AddItem 方法, ListBox 控件从水平区域的单元格填充请按照下列步骤操作:
1. 启动 Excel, 并打开新空白工作簿。
2. 在单元格 A1:E1 Sheet, 键入了您要用于填充 ListBox 控件值。
3. 在 工具 菜单, 指向 宏 , 然后单击 VisualBasic 编辑器 。
4. 在 插入 菜单上, 单击要在工作簿中插入 UserForm UserForm 。
5. 将 ListBox 控件添加到 UserForm。
6. 在 插入 菜单上, 单击要插入模块表 模块 。
7. 在代码窗口, 键入如下代码:
Sub PopulateListWithHorizontalRange()
For Each x In Sheet1.Range("A1:E1")
UserForm1.ListBox1.AddItem x.Value
Next
UserForm1.Show
End Sub
8. 然后单击 运行 在 工具 菜单上,、 " PopulateListWithHorizontalRange , 和 宏 。
在单元格 A 1: E 5 Sheet, 将值添加到 ListBox 1 一次循环宏过程。

A 1: E 5 单元 注意 ListBox 1 与不定 Sheet 1 上。

如何从 ListBox 控件绑定到多列的数据返回多个值

您可以格式 ListBox 控件以显示多个列的数据。 这意味着 ListBox 控件, 每个列表行上显示多个项目。 要多值列表, 中选定项收益请按照下列步骤操作:
1. 启动 Excel, 并打开新空白工作簿。
2. Sheet 由该单元格中键入以下数据:

A 1: 年 B 1: 区域 C1: 销售
A 2: 1996 B: 北美 C2: 140
3: 1996 B 3: 南非 C 3: 210
A 4: 1997 B4: 北美 C 4: 190
A5: 1997 B 5: 南非 C 5: 195
3. 在 工具 菜单, 指向 宏 , 然后单击 VisualBasic 编辑器 。
4. 在 插入 菜单上, 单击要在工作簿中插入 UserForm UserForm 。
5. 将 Label 控件添加到 UserForm。
6. 将 ListBox 控件添加到 UserForm。
7. 右击 ListBox , 然后单击 属性 。
8. 键入或选择值, 都表示为下列属性对 ListBox 控件与下表中列出:
   Property       Value
----------------------------
BoundColumn 1
ColumnCount 3
ColumnHeads True
RowSource Sheet1!A2:A5
9. 双击 ListBox 控件以显示代码窗口对 ListBox 控件。
10. 在代码窗口, 键入如下代码:
Private Sub ListBox1_Change()
Dim SourceData As Range
Dim Val1 As String, Val2 As String, Val3 As String
Set SourceRange = Range(ListBox1.RowSource)
Val1 = ListBox1.Value
Val2 = SourceRange.Offset(ListBox1.ListIndex, 1).Resize(1, 1).Value
Val3 = SourceRange.Offset(ListBox1.ListIndex, 2).Resize(1, 1).Value
Label1.Caption = Val1 & " " & Val2 & " " & Val3
End Sub
11. 在 运行 菜单上, 单击 运行子过程 / 用户窗体 。
当您单击 ListBox 控件, 中一个条目标签将更改为显示该条目中所有三个项目。

如何从绑定到工作表 ListBox 控件中删除所有项目

要从 ListBox 控件绑定到工作表, 删除所有项目清除, 是存储在 RowSource 属性值。 要从 ListBox 控件绑定到工作表, 删除项目请按照下列步骤:
1. 启动 Excel, 并打开新空白工作簿。
2. 在单元格 A 1: A 5 Sheet, 键入了您要用于填充 ListBox 控件值。
3. 在 工具 菜单, 指向 宏 , 然后单击 VisualBasic 编辑器 。
4. 在 插入 菜单上, 单击要在工作簿中插入 UserForm UserForm 。
5. 将 ListBox 控件添加到 UserForm。
6. 用鼠标右键单击 ListBox 控件, 然后单击 属性 。
7. 在 RowSource 属性, 键入 Sheet 1 A 1: A 5 !
8. 将 CommandButton 控件添加到 UserForm。
9. 双击以显示代码窗口为 CommandButton 控件 CommandButton 控件。
10. 在代码窗口, 为 CommandButton 1 Click 事件键入下列代码:
Private Sub CommandButton1_Click()
ListBox1.RowSource = ""
End Sub
11. 在 运行 菜单上, 单击 运行子过程 / 用户窗体 。

ListBox 控件, 添加到 UserForm 具有值, 您输入 Sheet 填充。
12. 单击 CommandButton 1 。
从 ListBox 1 中删除所有项目。

如何从不绑定到工作表 ListBox 控件中删除所有项目

没有没有单个 VBA 命令如果没有绑定到工作表列表, 从 ListBox 控件删除所有项目。 要从 ListBox 控件从 VisualBasic 数组, 填充删除所有项目请按照下列步骤:
1. 启动 Excel, 并打开新空白工作簿。
2. 在 工具 菜单, 指向 宏 , 然后单击 VisualBasic 编辑器 。
3. 在 插入 菜单上, 单击要在工作簿中插入 UserForm UserForm 。
4. 将 ListBox 控件添加到 UserForm。
5. 在 插入 菜单上, 单击要插入模块表 模块 。
6. 在代码窗口, 键入如下代码:
Sub PopulateListBox()
Dim MyArray As Variant
Dim Ctr As Integer
MyArray = Array("Apples", "Oranges", "Peaches", "Bananas", "Pineapples")
For Ctr = LBound(MyArray) To UBound(MyArray)
UserForm1.ListBox1.AddItem MyArray(Ctr)
Next
UserForm1.Show
End Sub
7. 将 CommandButton 控件添加到 UserForm。
8. 双击以显示代码窗口为 CommandButton 控件 CommandButton 控件。
9. 在代码窗口, 为 CommandButton 1 Click 事件键入下列代码:
Private Sub CommandButton1_Click()
For i = 1 To ListBox1.ListCount
ListBox1.RemoveItem 0
Next I
End Sub
10. 然后单击 运行 在 工具 菜单上,、 " PopulateListBox , 和 宏 。

ListBox 控件填充, 并再出现 UserForm。
11. 单击 CommandButton 1 。
从 ListBox 1 中删除所有项目。

 

ComboBox 控件

您可以使用 ComboBox 控件作为在下拉列表框中, 或组合框其中您可选择列表中值或键入新值。 Style 属性决定如果 ComboBox 控件作为下拉列表框或组合框。

注意 前述对 ListBox 控件中所有示例也能应用到 ComboBox 控件, 除例如获取多选择 ListBox 控件中选定项 " 如何 "。

如何向列表添加新项目如果 ComboBox 控件未绑定到工作表

键入值是未在列表中 ComboBox 控件, 时可能要向列表添加新值。 要添加新值, 如果 ComboBox 控件未绑定到工作表, ComboBox 控件中键入请按照下列步骤操作:
1. 启动 Excel, 并打开新空白工作簿。
2. 在 工具 菜单, 指向 宏 , 然后单击 VisualBasic 编辑器 。
3. 在 插入 菜单上, 单击要在工作簿中插入 UserForm UserForm 。
4. 将 ComboBox 控件添加到 UserForm。
5. 在 插入 菜单上, 单击要插入模块表 模块 。
6. 在代码窗口, 键入如下代码:
Sub PopulateComboBox()
Dim MyArray As Variant
Dim Ctr As Integer
MyArray = Array("Apples", "Oranges", "Peaches", "Bananas", "Pineapples")
For Ctr = LBound(MyArray) To Ubound(MyArray)
UserForm1.ComboBox1.AddItem MyArray(Ctr)
Next
UserForm1.Show
End Sub
7. 将 CommandButton 控件添加到 UserForm。
8. 双击以显示代码窗口为 CommandButton 控件 CommandButton 控件。
9. 在代码窗口, 为 CommandButton 1 Click 事件键入下列代码:
Private Sub CommandButton1_Click()
Dim listvar As Variant
listvar = ComboBox1.List
On Error Resume Next
' If the item is not found in the list...
If IsError(WorksheetFunction.Match(ComboBox1.Value, listvar, 0)) Then
' add the new value to the list.
ComboBox1.AddItem ComboBox1.Value
End If
End Sub
10. 然后单击 运行 在 工具 菜单上,、 " PopulateListBox , 和 宏 。

填充 组合框 控件, 并再出现 UserForm。
11. ComboBox 控件, 中键入 Mangoes (或任何值尚未是列表中)。
12. 单击 CommandButton 1 。
现在您键入新值将在列表末尾。

如何向列表添加新项目如果 ComboBox 控件绑定到工作表

当用户键入值是未在列表中 ComboBox 控件, 可能需要新值添加到列表。 要添加该列表, ComboBox 控件中键入新值请按照下列步骤操作:
1. 启动 Excel, 并打开新空白工作簿。
2. 单元格 A 1: A 5 Sheet, 中键入值与要用于填充 组合框 控件。
3. 选择单元格 A 1: A 5 Sheet 1 上。
4. 插入 菜单上指向 名称 , 然后单击 定义 。

在 工作簿中名称 框中, 键入 ListRange , 然后单击 确定 。 这创建 ListRange 定义名称。 使用定义名称 ListRange 将 ComboBox 控件的 RowSource 属性绑定到工作表。
5. 在 工具 菜单, 指向 宏 , 然后单击 VisualBasic 编辑器 。
6. 在 插入 菜单上, 单击要在工作簿中插入 UserForm UserForm 。
7. 将 ComboBox 控件添加到 UserForm。
8. 对于 ComboBox 1 , 属性 中键入 与 RowSource 属性 ListRange Sheet 1 !
9. 将 CommandButton 控件添加到 UserForm。
10. 双击以显示代码窗口为 CommandButton 控件 CommandButton 控件。
11. 在代码窗口, 为 CommandButton 1 Click 事件键入下列代码:
Private Sub CommandButton1_Click()
Dim SourceData As Range
Dim found As Object
Set SourceData = Range("ListRange")
Set found = Nothing
' Try to find the value on the worksheet.
Set found = SourceData.Find(ComboBox1.Value)
' If the item is not found in the list...
If found Is Nothing Then
' redefine ListRange.
SourceData.Resize(SourceData.Rows.Count + 1, 1).Name = "ListRange"
' Add the new item to the end of the list on the worksheet.
SourceData.Offset(SourceData.Rows.Count, 0).Resize(1, 1).Value _
= ComboBox1.Value
' Reset the list displayed in the ComboBox.
ComboBox1.RowSource = Range("listrange").Address(external:=True)
End If
End Sub
12. 在 运行 菜单上, 单击 运行子过程 / 用户窗体 。

UserForm 出现在 Sheet 1。
13. ComboBox 控件, 中键入值尚未列表中。
14. 单击 CommandButton 1 。
ComboBox 控件中键入新项目添加到列表, 并范围扩展到包括单元 A1:A6, ComboBox 控件绑定到列表。

当出现 UserForm 如何显示 ComboBox 控件列表

有时, 可能非常有用以 UserForm 首次出现时显示 ComboBox 控件的列表。 以下示例使用是 UserForm Activate 事件。 要显示的 ComboBox 控件, 列表请按照下列步骤操作:
1. 启动 Excel, 并打开新空白工作簿。
2. 单元格 A 1: A 5 Sheet, 中键入值与要用于填充 组合框 控件。
3. 在 工具 菜单, 指向 宏 , 然后单击 VisualBasic 编辑器 。
4. 在 插入 菜单上, 单击要在工作簿中插入 UserForm UserForm 。
5. 将 ComboBox 控件添加到 UserForm。
6. 对于 ComboBox 1 , 属性 中键入 Sheet 1 与 RowSource 属性 A 1: A 5 !
7. 双击以显示代码窗口对于 UserForm UserForm 。
8. 在代码窗口, 为 CommandButtonClick 事件键入下列代码:
Private Sub UserForm_Activate()
ComboBox1.DropDown
End Sub
9. 在 运行 菜单上, 单击 运行子过程 / 用户窗体 。
UserForm, Sheet 1 上出现, 您可看到该列表对于 ComboBox 1 。

当其他 ComboBox 控件中进行选择如何显示一个 ComboBox 控件列表

要, ComboBox 控件中进行选择时自动显示一个 ComboBox 控件的列表请按照下列步骤操作:
1. 启动 Excel, 并打开新空白工作簿。
2. 在单元格 A 1: A 10 Sheet, 输入值与要用于填充 组合框 控件。
3. 在 工具 菜单, 指向 宏 , 然后单击 VisualBasic 编辑器 。
4. 在 插入 菜单上, 单击 模块 。
5. 在代码窗口为模块, 键入如下代码:
Sub DropDown_ComboBox()
UserForm1.ComboBox2.DropDown
End Sub
6. 在 插入 菜单上, 单击要在工作簿中插入 UserForm UserForm 。
7. 将 ComboBox 控件添加到 UserForm。
8. 对于 ComboBox 1 , 属性 中键入 Sheet 1 与 RowSource 属性 A 1: A 5 !
9. 双击要打开代码窗口对 ComboBox 控件 ComboBox 控件。
10. 在代码窗口对 ComboBox 控件, 为 ComboBox Click 事件键入下列代码:
Private Sub ComboBox1_Click()
Application.OnTime Now, "DropDown_ComboBox"
End Sub
11. 添加到 UserForm 二 ComboBox 控件。
12. 对于 ComboBox2 , 属性 中键入 Sheet 1 与 RowSource 属性 A6:A10 !
13. 在 运行 菜单上, 单击 运行子过程 / 用户窗体 。
对于 ComboBox2 列表单击, ComboBox 1 列表中的项目时将自动出现。

 

框架控件

使用 Frame 控件来分组 UserForm 中逻辑相关项。 框架 控件经常用于分组 OptionButton 控件。

如何循环 Frame 控件上的所有控件

要使用 EachàNext For 循环来访问 框架 控件, 中所有控件请按照下列步骤:
1. 启动 Excel, 并打开新空白工作簿。
2. 在 工具 菜单, 指向 宏 , 然后单击 VisualBasic 编辑器 。
3. 在 插入 菜单上, 单击要在工作簿中插入 UserForm UserForm 。
4. 将 Frame 控件添加到 UserForm。
5. OptionButton 控件添加到 Frame 控件。

重复此步骤向 Frame 控件中添加两个详细 OptionButton 控件。
6. 双击要打开代码窗口对 Frame 控件 Frame 控件。
7. 在代码窗口, 为 框架 Click 事件键入下列代码:
Private Sub Frame1_Click()
Dim Ctrl As Control
For Each Ctrl In Frame1.Controls
Ctrl.Enabled = Not Ctrl.Enabled
Next
End Sub
8. 在 运行 菜单上, 单击 运行子过程 / 用户窗体 。
9. 在 UserForm, 单击 Frame 控件。
首次您单击 框架 控件, Frame 控件中所有控件是不可用。 如果再次, 单击 Frame 控件控件可再次。
 

OptionButton 控件

可使用 OptionButton 控件组进行一个选择的选项组中。 使用以下技术到组 OptionButton 控件之一:
框架 控件
GroupName 属性
注意 On 值, 值, 和 True 值表明已选中一个 OptionButton Off 值、 值, 和 False 值表明未选中 OptionButton 攻击。

如何确定当 OptionButton 控件位于 Frame 控件被选中 OptionButton 控件

通过使用 Frame 控件, OptionButtons 控件分组时您可以确定通过循环 Frame 控件中所有控件并检查 Value 属性的每个控件是选定 OptionButton 控件。 要确定所选, OptionButton 控件请按照下列步骤:
1. 启动 Excel, 并打开新空白工作簿。
2. 在 工具 菜单, 指向 宏 , 然后单击 VisualBasic 编辑器 。
3. 在 插入 菜单上, 单击要在工作簿中插入 UserForm UserForm 。
4. 将 Frame 控件添加到 UserForm。
5. OptionButton 控件添加到 Frame 控件。

重复此步骤向 Frame 控件中添加两个详细 OptionButton 控件。
6. 添加一个 CommandButton 控件 UserForm 之外 Frame 控件上。
7. 双击以显示代码窗口对于 UserForm CommandButton 控件。
8. 在代码窗口, 为 CommandButton 1 Click 事件键入下列代码:
Private Sub CommandButton1_Click()
For Each x In Frame1.Controls
If x.Value = True Then
MsgBox x.Caption
End If
Next
End Sub
9. 在 运行 菜单上, 单击 运行子过程 / 用户窗体 。
10. UserForm , 中一个 OptionButton 控件, 依次 CommandButton 1 。
将出现一个消息框包含当前选定 OptionButton 控件的题注。

如何确定所选 OptionButton 控件

对以下示例目的是为了确定 Group1 中选定 OptionButton 控件。 若要创建具有两个 OptionButton 控件组, UserForm 请按照下列步骤:
1. 启动 Excel, 并打开新空白工作簿。
2. 在 工具 菜单, 指向 宏 , 然后单击 VisualBasic 编辑器 。
3. 在 插入 菜单上, 单击要在工作簿中插入 UserForm UserForm 。
4. 将 Frame 控件添加到 UserForm。
5. Frame 控件中添加一个 OptionButton 控件。

重复此步骤向 Frame 控件中添加两个详细 OptionButton 控件。
6. 对于每个 OptionButton 控件, 在 GroupName 属性键入 Group1 。
7. 重复步骤 4 和 5 以创建包含三个 OptionButton 控件二 Frame 控件。
8. 对于每个 OptionButton 控件在第二个 框架 控件, 在 GroupName 属性键入 Group2 。
9. 添加一个 CommandButton 控件 UserForm 之外 Frame 控件上。
10. 双击以显示代码窗口对于 UserForm CommandButton 控件。
11. 在代码窗口, 为 CommandButton 1 Click 事件键入下列代码:
Private Sub CommandButton1_Click()
Dim x As Control
' Loop through ALL the controls on the UserForm.
For Each x In Me.Controls
' Check to see if "Option" is in the Name of each control.
If InStr(x.Name, "Option") Then
' Check Group name.
If x.GroupName = "Group1" Then
' Check the status of the OptionButton.
If x.Value = True Then
MsgBox x.Caption
Exit For
End If
End If
End If
Next
End Sub
12. 在 运行 菜单上, 单击 运行子过程 / 用户窗体 。
13. UserForm, 中 Group1, 中一个 OptionButton 控件依次 CommandButton 1 。
将出现一个消息框包含 OptionButton 控件当前选定的标题。

 

CheckBox 控件

您可以使用 CheckBox 控制来指示真或假值。 带有复选标记中出现 CheckBox 控件指示值为 True 。 与没有复选标记出现 CheckBox 表示值为 False 。 如果 TripleState 属性的值为 True , CheckBox 控件还可以 Null 值。 可用似乎具有 Null 值 CheckBox 控件。

注意 On 值, 值, 和 True 值表明, CheckBox 控件被选定。 Off 值、 值, 和 False 值表明 CheckBox 控件已清除。

如何检查 CheckBox 控件的值

要使用 Value 属性以返回当前值的 CheckBox 控件, 请按照下列步骤:
1. 启动 Excel, 并打开新空白工作簿。
2. 在 工具 菜单, 指向 宏 , 然后单击 VisualBasic 编辑器 。
3. 在 插入 菜单上, 单击要在工作簿中插入 UserForm UserForm 。
4. 将 CheckBox 控件添加到 UserForm。
5. 在属性列表对于 CheckBox 1 , 选择 True 与 TripleState 属性。
6. 双击 CheckBox 控件以显示代码窗口为 CheckBox 控件。
7. 在代码窗口, 为 CheckBox 1 Change 事件键入下列代码:
Private Sub CheckBox1_Change()
Select Case CheckBox1.Value
Case True
CheckBox1.Caption = "True"
Case False
CheckBox1.Caption = "False"
Case Else
CheckBox1.Caption = "Null"
End Select
End Sub
8. 在 运行 菜单上, 单击 运行子过程 / 用户窗体 。
当您单击 CheckBox 控件, 标题的 CheckBox 控件更改以反映当前值。
 

切换按钮控件

切换按钮 控件具有相同外观作为一个 CommandButton 控件在您单击它。 当单击 切换按钮 控件, 它似乎按或推。 当未选中按钮 Value 属性的 切换按钮 控件是 True 选择按钮时, False 。 如果 TripleState 属性的值为 True , ToggleButton 控件还可以 Null 值。 可用似乎具有 Null 值 切换按钮 控件。

注意 On 值, 值, 和 True 值表明选定 ToggleButton 控件。 Off 值、 值, 和 False 值表明未选中 ToggleButton 控件。

如何获取 ToggleButton 控件的值

要获取值 ToggleButton 控件, 请按照下列步骤操作:
1. 启动 Excel, 并打开新空白工作簿。
2. 在 工具 菜单, 指向 宏 , 然后单击 VisualBasic 编辑器 。
3. 在 插入 菜单上, 单击要在工作簿中插入 UserForm UserForm 。
4. 添加 UserForm 上一个 切换按钮 控件。
5. 将 Label 控件添加到 UserForm。
6. 双击要打开代码窗口为 ToggleButton 控件 切换按钮 控件。
7. 在代码窗口, 为 ToggleButton1Click 事件键入下列代码:
Private Sub ToggleButton1_Click()
If ToggleButton1.Value = True Then
' Set UserForm background to Red.
Me.BackColor = RGB(255, 0, 0)
Else
' Set UserForm background to Blue.
Me.BackColor = RGB(0, 0, 255)
End If
End Sub
8. 在 运行 菜单上, 单击 运行子过程 / 用户窗体 。
当您单击 切换按钮 控件, 的 UserForm 更改背景色。

如何创建互斥 ToggleButton 控件组

本示例将变量使用 MouseUp 事件并调用 ExclusiveToggleButtons 过程。 ExclusiveToggleButtons 过程决定 ToggleButton 控件, 选中, 然后取消其他。 若要创建互斥 ToggleButton 控件组, 请按照下列步骤:
1. 启动 Excel, 并打开新空白工作簿。
2. 在 工具 菜单, 指向 宏 , 然后单击 VisualBasic 编辑器 。
3. 在 插入 菜单上, 单击 模块 。
4. 在代码窗口为模块, 键入如下代码:
' Variable that holds the name of the ToggleButton that was clicked.
Public clicked As String
Sub ExclusiveToggleButtons()
Dim toggle As Control
' Loop through all the ToggleButtons on Frame1.
For Each toggle In UserForm1.Frame1.Controls
' If Name of ToggleButton matches name of ToggleButton
' that was clicked...
If toggle.Name = clicked Then
'...select the button.
toggle.Value = True
Else
'...otherwise clear the selection of the button.
toggle.Value = False
End If
Next
End Sub
5. 在 插入 菜单上, 单击要在工作簿中插入 UserForm UserForm 。
6. 将 Frame 控件添加到 UserForm。
7. Frame 控件中添加一个 切换按钮 控件。

重复此步骤向 Frame 控件中添加两个详细 切换按钮 控件。
8. 双击以显示代码窗口对于 UserForm Frame 控件。
9. 在代码窗口为模块, 为 ToggleButton MouseUp 事件键入下列代码:
Private Sub ToggleButton1_MouseUp(ByVal Button As Integer, _
ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
clicked = ToggleButton1.Name
Application.OnTime Now, "ExclusiveToggleButtons"
End Sub
Private Sub ToggleButton2_MouseUp(ByVal Button As Integer, _
ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
clicked = ToggleButton2.Name
Application.OnTime Now, "ExclusiveToggleButtons"
End Sub
Private Sub ToggleButton3_MouseUp(ByVal Button As Integer, _
ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
clicked = ToggleButton3.Name
Application.OnTime Now, "ExclusiveToggleButtons"
End Sub
10. 在 运行 菜单上, 单击 运行子过程 / 用户窗体 。
单击 切换按钮 控件, 时取消以前选定 切换按钮 控件。
 

TabStrip 控件

使用 TabStrip 控件来查看不同组的一组控件的信息。

如何通过编程控制 TabStrip 控件

若要更改 BackColor 属性的 图像 控件根据所选, 选项卡请按照下列步骤:
1. 启动 Excel, 并打开新空白工作簿。
2. 在 工具 菜单, 指向 宏 , 然后单击 VisualBasic 编辑器 。
3. 在 插入 菜单上, 单击要在工作簿中插入 UserForm UserForm 。
4. 添加到 UserForm TabStrip 控件。
5. 添加 图像 控件包含对数的 TabStrip 控件, 但没有涉及标签,。
6. 在属性窗格用于 Image1、 类型和 H000000FF & BackColor 属性中。
7. 双击要打开代码窗口对 TabStrip 控件 TabStrip 控件。
8. 在代码窗口, 为 TabStrip1 Change 事件键入下列代码:
Private Sub TabStrip1_Change()
Dim i As Integer
i = TabStrip1.SelectedItem.Index
Select Case i
Case 0
' If Tab1 is selected, change the color of Image control to Red.
Image1.BackColor = RGB(255, 0, 0)
Case 1
' If Tab2 is selected, change the color of Image control to Green.
Image1.BackColor = RGB(0, 255, 0)
End Select
End Sub
9. 在 运行 菜单上, 单击 运行子过程 / 用户窗体 。
根据是活动 TabStrip 控件中页上 图像 控件修订的颜色。

有关 TabStrip 控件, 请单击下列文章编号以查看 Microsoft 知识库中相应:
 

multiPage 控件

使用 MultiPage 控件来处理大量可被分为几类排序信息。 MultiPage 控件由组成一个或多个 Page 对象, 每个包含不同组的控件。 以编程方式通过设置 MultiPage 控件的 Value 属性设置活动页。

如何控制 MultiPage 控件编程

要添加 MultiPage 控件并控制它通过使用宏, 请按照下列步骤操作:
1. 启动 Excel, 并打开新空白工作簿。
2. 在 工具 菜单, 指向 宏 , 然后单击 VisualBasic 编辑器 。
3. 在 插入 菜单上, 单击要在工作簿中插入 UserForm UserForm 。
4. 添加到 UserForm MultiPage 控件。
5. 将 Label 控件添加到 Page 1 多页 控件上。
6. 将 TextBox 控件添加到 Page 1 多页 控件上。
7. MultiPage 控件, 上单击, Page 2 , 然后重复步骤 5 和 6 以添加一个 Label 控件和 TextBox 控件。
8. 双击要打开代码窗口对 MultiPage 控件 MultiPage 控件。
9. 在代码窗口, 为 MultiPage1 Change 事件键入下列代码:
Private Sub MultiPage1_Change()
Select Case MultiPage1.Value
' If activating Page1...
Case 0
Label1.Caption = TextBox2.Text
TextBox1.Text = ""
' If activating Page2...
Case 1
Label2.Caption = TextBox1.Text
TextBox2.Text = ""
End Select
End Sub
10. 在代码窗口, 为 UserForm Initialize 事件键入下列代码:
Private Sub UserForm_Initialize()
' Force Page1 to be active when UserForm is displayed.
MultiPage1.Value = 0
Label1.Caption = ""
End Sub
11. 在 运行 菜单上, 单击 运行子过程 / 用户窗体 。

Page 1, 上 TextBox 控件中键入 Test 。 当单击 Page 2 选项卡、 TextBox 2 被清除, 和标题为 Label 2 变为项, 在 Page 1 上 TextBox 所做 (" Test ") (" Test ")

如何通过使用 MultiPage 控件创建一个向导界面

将任务需要几个增量步骤, 向导界面可能会非常有效。 您可使用 MultiPage 控件以创建一个向导界面代替使用多用户窗体。 本示例操作具有三页 MultiPage 控件。 附加到对 UserForm Initialize 事件过程禁用 Page 2 和 Page3, 并强制 Page 1 的 多页 控件可活动。

注意 您通过使用 Pages 集合, 索引 MultiPage 控件的页面时集合中第一页是零页。 此过程还设置标题的 CommandButton 控件并禁用 < 备份 按钮。

注意的功能是分配给 CommandButton 1 控件的 Click 事件过程 " 后退 " 按钮 < 是分配给 CommandButton 2 是 Click 事件过程控制功能的下一步 > 按钮。 若要通过使用 MultiPage 控件, 创建一个向导界面请按照下列步骤:
1. 启动 Excel, 并打开新空白工作簿。
2. 在 工具 菜单, 指向 宏 , 然后单击 VisualBasic 编辑器 。
3. 在 插入 菜单上, 单击要在工作簿中插入 UserForm UserForm 。
4. 添加到 UserForm MultiPage 控件。
5. 右键单击, " Page 1 选项卡, 然后单击要添加到 多页 控件 Page3 新页 。
6. 添加一个 CommandButton 控件不位于 多页 控件上 UserForm 上。

重复此步骤可添加一个 UserForm 上二 CommandButton 控件。
7. 双击以打开代码窗口对于 UserForm UserForm 。
8. 在代码窗口, 为 UserForm Initialize 事件键入下列代码:
Private Sub UserForm_Initialize()
With MultiPage1
' The next 2 lines disable Page2 & Page3.
.Pages(1).Enabled = False
.Pages(2).Enabled = False
' Make Page1 the active page.
.Value = 0
End With
' Set the caption on the CommandButtons.
CommandButton1.Caption = "<Back"
CommandButton1.Enabled = False
CommandButton2.Caption = "Next>"
End Sub
' Procedure for the "<Back" button
Private Sub CommandButton1_Click()
Select Case MultiPage1.Value
Case 1 ' If Page2 is active...
With MultiPage1
.Pages(0).Enabled = True ' Enable Page1.
.Value = MultiPage1.Value - 1 ' Move back 1 page.
.Pages(1).Enabled = False ' Disable Page2.
End With
CommandButton1.Enabled = False ' Disable Back button.
Case 2 ' If Page3 is active...
With MultiPage1
.Pages(1).Enabled = True ' Enable Page2.
.Value = MultiPage1.Value - 1 ' Move back 1 page.
.Pages(2).Enabled = False ' Disable Page3.
CommandButton2.Caption = "Next>"
End With
End Select
End Sub
' Procedure for the "Next>" button
Private Sub CommandButton2_Click()
Select Case MultiPage1.Value
Case 0 ' If Page1 is active...
With MultiPage1
.Value = MultiPage1.Value + 1 ' Move forward 1 page.
.Pages(1).Enabled = True ' Enable Page2.
.Pages(0).Enabled = False ' Disable Page1.
End With
CommandButton1.Enabled = True ' Enable Back button.
Case 1 ' If Page2 is active...
With MultiPage1
.Value = MultiPage1.Value + 1 ' Move forward 1 page.
.Pages(2).Enabled = True ' Enable Page3.
.Pages(1).Enabled = False ' Disable Page2.
End With
CommandButton2.Caption = "Finish" ' Change Next button to Finish.
Case 2 ' If Page3 is active...
MsgBox "Finished!" ' User is Finished.
Unload Me ' Unload the UserForm.
End Select
End Sub
9. 在 运行 菜单上, 单击 运行子过程 / 用户窗体 。
当您单击下一步 > 激活 Page 2 后退 " 按钮可, 和 < 当您单击 Next > 第二次, Page3 激活并题注为 CommandButton 2 更改为 " 完成 "。
 

ScrollBar 控件

当您需要更改由另一控件, 如 Label 控件显示值可以使用 ScrollBar 控件。

如何更改 Label 控件是基于值 ScrollBar 控件

若要 Label 控件的 Caption 属性更改到当前设置的 Value 属性的 ScrollBar 控件, 请按照下列步骤:
1. 启动 Excel, 并打开新空白工作簿。
2. 在 工具 菜单, 指向 宏 , 然后单击 VisualBasic 编辑器 。
3. 在 插入 菜单上, 单击要在工作簿中插入 UserForm UserForm 。
4. 添加到 UserForm ScrollBar 控件。
5. 将 Label 控件添加到 UserForm。
6. 双击要打开代码窗口对 ScrollBar 控件 ScrollBar 控件。
7. 在代码窗口, 为 ScrollBar 1 Change 事件键入下列代码:
Private Sub ScrollBar1_Change()
Label1.Caption = ScrollBar1.Value
End Sub
8. 在 运行 菜单上, 单击 运行子过程 / 用户窗体 。
当您通过使用 ScrollBar 控件, 滚动 Label 1 更新与对 ScrollBar 控件当前值。
 

数值调节钮控件

到递增或递减其他控件, 如 Label 控件的值经常使用 数值调节钮 控件, 像 ScrollBar 控件,。 SmallChange 属性决定多少值的 数值调节钮 控件单击它时更改。

如何添加 SpinButton 控件, 增加或减少存储在 TextBox 控件中日期

要添加 SpinButton 控件, 增加或减少日期存储在 TextBox 控件, 请按照下列步骤操作:
1. 启动 Excel, 并打开新空白工作簿。
2. 在 工具 菜单, 指向 宏 , 然后单击 VisualBasic 编辑器 。
3. 在 插入 菜单上, 单击要在工作簿中插入 UserForm UserForm 。
4. 将 数值调节钮 控件添加到 UserForm。
5. 将 TextBox 控件添加到 UserForm。
6. 双击要打开代码窗口为 SpinButton 控件 SpinButton 控件。
7. 在代码窗口, 为 SpinButton 1 SpinUp 事件键入下列代码:
Private Sub SpinButton1_SpinUp()
TextBox1.Text = DateValue(TextBox1.Text) + 1
End Sub
8. 在代码窗口, 为 SpinButton 1 SpinDown 事件键入下列代码:
Private Sub SpinButton1_SpinDown()
TextBox1.Text = DateValue(TextBox1.Text) - 1
End Sub
9. 在代码窗口, 为 UserForm Initialize 事件键入下列代码:
Private Sub UserForm_Initialize()
TextBox1.Text = Date
End Sub
10. 在 运行 菜单上, 单击 运行子过程 / 用户窗体 。
出现 UserForm 时, TextBox 中显示当前日期。 单击 数值调节钮 控件, 时增加或减少日期为由一天。

在本示例, 如果更改是 SpinButton 1 , SmallChange 属性, 不影响天数当您单击 SpinButton 1 被通过更改 TextBox 中项。 过程, 附加到 SpinUp 事件和 SpinDown 事件是 SpinButton 1 天数仅由决定。

 
RefEdit 控件 imitates 引用框 Excel 中内置行为。 Value 属性可用于获取当前单元格地址存储在 RefEdit 控件。

您选择通过使用 RefEdit 控件如何填充的单元格区域基于区域

要使用 RefEdit 控件来填充单元格, 请按照下列步骤:
1. 启动 Excel, 并打开新空白工作簿。
2. 在 工具 菜单, 指向 宏 , 然后单击 VisualBasic 编辑器 。
3. 在 插入 菜单上, 单击要在工作簿中插入 UserForm UserForm 。
4. 添加到 UserForm RefEdit 控件。
5. 将 CommandButton 控件添加到 UserForm。
6. 双击要打开代码窗口为 CommandButton 控件 CommandButton 控件。
7. 在代码窗口, 为 CommandButton 1 Click 事件键入下列代码:
Private Sub CommandButton1_Click()
Dim MyRange As String
MyRange = RefEdit1.Value
Range(MyRange).Value = "test"
Unload Me
End Sub
8. 在 运行 菜单上, 单击 运行子过程 / 用户窗体 。

UserFormappears。
9. 单击按钮 RefEdit 控件中。

注意 UserForm 折叠。
10. 选定的单元格 A 1: A 5, 如区域, 然后单击按钮以展开 UserForm RefEdit 控件中。
11. 单击 CommandButton 1 。
UserForm 关闭并且, 所选单元格现在包含单词 " 测试 "。

 

图像控件

图像 控件的目的是为了 UserForm 上显示图片。 要将图片分配给 图像 控件在运行时, 使用 LoadPicture 函数。

如何加载图像控件中图片

若要插入 图像 控件提示您要选择图片来单击 图像 控件, 时加载请按照下列步骤:
1. 启动 Excel, 并打开新空白工作簿。
2. 在 工具 菜单, 指向 宏 , 然后单击 VisualBasic 编辑器 。
3. 在 插入 菜单上, 单击要在工作簿中插入 UserForm UserForm 。
4. 添加 UserForm 上 图像 控件。
5. 双击要打开代码窗口为 图像 控件 图像 控件。
6. 在代码窗口, 为 Image1 Click 事件键入下列代码:
Private Sub Image1_Click()
Dim fname As String
' Display the Open dialog box.
fname = Application.GetOpenFilename(filefilter:= _
"Bitmap Files(*.bmp),*.bmp", Title:="Select Image To Open")
' If you did not click Cancel...
If fname <> "False" Then
' Load the bitmap into the Image control.
Image1.Picture = LoadPicture(fname)
' Refresh the UserForm.
Me.Repaint
End If
End Sub
7. 在 运行 菜单上, 单击 运行子过程 / 用户窗体 。

UserForm 出现。
8. 单击 图像 控件。

当您单击 图像 控件, 出现 图像选择要打开 对话框, 然后您可选择位图文件插入控件。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多