Blog信息 |
blog名称:注册会计师(注会)练习软件 日志总数:398 评论数量:116 留言数量:27 访问次数:3273385 建立时间:2005年6月6日 |

| |
微软网站复制过来:VBA基础查询 软件技术
吕向阳 发表于 2008/5/15 0:36:25 |
概要本文介绍如何更改用户窗体编程 MicrosoftExcel 中。 它包括示例和 Microsoft Visual Basic for Applications (VBA) 宏, 显示您如何利用用户窗体的功能以及如何使用 ActiveXÖ 控件可供用户窗体。
简介用户窗体的基本原则描述如何在显示用户窗体、 如何暂时隐藏用户窗体, 以及如何消除用户窗体。 您是还说明如何使用 Initialize 事件、 Click 事件和 Terminate 事件最常见事件与 UserFormsù 相关联。 一个或多个以下示例演示如何使用 UserForm 中每个以下 ActiveXÖ 控件: • 标签 控件 • TextBox 控件 • CommandButton 控件 • ListBox 控件 • ComboBox 控件 • 框架 控件 • OptionButton 控件 • CheckBox 控件 • 切换按钮 控件 • TabStrip 控件 • multiPage 控件 • ScrollBar 控件 • 数值调节钮 控件 • RefEdit 控件 • 图像 控件
回到顶端
INTRODUCTION本文描述如何使用 VBA 用户窗体在 Excel 中进行更改。 回到顶端
更多信息Microsoft 提供编程示例仅, 供图示不附带任何明示或暗示。 这包括, 但不仅限于, 适销性或用于特定目的适用性的暗示保证。 本文假定您已熟悉与正在演示编程语言以及工具来调试过程来创建和使用。 Microsoft 支持工程师可以帮助解释功能的特定过程, 但它们将会修改这些示例以提供添加功能或构建过程以满足特定要求。 回到顶端
UserForm 基础如何显示 UserForm用于显示 UserForm 编程语法是: UserFormName .Show要显示名 UserForm1, UserForm 请使用以下代码: UserForm1.Show不显示它实际上可以加载到内存 UserForm。 复杂 UserForm 可能需要几秒来显示。 因为预先您能加载到内存, UserForm 可决定何时导致此开销。 要加载到内存 UserForm1 不显示它, 请使用以下代码: Load UserForm1要显示 UserForm, 必须使用 Show 方法以前已显示。 要暂时隐藏 UserForm 如何如果要暂时隐藏 UserForm, 使用 隐藏 方法。 可能想要隐藏 UserForm 如果应用程序涉及用户窗体之间移动。 要隐藏 UserForm, 请使用以下代码: UserForm1.Hide有关其他信息, 请单击下列文章编号以查看 Microsoft 知识库中相应: 213747 (http://support.microsoft.com/kb/213747/) XL 2000: 如何用户自定义窗体带有命令按钮之间移动 如何从内存删除 UserForm要从内存, 删除 UserForm 使用 Unload 语句。 要卸载 UserForm 名 UserForm1, 请使用以下代码: Unload UserForm1如果您卸载 UserForm, 是与 UserForm 或, 是与用户窗体上控件的事件过程中您可以使用 " Me " 关键字的 UserForm 的名称而 (例如, 您单击 CommandButton 控件),。 要使用 " 我 " 关键字来卸载 UserForm, 请使用以下代码: Unload Me如何使用 UserForm 事件用户窗体支持许多预定义事件, 您可以附加到 VBA 过程。 事件发生时, 附加到事件过程运行。 是由用户执行单个操作可启动多事件。 之间最经常 UserForm 使用事件是 Initialize 事件、 Click 事件和 Terminate 事件。
A 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 事件代码更改 Width 属性和 Height 属性是 UserForm Resize 事件发生两次。
关闭 UserForm 初始化 QueryClose 事件。 QueryClose 事件显示消息框包含标题, 您赐予 UserForm 代码中为 Initialize 事件。 可以使用时要执行特定的操作集如果用户关闭 UserForm QueryClose 事件。
然后生成一个消息框, 指出的 UserForm 标题是 UserForm1 Terminate 事件。 Terminate 事件是从内存移除 UserForm 并返回到其原始状态的 UserForm 标题后发生。 如何防止 UserForm 关闭通过关闭按钮当您运行 UserForm, 关闭 按钮添加到 UserForm 窗口的右上角。 如果要防止 UserForm 关闭通过 关闭 按钮, 您必须捕获 QueryClose 事件。
QueryClose 事件发生前 UserForm 是内存中卸载。 使用的 QueryClose 事件 CloseMode 参数来确定如何 UserForm 关闭。 vbFormControlMenu 值对于 CloseMode 参数指示, 关闭 按钮被单击。 若要保留 UserForm 活动, 请将对 QueryClose 事件 Cancel 参数设为 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。
有关其他信息, 请单击文章编号, 查看 Microsoft 知识库文章中文章: 207714 (http://support.microsoft.com/kb/207714/) 使用用户窗体集合 XL 2000: 运行时错误 211527 (http://support.microsoft.com/kb/211527/) XL 2000: 无法将 UserForm 控件拖到工作表 211868 (http://support.microsoft.com/kb/211868/) 运行宏, 插入控件 UserForm XL 2000: 错误 213582 (http://support.microsoft.com/kb/213582/) 当您使用宏将控件添加到 UserForm XL 2000: 问题 213583 (http://support.microsoft.com/kb/213583/) XL 2000: 无法显示其他项目中用户窗体 213736 (http://support.microsoft.com/kb/213736/) 如何确定密钥以及鼠标按 XL 2000: 213744 (http://support.microsoft.com/kb/213744/) XL 2000: 如何来暂时隐藏 UserForm 213747 (http://support.microsoft.com/kb/213747/) XL 2000: 如何用户自定义窗体带有命令按钮之间移动 213749 (http://support.microsoft.com/kb/213749/) XL 2000: 如何用于 UserForm 进入数据 213768 (http://support.microsoft.com/kb/213768/) XL 2000: 如何动态调整用户窗体 213774 (http://support.microsoft.com/kb/213774/) XL 2000: 如何创建具有 UserForm 启动屏幕 回到顶端
VBA 代码Excel 包含 15 个其他控件, 您可以使用用户窗体上。 本节包含各种示例使用这些控件编程。
注意 本文中包含 The VBA 代码不包含影响所有属性和控件事件示例。 如果您需要, 可使用 @ @ Properties@@ @ 窗口以查看可供控件属性的列表。 要在 视图 菜单上, 查看列表的属性, 请单击 属性窗口 。 如何使用设计模式来编辑控件当您使用 VisualBasic 编辑器来设计一个对话框, 使用设计模式。 在设计模式, 编辑控件然后您可更改 UserForm 在属性窗口上的控件属性。 要在 视图 菜单上, 显示属性窗口, 请单击 属性窗口 。
当您处在设计模式 注意 对事件不响应控件。 当您运行一个对话框, 显示方式, 用户看到它, 程序处于运行模式。 在做到运行模式中的控件属性更改, UserForm 是内存中卸载时不保留。
注意 控件执行响应事件在运行模式。 如何引用 UserForm 上控件如何您引用控件编程取决于 VisualBasic 模块表运行代码的类型。 如果从常规模块, 运行代码语法是: UserFormName.Controlname.Property = 值 例如, 如果要设置名为 UserForm UserForm1 名到 Bob , 的值上 TextBox TextBox 控件的 Text 属性使用以下代码: UserForm1.TextBox1.Text = "Bob"如果代码是由控件的事件或 UserForm, 启动过程中, 执行不需要引用 UserForm 名称。 相反, 使用以下代码: TextBox1.Text = "Bob"当代码附加到对象, 代码附加到事件对该对象之一。 您将众多, 本文示例中代码附加到 CommandButton 对象的 Click 事件。 回到顶端
标签控件标签 控件主要用于描述 UserForm 上其他控件。 运行 UserForm 时 Label 控件不能编辑由用户。 到设置或返回一个 Label 控件中文本使用 Caption 属性。 其他常用属性用于格式化一个 Label 控件包括 Font 属性和 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 。 文本粗时间新罗马具有字体大小是 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. 键入密码 userform 在 TextBox 控件。 12. 单击 CommandButton 控件。 对于本例, 密码是 " userform "。 如果键入了错误密码, 您出现消息框, 指示密码不正确、 TextBox 控件被清除, 并重新然后您可键入密码。 当您键入正确密码, 收到欢迎消息, 并 UserForm 关闭。
有关其他信息, 请单击下列文章编号以查看 Microsoft 知识库中相应: 213555 (http://support.microsoft.com/kb/213555/) XL 2000: 无数据验证属性对于 UserForm TextBoxes 回到顶端
CommandButton 控件可用于 CommandButton 控件启动 VBA 过程。 VBA 过程通常附加到 CommandButton 控件的 Click 事件。 要使用, Click 事件发生时运行过程 CommandButton 控件请按照下列步骤: 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 控件的背景颜色更改每次, 单击它。
有关 CommandButton 控件, 有关信息请单击下列文章编号, 查看 Microsoft 知识库文章中文章: 213572 (http://support.microsoft.com/kb/213572/) Clicking Cancel 按钮 XL 2000: 可能没有消除 UserForm 213743 (http://support.microsoft.com/kb/213743/) XL 2000: 如何 UserForm 上设置默认命令按钮 回到顶端
ListBox 控件ListBox 控件的目的是为了向用户显示要选择的项目列表。 您可以存储 ListBox 控件在 Excel 工作表项列表。 使用 RowSource 属性来填充工作表, 上 ListBox 控件与单元格范围。 ListBox 控件在使用 MultiSelect 属性, 时可设置为接受多重选择。 如何从 ListBox 控件获取当前选定项使用 ListBox 控件的 Value 属性可返回当前选定项。 要返回单个选择, 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 。 与单元格 A 1: A 5 Sheet 中值填充 ListBox 1 。 如何填充一个 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 过程生成简单数组, 并数组中添加项向 ListBox 控件通过 AddItem 方法。 然后, 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: E5, Sheet 将值添加到一个 ListBox 1 次循环宏过程。
单元格 A 1: E5 注意 ListBox 1 与不定 Sheet。 如何从 ListBox 控件绑定到多列的数据返回多个值您可以格式 ListBox 控件以显示多个列的数据。 这意味着, ListBox 控件列表每一行上显示多个项目。 要从列表, 中选定项返回多个值请按照下列步骤操作: 1. 启动 Excel, 并打开新空白工作簿。 2. Sheet 指示是单元格中键入以下数据:
A 1: 年 B1: 区域 C1: 销售 A 2: 1996 B: 北美 C 2: 140 A: 1996 B 3 南部: C 3: 210 A: 1997 B: 北美 C: 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 属性, 中键入 Sheet1!A1:A5. 8. 将 CommandButton 控件添加到 UserForm。 9. 双击 CommandButton 控件以显示 CommandButton 控件的代码窗口。 10. 在代码窗口, 键入以下代码为 CommandButton 1 Click 事件: Private Sub CommandButton1_Click()
ListBox1.RowSource = ""
End Sub 11. 在 运行 菜单上, 单击 运行子过程 / 用户窗体 。
ListBox 控件, 添加到 UserForm 填充了, 您在 Sheet 1 上输入值。 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 中删除所有项目。
有关 ListBox 控件, 有关信息请单击下列文章编号, 查看 Microsoft 知识库文章中文章: 161598 (http://support.microsoft.com/kb/161598/) OFF: 如何向 ComboBox 或 ListBox Excel 或 Word 中添加数据 211446 (http://support.microsoft.com/kb/211446/) TextColumn 属性显示第一列仅 XL 2000: 211896 (http://support.microsoft.com/kb/211896/) XL 2000: 如何模拟对于用户窗体组合列表编辑控件 211899 (http://support.microsoft.com/kb/211899/) XL 2000: 问题设置 ListBox 控件中列标题 213721 (http://support.microsoft.com/kb/213721/) XL 2000: 如何从 ListBox 或 ComboBox 删除所有项目 213722 (http://support.microsoft.com/kb/213722/) XL 2000: 如何使用 TextColumn 属性 213723 (http://support.microsoft.com/kb/213723/) XL 2000: 如何从显示多列列表框返回值 213746 (http://support.microsoft.com/kb/213746/) XL 2000: 如何与多个区域填充列表框控件 213748 (http://support.microsoft.com/kb/213748/) XL 2000: 如何填充一个列表框基于另一个列表框 213752 (http://support.microsoft.com/kb/213752/) 当 RowSource 是数据绑定 XL 2000: 使用 AddItem 方法导致错误 213756 (http://support.microsoft.com/kb/213756/) XL 2000: 在 ListBox 或 ComboBox 控件使用 RemoveItem 方法 213759 (http://support.microsoft.com/kb/213759/) XL 2000: 如何确定列表框中选择哪些项目 回到顶端
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 , 依次 运行 。
ComboBox 控件填充, 然后出现 UserForm。 11. ComboBox 控件, 中键入 Mangoes (或任何值尚未是列表中)。 12. 单击 CommandButton 1 。 新值, 键入现在显示在列表末尾。 如何向列表添加新项如果 ComboBox 控件绑定到工作表当用户键入值是在列表中 ComboBox 控件, 尚未可能要向列表添加新值。 要添加与您添加到列表, ComboBox 控件中键入新值请按照下列步骤: 1. 启动 Excel, 并打开新空白工作簿。 2. 在单元格 A 1: A 5 Sheet, 键入值与要用于填充 组合框 控件。 3. 选择单元格 A 1: A 5 Sheet。 4. 插入 菜单上指向 名称 , 然后单击 定义 。
在 工作簿中名称 框中, 键入 ListRange 然后单击 OK. 这创建 ListRange 定义名称。 使用定义名称 ListRange 将 ComboBox 控件的 RowSource 属性绑定到工作表。 5. 在 工具 菜单, 指向 宏 , 然后单击 VisualBasic 编辑器 。 6. 在 插入 菜单上, 单击 UserForm 将 UserForm 插入工作簿。 7. 将 ComboBox 控件添加到 UserForm。 8. 在 属性 对于 ComboBox 1 , 键入 Sheet1!ListRange 作为 RowSource 属性。 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 , 键入 Sheet1!A1:A5 作为 RowSource 属性。 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 , 键入 Sheet1!A1:A5 作为 RowSource 属性。 9. 双击以打开代码窗口对 ComboBox 控件 ComboBox 控件。 10. ComboBox 控件, 代码窗口中键入以下代码为 ComboBox Click 事件: Private Sub ComboBox1_Click()
Application.OnTime Now, "DropDown_ComboBox"
End Sub 11. 将二个 ComboBox 控件添加到 UserForm。 12. 在 属性 对于 ComboBox2 , 键入 Sheet1!A6:A10 作为 RowSource 属性。 13. 在 运行 菜单上, 单击 运行子过程 / 用户窗体 。 单击 ComboBox 1 列表, 中的项列表 ComboBox2 自动出现。
有关 ComboBox 控件, 有关信息请单击下列文章编号, 查看 Microsoft 知识库文章中文章: 161598 (http://support.microsoft.com/kb/161598/) OFF: 如何向 ComboBox 或 ListBox Excel 或 Word 中添加数据 211446 (http://support.microsoft.com/kb/211446/) TextColumn 属性显示第一列仅 XL 2000: 211899 (http://support.microsoft.com/kb/211899/) XL 2000: 问题设置 ListBox 控件中列标题 213717 (http://support.microsoft.com/kb/213717/) 使用 DropDown 方法与 ComboBox XL 2000: 运行时错误 213718 (http://support.microsoft.com/kb/213718/) XL 2000: 如何显示组合框列表 UserForm 是否显示时 213721 (http://support.microsoft.com/kb/213721/) XL 2000: 如何从 ListBox 或 ComboBox 删除所有项目 213722 (http://support.microsoft.com/kb/213722/) XL 2000: 如何使用 TextColumn 属性 213752 (http://support.microsoft.com/kb/213752/) 当 RowSource 是数据绑定 XL 2000: 使用 AddItem 方法导致错误 213756 (http://support.microsoft.com/kb/213756/) XL 2000: 在 ListBox 或 ComboBox 控件使用 RemoveItem 方法 回到顶端
框架控件使用 Frame 控件进行分组 UserForm 中逻辑相关项。 框架 控件经常用于组 OptionButton 控件。 如何循环 Frame 控件上所有控件要使用 EachàNext For 循环来访问 Frame 控件, 中所有控件请按照下列步骤: 1. 启动 Excel, 并打开新空白工作簿。 2. 在 工具 菜单, 指向 宏 , 然后单击 VisualBasic 编辑器 。 3. 在 插入 菜单上, 单击 UserForm 将 UserForm 插入工作簿。 4. 将 Frame 控件添加到 UserForm。 5. 将一个 OptionButton 控件添加到 Frame 控件。
重复此步骤以 Frame 控件中添加两详细 OptionButton 控件。 6. 双击以打开代码窗口 Frame 控件的 Frame 控件。 7. 在代码窗口, 键入下列代码对于 Frame 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 控件中所有控件是不可用。 如果再次, 单击 Frame 控件控件可再次。 回到顶端
OptionButton 控件可使用 OptionButton 控件组进行组的选项之间选择一个。 您可以使用以下技术到组 OptionButton 控件之一: • 框架 控件 • GroupName 属性
注意 On 值, 是 值和 True 值表示已选中一个 OptionButton 。 Off 值、 无 值和 False 值表明 OptionButton 一个未选中。 如何确定当 OptionButton 控件位于 Frame 控件被选定, OptionButton 控件通过使用 Frame 控件, OptionButtons 控件分组时您可以确定 OptionButton 控件所选通过循环 Frame 控件中所有控件并检查每个控件的 Value 属性。 要确定所选, 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 控件, Group1 在 GroupName 属性。 7. 重复步骤 4 和 5 以创建另一个 Frame 控件包含三个 OptionButton 控件。 8. 键入二 Frame 控件, 中每个 OptionButton 控件 Group2 在 GroupName 属性。 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 控件当前选定的标题。
有关 OptionButton 控件, 请单击下列文章编号以查看 Microsoft 知识库中相应: 213724 (http://support.microsoft.com/kb/213724/) XL 2000: 问题为选项按钮使用 TripleState 属性 |
|
|