Excel使用VBA窗体的教程_vba使用窗体操作表格

2020-02-27 其他范文 下载本文

Excel使用VBA窗体的教程由刀豆文库小编整理,希望给你工作、学习、生活带来方便,猜你可能喜欢“vba使用窗体操作表格”。

中文部分

如何更改用户窗体在 Excel 2003,Excel 2002 和 Excel 2000 中的使用 vba 的应用程序(VBA)

查看本文应用于的产品 本页 概要 简介 更多信息

用户窗体基础知识 如何显示用户窗体 如何暂时隐藏用户窗体

若要从内存中删除用户窗体的方法 如何使用用户窗体事件 如何捕获用户窗体事件

如何防止用户窗体被关闭,通过使用关闭按钮 VBA 代码

如何使用设计模式中编辑控件 如何在用户窗体上的控件,请参阅 标签控件

如何使用 WITH 语句设置 Label 控件的格式 文本框控件

如何使用验证密码 TextBox 控件 命令按钮控件 列表框控件

如何获取当前选定的项从列表框控件

如何获取多个选择列表框控件中的选定的项

如何使用来填充列表框控件与单元格在工作表上的行来源属性 如何填充数组中的值与列表框控件

如何使用工作表上的水平单元格区域来填充列表框控件 如何从列表框控件所绑定到的数据的多个列中返回多个值 如何从列表框控件所绑定到工作表中删除所有项目 如何删除未绑定到工作表的列表框控件的所有项目 组合框控件

如何将新项添加到列表中,如果组合框控件没有绑定到工作表 如何将新项添加到列表中,如果组合框控件所绑定到工作表 如何显示一个组合框控件的列表,用户窗体出现时

如何在另一个组合框控件中进行选择时显示的一个组合框控件列表 框架控件

如何循环访问一框架控件上的所有控件 选项按钮控件

如何确定选项按钮控件是在一框架控件上时选择了该选项按钮控件 如何确定已选中该选项按钮控件 复选框控件

如何检查复选框控件的值 切换按钮控件

如何获取切换按钮控件的值

如何创建一组互斥的切换按钮控件 TabStrip 控件

如何以编程方式控制 TabStrip 控件 多页控件

如何以编程方式控制多页控件

如何通过使用多页控件中创建一个向导界面 滚动条控件

如何更改基于滚动条控件的值的 Label 控件 数值调节钮控件

如何添加一个递增的数值调节钮控件或递减 TextBox 控件中存储的日期 RefEdit 控件

如何填充基于您使用 RefEdit 控件来选择该区域的单元格区域 图像控件

如何加载图像控件中的图片 其他信息 对象浏览器

展开全部 | 关闭全部

概要

这篇文章介绍了如何更改用户窗体以编程方式在 Excel 中。它包括示例和 Microsoft Visual Basic 用于向您展示如何利用用户窗体的功能以及如...这篇文章介绍了如何更改用户窗体以编程方式在 Excel 中。它包括示例和 Microsoft Visual Basic 用于向您展示如何利用用户窗体的功能以及如何使用 ActiveX ™ 控件,供用户窗体的应用程序(VBA)宏。

用户窗体的基本原则的介绍,介绍如何显示用户窗体、如何暂时隐藏用户窗体,以及如何消除用户窗体。您还显示了如何使用与用户窗体相关联的最常见事件 — — 初始化 事件、Click 事件和 终止 事件。一个或多个下面的示例演示如何在用户窗体中使用的每个以下 ActiveX ™ 控件: 标签 控件

TextBox 控件

命令按钮 控件

列表框 控件

组合框 控件

框架 控件

选项按钮 控件

复选框 控件

切换按钮 控件

TabStrip 控件

multiPage 控件

滚动条 控件

数值调节钮 控件

RefEdit 控件 图像 控件 回到顶端 简介

本文介绍如何使用 VBA 在 Excel 中的用户窗体中进行更改。本文介绍如何使用 VBA 在 Excel 中的用户窗体中进行更改。回到顶端 更多信息

Microsoft 提供的编程示例只,用于说明不附带任何明示或暗示保证。这包括,但不限于对适销性或针对特定用途的适用性的暗示的担保。本文假定您熟悉演示了正在使用...Microsoft 提供的编程示例只,用于说明不附带任何明示或暗示保证。这包括,但不限于对适销性或针对特定用途的适用性的暗示的担保。本文假定您熟悉演示了正在使用的编程语言以及用于创建和调试过程的工具。Microsoft 支持工程师可以帮助解释某个特定过程的功能,但他们不会修改这些示例以提供额外的功能或构建过程来满足您的具体要求。回到顶端

用户窗体基础知识 如何显示用户窗体

以编程方式显示用户窗体使用的语法是下列:

UserFormName.Show 若要显示名为 UserForm1 用户窗体,请使用下面的代码:

UserForm1.Show 您可以加载到内存用户窗体而不实际显示它。可能需要复杂的用户窗体几秒钟,会出现。因为您可以预用户窗体加载到内存中,您可以决定何时导致此开销。而不显示它加载到内存的 UserForm1,使用下面的代码: 若要显示该过程用户窗体的Load UserForm1 必须使用 Show 方法以前显示的。

如何暂时隐藏用户窗体

如果要暂时隐藏用户窗体,请使用 隐藏 方法。您可能希望隐藏用户窗体,如果您的应用程序涉及到用户窗体之间移动。若要隐藏用户窗体,请使用下面的代码:

UserForm1.Hide 有关更多的信息请单击下面的文章编号,以查看 Microsoft 知识库中相应的文章:

213747(http://support.microsoft.com/kb/213747/)XL2000: 如何使用命令按钮的自定义用户窗体之间移动

若要从内存中删除用户窗体的方法

若要从内存中删除用户窗体使用 卸载 语句。若要卸载用户窗体名为 UserForm1,使用下面的代码:

Unload UserForm1 如果您卸载与用户窗体关联,或与用户窗体上的控件相关联的事件过程中用户窗体(例如对于单击 命令按钮 控件)时,您可以使用“me”关键字而不是用户窗体的名称。若要使用“me”关键字卸载用户窗体,使用下面的代码:

Unload Me 如何使用用户窗体事件

用户窗体支持许多预定义的事件您可以将附加的 VBA 过程。当发生事件时您附加到该事件过程将运行。由用户执行的单个操作可以启动多个事件。最经常使用的用户窗体的事件包括 初始化 事件、Click 事件和 终止 事件。

注意在 Visual Basic 模块包含事件过程可能被称为用户窗体后面的模块。一个模块包含事件过程,将不可见的 Microsoft 项目资源管理器窗口 Visual Basic 编辑器的 模块 集合中。您必须双击用户窗体可以查看用户窗体代码模块的正文。

如何捕获用户窗体事件

补漏白用户窗体事件,请按照下列步骤操作:

在 Excel 中创建一个新的工作簿。

在 工具 菜单上指向 宏,然后单击 Visual Basic 编辑器。

在 插入 菜单上单击 用户窗体 插入您的工作簿中的用户窗体。

双击要为用户窗体显示在代码窗口在 用户窗体。

在模块中键入以下代码:

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 在 运行 菜单上单击 运行子过程/用户窗体。

当首次加载用户窗体,时该宏使用 初始化 事件更改为“事件事件事件!和 背景色 属性,以深蓝色的用户窗体的 标题 属性。

单击用户窗体,时您会触发 Click 事件。Click 事件调整用户窗体的大小。因为您创建了的 Resize 事件过程,所以您会收到两个消息框,单击用户窗体后。Resize 事件发生两次,因为后 Click 事件的代码更改 宽度 属性和用户窗体的 高度 属性。

关闭用户窗体初始化 QueryClose 事件。该 QueryClose 事件显示一个消息框,其中包含您在代码中赋予用户窗体,初始化 事件的标题。您可以使用 QueryClose 事件时要执行一组特定的操作,如果用户关闭用户窗体。

终止 事件,然后将生成一个消息框指出用户窗体的标题是 UserForm1。在 终止 事件发生后就会从内存中删除用户窗体和用户窗体的标题返回到其原始状态。

如何防止用户窗体被关闭,通过使用关闭按钮

您在运行用户窗体时在用户窗体窗口的右上角添加一个 关闭 按钮。如果要防止用户窗体通过使用 关闭 按钮关闭,您必须补漏白 QueryClose 事件。

用户窗体是从内存中卸载之前,将发生 QueryClose 事件。使用 QueryClose 事件的 CloseMode 参数来确定如何关闭用户窗体。CloseMode 参数 vbFormControlMenu 值表明在 关闭 按钮被单击。若要使用户窗体处于活动状态,请 QueryClose 事件的 取消 参数设置为 True。若要使用 QueryClose 事件来防止用户窗体通过使用 关闭 按钮关闭,请按照下列步骤操作:

在 Excel 中创建一个新的工作簿。

在 工具 菜单上指向 宏,然后单击 Visual Basic 编辑器。

在 插入 菜单上单击 用户窗体 插入您的工作簿中的用户窗体。

向用户窗体中添加 命令按钮 控件。

双击要为用户窗体显示在代码窗口在 用户窗体。

在代码窗口中键入以下代码:

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 在 运行 菜单上单击 运行子过程/用户窗体。

当您单击 关闭 按钮时,用户窗体未关闭。您必须单击关闭用户窗体的 命令按钮 控件。

有关更多的信息请单击下面的文章编号,以查看 Microsoft 知识库中相应的文章:

207714(http://support.microsoft.com/kb/207714/)XL2000: 使用用户窗体集合的运行时错误

211527(http://support.microsoft.com/kb/211527/)XL2000: 无法将用户窗体控件拖动到工作表

211868(http://support.microsoft.com/kb/211868/)XL2000: 错误运行宏的用户窗体中插入控件

213582(http://support.microsoft.com/kb/213582/)XL2000: 问题时,可以将控件添加到用户窗体中使用宏

213583(http://support.microsoft.com/kb/213583/)XL2000: 无法在其他项目中显示用户窗体

213736(http://support.microsoft.com/kb/213736/)XL2000: 如何确定键按下鼠标按钮一起

213744(http://support.microsoft.com/kb/213744/)XL2000: 如何暂时隐藏用户窗体

213747(http://support.microsoft.com/kb/213747/)XL2000: 如何使用命令按钮的自定义用户窗体之间移动

213749(http://support.microsoft.com/kb/213749/)XL2000: 如何使用用户窗体用于输入数据

213768(http://support.microsoft.com/kb/213768/)XL2000: 如何动态地调整用户窗体的大小

213774(http://support.microsoft.com/kb/213774/)XL2000: 如何使用用户窗体中创建一个启动屏幕 回到顶端 VBA 代码

Excel 包含 15 个不同的控件,您可以在用户窗体上使用。此部分包含以编程方式使用这些控件的各种示例。

注意包括在本文中的 VBA 代码不包含影响所有属性和控件事件的示例。如果必须为可以使用属性窗口以查看可用的控件的属性的列表。若要查看的属性,列表在 视图 菜单上,单击 属性窗口。

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

当您使用 Visual Basic 编辑器设计对话框时,您正在使用设计模式中。在设计模式中,您可以编辑控件并可以更改在属性窗口中的用户窗体上控件的属性。若要显示在 视图 菜单上的属性窗口,单击 属性窗口。

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

注意控件响应在运行模式中的事件。

如何在用户窗体上的控件,请参阅

您如何引用的控件以编程方式取决于您在其中运行代码的 Visual Basic 模块工作表的类型。如果从常规模块运行该代码,语法是下列:

UserFormName.Controlname.Property = Value 如果要将名为 TextBox1王俊元 的值名为 UserForm1 用户窗体上的 文本框 控件的 Text 属性设置,例如对于使用下面的代码:

UserForm1.TextBox1.Text = ”Bob“ 如果代码是通过控件的事件或用户窗体启动的过程中,您无需用户窗体的名称,请参阅。而是,使用下面的代码:

TextBox1.Text = ”Bob“ 时将代码附加到对象中,代码附加到该对象的事件之一。在很多本文示例中将代码附加到 命令按钮 对象的 Click 事件。回到顶端 标签控件

标签 控件主要用于描述在用户窗体上的其他控件。用户窗体运行时,不能由用户编辑 标签 控件。若要设置或返回一个 标签 控件中的文本,请使用 标题 属性。设置格式的 Label 控件的其他常用的属性包括 Font 属性并将 前景色 属性。

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

要使用 WITH 语句更改 Label 控件的属性,请按照下列步骤,请执行以下操作:

启动 Excel,然后打开一个新的空白工作簿。

在 工具 菜单上指向 宏,然后单击 Visual Basic 编辑器。

在 插入 菜单上单击 用户窗体 插入您的工作簿中的用户窗体。

向用户窗体中添加一个 Label 控件。

向用户窗体中添加 命令按钮 控件。

双击 命令按钮 控件的用户窗体中打开代码窗口。

在代码窗口中键入下面的代码为 CommandButton1 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 在 运行 菜单上单击 运行子过程/用户窗体。

单击 命令按钮。

文本加粗黑体字体大小为 14 中的 标签 控件”这是标签示例 1“出现。回到顶端 文本框控件

文本框 控件经常用于收集用户的输入。Text 属性将包含由 TextBox 控件中的项。如何使用验证密码 TextBox 控件

如果您设置 TextBox 控件的 PawordChar 属性,则它将成为”伪装的编辑“控件。您指定的字符以可视方式将取代 TextBox 控件中键入的每个字符。要使用 TextBox 控件验证密码,请按照下列步骤,请执行以下操作:

启动 Excel,然后打开一个新的空白工作簿。

在 工具 菜单上指向 宏,然后单击 Visual Basic 编辑器。

在 插入 菜单上单击 用户窗体 插入您的工作簿中的用户窗体。

向用户窗体中添加 文本框 控件。

在 视图 菜单上单击 属性,以使属性窗口可见。

在 文本框 控件 PawordChar 属性键入 *。

注意您正将该值改为一个星号。

向用户窗体中添加 命令按钮 控件。

双击 命令按钮 控件的用户窗体中打开代码窗口。

在代码窗口中键入下面的代码为 CommandButton1 Click 事件:

Private Sub CommandButton1_Click()

If TextBox1.Text ”userform“ Then

MsgBox ”Paword is Incorrect.Please reenter.“

TextBox1.Text = ”“

TextBox1.SetFocus

Else

MsgBox ”Welcome!“

Unload Me

End If

End Sub

在 运行 菜单上单击 运行子过程/用户窗体。

键入密码 userformTextBox 控件中。

单击 命令按钮 控件。

此例如密码为”userform“。如果您键入了不正确的密码,您将收到一个消息框,指出您的密码不正确、清除 TextBox 控件,然后重新键入密码。当您键入正确的密码时, 您会收到一个欢迎消息和用户窗体被关闭。

有关更多的信息请单击下面的文章编号,以查看 Microsoft 知识库中相应的文章:

213555(http://support.microsoft.com/kb/213555/)XL2000: 无数据验证属性的用户窗体文本框

回到顶端 命令按钮控件

使用 命令按钮 控件,可以启动 VBA 过程。VBA 过程通常被附加到 命令按钮 控件的 Click 事件。若要将一个 Click 事件发生时运行一个过程的 命令按钮 控件,请按照下列步骤操作:

启动 Excel,然后打开一个新的空白工作簿。在 工具 菜单上指向 宏,然后单击 Visual Basic 编辑器。

在 插入 菜单上单击 用户窗体 插入您的工作簿中的用户窗体。

向用户窗体中添加 命令按钮 控件。

双击要为用户窗体显示代码窗口的 命令按钮 控件。

在代码窗口中键入以下代码:

Private Sub CommandButton1_Click()

red = Int(Rnd * 255)

green = Int(Rnd * 255)

blue = Int(Rnd * 255)

CommandButton1.BackColor = RGB(red, green, blue)

End Sub

在 运行 菜单上单击 运行子过程/用户窗体。

背景色的 CommandButton1 控件更改每次单击它。

有关命令按钮控件的附加信息,请单击下面的文章编号,以查看 Microsoft 知识库中相应的文章:

213572(http://support.microsoft.com/kb/213572/)XL2000: 单击取消按钮可能不关闭用户窗体

213743(http://support.microsoft.com/kb/213743/)XL2000: 如何在用户窗体上设置为默认命令按钮 回到顶端 列表框控件

列表框 控件的用途是向用户显示要从选择的项的列表。您可以将存储在 Excel 工作表上的 列表框 控件的项列表。若要填充 列表框 控件与单元格区域的工作表上,使用 行来源 属性。您在使用 MultiSelect 属性时可以接受多个选定内容设置 列表框 控件。

如何获取当前选定的项从列表框控件

使用 列表框 控件的 Value 属性返回当前选定的项。若要在单个选择 列表框 控件中返回当前选定的项,请按照下列步骤操作:

启动 Excel,然后打开一个新的空白工作簿。

在单元格 a1: a5 工作表 Sheet1 上,键入想要使用来填充 列表框 控件的值。

在 工具 菜单上指向 宏,然后单击 Visual Basic 编辑器。

在 插入 菜单上单击 用户窗体 插入您的工作簿中的用户窗体。

向用户窗体中添加 列表框 控件。

双击 列表框 控件以显示 列表框 控件在代码窗口。

在代码窗口中键入下面的代码为 ListBox1 Click 事件: Private Sub ListBox1_Click()

MsgBox ListBox1.Value

End Sub

在 运行 菜单上单击 运行子过程/用户窗体。

当您单击列表中的某个项目时,当前选定的项将出现一个消息框。如何获取多个选择列表框控件中的选定的项

若要确定在选择多个 列表框 控件中选定的项,您必须依次通过在列表中的所有项目,然后查询 Selected 属性。若要选择多个 列表框 控件中返回当前选定的项目,请按照下列步骤操作:

启动 Excel,然后打开一个新的空白工作簿。

在单元格 a1: a5 工作表 Sheet1 上,键入想要使用来填充 列表框 控件的值。

在 工具 菜单上指向 宏,然后单击 Visual Basic 编辑器。

在 插入 菜单上单击 用户窗体 插入您的工作簿中的用户窗体。

向用户窗体中添加 列表框 控件。

在 视图 菜单上单击 属性 以显示的属性窗口。

键入表示为以下 列表框 控件属性的值:

Property

Value

-----------

-----------------------

MultiSelect

' 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

在 运行 菜单上单击 运行子过程/用户窗体。

在列表中选择一个或多个项目。

单击 CommandButton1。单击 CommandButton1 后,您在 列表框 控件中选择的每个项目将出现在单独的消息框中。在消息框中显示所有选定的项目后,用户窗体将自动关闭。

如何使用来填充列表框控件与单元格在工作表上的行来源属性

若要使用 行来源 属性来填充 列表框 控件中的单元格区域的工作表上,请按照下列步骤操作:

启动 Excel,然后打开一个新的空白工作簿。

在单元格 a1: a5 工作表 Sheet1 上,键入想要使用来填充 列表框 控件的值。

在 工具 菜单上指向 宏,然后单击 Visual Basic 编辑器。

在 插入 菜单上单击 用户窗体 插入您的工作簿中的用户窗体。向用户窗体中添加 列表框 控件。

向用户窗体中添加 命令按钮 控件。

双击要为用户窗体显示代码窗口的 命令按钮 控件。

在代码窗口中键入下面的代码为 CommandButton1 Click 事件:

Private Sub CommandButton1_Click()

ListBox1.RowSource = ”=Sheet1!A1:A5“ End Sub

在 运行 菜单上单击 运行子过程/用户窗体。

注意ListBox1 不包含任何值。

单击 CommandButton1。

ListBox1 将用在工作表 Sheet1 上的单元格 a1: a5 中值进行填充。

如何填充数组中的值与列表框控件

本示例显示如何填充 列表框 控件与一个数组变量。您必须将数组中的值一次分配给 列表框 控件的一个项目中。通常,此过程要求您使用如 For…Next 循环的循环结构。若要填充 列表框 控件与一个数组变量,请按照下列步骤操作:

启动 Excel,然后打开一个新的空白工作簿。

在 工具 菜单上指向 宏,然后单击 Visual Basic 编辑器。

在 插入 菜单上单击 用户窗体 插入您的工作簿中的用户窗体。

向用户窗体中添加 列表框 控件。

在 插入 菜单上单击 模块,插入模块工作表。

在代码窗口中键入以下代码:

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

在 工具 菜单上单击 宏 并单击 PopulateListBox,然后单击 运行。

PopulateListBox 过程生成一个简单的数组,然后在数组中将项目添加到 列表框 控件中,通过使用 AddItem 方法。然后,用户窗体将显示。

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

如果将 列表框 控件的 行来源 属性设置为单元格的水平区域,只有第一个值将出现在 列表框 控件中。

通过使用 AddItem 方法来填充 列表框 控件中的单元格的水平区域,请按照下列步骤操作: 启动 Excel,然后打开一个新的空白工作簿。

在工作表 Sheet1 上单元格 A1:E1,键入想要使用来填充 列表框 控件的值。

在 工具 菜单上指向 宏,然后单击 Visual Basic 编辑器。

在 插入 菜单上单击 用户窗体 插入您的工作簿中的用户窗体。

向用户窗体中添加 列表框 控件。

在 插入 菜单上单击 模块,插入模块工作表。

在代码窗口中键入以下代码:

Sub PopulateListWithHorizontalRange()

For Each x In Sheet1.Range(”A1:E1“)

UserForm1.ListBox1.AddItem x.Value

Next

UserForm1.Show

End Sub 在 工具 菜单上单击 宏 并单击 PopulateListWithHorizontalRange,然后单击 运行。宏过程循环一次添加到 ListBox1 之一的值的单元格 A1:E5 sheet1。

注意ListBox1 不会绑定到的单元格 A1:E5 工作表 Sheet1 上。

如何从列表框控件所绑定到的数据的多个列中返回多个值

您可以设置要显示的数据的多个列的 列表框 控件的格式。这意味着 列表框 控件列表的每一行上显示多个项目。若要从列表中选定的项返回多个值,请按照下列步骤操作:

启动 Excel,然后打开一个新的空白工作簿。

表示工作表 Sheet1 上的单元格中键入以下数据: 收起该表格展开该表格

A1: 年 a2: 1996年 a3: 1996年 B1: 区域 C1: 销售 B2: 北美 c2: 140 B3: 南

c3: 210 a4: 1997年 B4: 北美 c4: 190 a5: 1997年 B5: 南 c5: 195 在 工具 菜单上指向 宏,然后单击 Visual Basic 编辑器。

在 插入 菜单上单击 用户窗体 插入您的工作簿中的用户窗体。

向用户窗体中添加一个 Label 控件。

向用户窗体中添加 列表框 控件。

列表框 中,用鼠标右键单击,然后单击 属性。

键入或选择 列表框 控件的下列属性的指示,如列出下表中的值:

Property

Value

----------------------------

BoundColumn

ColumnCount

ColumnHeads

True

RowSource

Sheet1!A2:A5 双击 列表框 控件以显示 列表框 控件在代码窗口。

在代码窗口中键入下面的代码:

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 在 运行 菜单上单击 运行子过程/用户窗体。

当您单击 列表框 控件中的项时,该标签将更改为该条目中显示所有这三个项目。

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

若要从 列表框 控件所绑定到工作表中删除所有项目,清除 行来源 属性中存储的值。若要从 列表框 控件所绑定到工作表中删除项目,请按照下列步骤操作:

启动 Excel,然后打开一个新的空白工作簿。

在单元格 a1: a5 工作表 Sheet1 上,键入想要使用来填充 列表框 控件的值。

在 工具 菜单上指向 宏,然后单击 Visual Basic 编辑器。

在 插入 菜单上单击 用户窗体 插入您的工作簿中的用户窗体。

向用户窗体中添加 列表框 控件。

用鼠标右键单击 列表框 控件,然后单击 属性。

在 行来源 属性中键入 Sheet1!a1: a5。

向用户窗体中添加 命令按钮 控件。

双击 命令按钮 控件以显示 命令按钮 控件在代码窗口。

在代码窗口中键入下面的代码为 CommandButton1 Click 事件:

Private Sub CommandButton1_Click()

ListBox1.RowSource = ”“

End Sub 在 运行 菜单上单击 运行子过程/用户窗体。

您输入工作表 Sheet1 上的值填充 列表框 控件添加到用户窗体。

单击 CommandButton1。

从 ListBox1 中删除所有项目。

如何删除未绑定到工作表的列表框控件的所有项目

没有从 列表框 控件中删除所有项目,如果列表不会绑定到工作表的单个 VBA 命令。若要删除从 Visual Basic 数组填充 列表框 控件的所有项目,请按照下列步骤操作: 启动 Excel,然后打开一个新的空白工作簿。

在 工具 菜单上指向 宏,然后单击 Visual Basic 编辑器。

在 插入 菜单上单击 用户窗体 插入您的工作簿中的用户窗体。

向用户窗体中添加 列表框 控件。

在 插入 菜单上单击 模块,插入模块工作表。

在代码窗口中键入以下代码:

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

向用户窗体中添加 命令按钮 控件。

双击 命令按钮 控件以显示 命令按钮 控件在代码窗口。

在代码窗口中键入下面的代码为 CommandButton1 Click 事件:

Private Sub CommandButton1_Click()

For i = 1 To ListBox1.ListCount

ListBox1.RemoveItem 0

Next I

End Sub 在 工具 菜单上单击 宏 并单击 PopulateListBox,然后单击 运行。

填充 列表框 控件,和用户窗体随即出现。

单击 CommandButton1。

从 ListBox1 中删除所有项目。

有关列表框控件的其他信息,请单击下面的文章编号,以查看 Microsoft 知识库中相应的文章:

161598(http://support.microsoft.com/kb/161598/)OFF: 如何将数据添加到组合框或列表框,Excel 或 Word 中

211446(http://support.microsoft.com/kb/211446/)XL2000: TextColumn 属性显示仅在第一列

211896(http://support.microsoft.com/kb/211896/)XL2000: 如何模拟的用户窗体的组合列表编辑控件

211899(http://support.microsoft.com/kb/211899/)XL2000: 问题在列表框控件中设置列标题 213721(http://support.microsoft.com/kb/213721/)XL2000: 如何从列表框或组合框中删除所有项目

213722(http://support.microsoft.com/kb/213722/)XL2000: 如何使用 TextColumn 属性

213723(http://support.microsoft.com/kb/213723/)XL2000: 如何从一个列表框中显示多个列的返回值

213746(http://support.microsoft.com/kb/213746/)XL2000: 如何使用多个单元格区域来填充列表框控件

213748(http://support.microsoft.com/kb/213748/)XL2000: 如何填充一个列表框基于另一个列表框

213752(http://support.microsoft.com/kb/213752/)XL2000: 使用 AddItem 方法将行来源为数据绑定时导致错误

213756(http://support.microsoft.com/kb/213756/)XL2000: 使用 RemoveItem 方法与列表框或组合框控件

213759(http://support.microsoft.com/kb/213759/)XL2000: 如何确定列表框中选择了哪项 回到顶端 组合框控件

为一个下拉列表框或组合框中,您可以在列表中选择一个值,或键入一个新值,您可以使用 组合框 控件。样式 属性确定是否 组合框 控件都将作为一个下拉列表框或组合框。

注意列表框 控件上一节中的所有示例还可都应用于 组合框 控件的如何获取在选择多个列表框控件中的所选的项目示例除外。

如何将新项添加到列表中,如果组合框控件没有绑定到工作表

当您键入一个值,它已不在 组合框 控件中列表时, 您可能希望将新值添加到列表。若要将在新的值,如果 组合框 控件没有绑定到工作表,在 组合框 控件中键入,请按照下列步骤操作:

启动 Excel,然后打开一个新的空白工作簿。

在 工具 菜单上指向 宏,然后单击 Visual Basic 编辑器。

在 插入 菜单上单击 用户窗体 插入您的工作簿中的用户窗体。

向用户窗体中添加一个 组合框 控件。

在 插入 菜单上单击 模块,插入模块工作表。

在代码窗口中键入以下代码:

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 向用户窗体中添加 命令按钮 控件。

双击 命令按钮 控件以显示 命令按钮 控件在代码窗口。

在代码窗口中键入下面的代码为 CommandButton1 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 在 工具 菜单上单击 宏 并单击 PopulateListBox,然后单击 运行。

填充 组合框 控件,和用户窗体随即出现。

在 组合框 控件中键入 Mangoes(或列表中已不存在的任何值)。

单击 CommandButton1。

现在您所键入的新值显示在列表的末尾。

如何将新项添加到列表中,如果组合框控件所绑定到工作表

当用户在键入一个值,组合框 控件中列表中已不存在时您可能希望将新值添加到列表。若要将您在列表中将 组合框 控件中键入新值,请按照下列步骤操作:

启动 Excel,然后打开一个新的空白工作簿。

在单元格 a1: a5 工作表 Sheet1 上,键入想要使用来填充 组合框 控件的值。

选择工作表 Sheet1 上的单元格 a1: a5。

在 插入 菜单上指向 名称,然后单击 定义。

在 在当前工作簿中的名称 框中键入 ListRange,然后单击 确定。这将创建已定义的名称 ListRange。您可以使用已定义的名称 ListRange 绑定到该工作表的 行来源 属性的 组合框 控件。

在 工具 菜单上指向 宏,然后单击 Visual Basic 编辑器。

在 插入 菜单上单击 用户窗体 插入您的工作簿中的用户窗体。

向用户窗体中添加一个 组合框 控件。

在 属性 为 ComboBox1,键入 Sheet1!ListRange 作为 行来源 属性。

向用户窗体中添加 命令按钮 控件。

双击 命令按钮 控件以显示 命令按钮 控件在代码窗口。

在代码窗口中键入下面的代码为 CommandButton1 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“).Addre(external:=True)

End If

End Sub 在 运行 菜单上单击 运行子过程/用户窗体。

用户窗体将显示工作表 Sheet1 上。

在 组合框 控件中键入一个值,它在列表中已不存在。

单击 CommandButton1。

您在 组合框 控件中键入新项添加到列表和 组合框 控件绑定到的列表将被展开以包括单元格 A1:A6。

如何显示一个组合框控件的列表,用户窗体出现时

有时,可能显示一个 组合框 控件的列表,用户窗体首次出现时很有用。下面的示例使用的用户窗体,Activate 事件。若要显示的一个 组合框 控件列表,请按照下列步骤操作:

启动 Excel,然后打开一个新的空白工作簿。

在单元格 a1: a5 工作表 Sheet1 上,键入想要使用来填充 组合框 控件的值。

在 工具 菜单上指向 宏,然后单击 Visual Basic 编辑器。

在 插入 菜单上单击 用户窗体 插入您的工作簿中的用户窗体。

向用户窗体中添加一个 组合框 控件。

在 属性 为 ComboBox1,键入 Sheet1!a1: a5 作为 行来源 属性。

双击要为用户窗体显示在代码窗口在 用户窗体。

在代码窗口中键入下面的 命令按钮的 Click 事件的代码:

Private Sub UserForm_Activate()

ComboBox1.DropDown

End Sub 在 运行 菜单上单击 运行子过程/用户窗体。

用户窗体显示在 Sheet1,上,您可以为 ComboBox1 来查看列表。如何在另一个组合框控件中进行选择时显示的一个组合框控件列表

若要在另一个 组合框 控件中进行选择时,会自动显示一个 组合框 控件的列表,请按照下列步骤操作:

启动 Excel,然后打开一个新的空白工作簿。

在单元格 a1: a10 工作表 Sheet1 上,键入想要使用来填充 组合框 控件的值。

在 工具 菜单上指向 宏,然后单击 Visual Basic 编辑器。

在 插入 菜单上单击 模块。

模块代码窗口中键入以下代码:

Sub DropDown_ComboBox()

UserForm1.ComboBox2.DropDown

End Sub 在 插入 菜单上单击 用户窗体 插入您的工作簿中的用户窗体。

向用户窗体中添加一个 组合框 控件。

在 属性 为 ComboBox1,键入 Sheet1!a1: a5 作为 行来源 属性。

双击要打开 组合框 控件在代码窗口 组合框 控件。

在 组合框 控件在代码窗口中键入下面的代码的 组合框的 Click 事件:

Private Sub ComboBox1_Click()

Application.OnTime Now, ”DropDown_ComboBox“

End Sub 向用户窗体中添加第二个 组合框 控件。

在 属性 为 ComboBox2,键入 Sheet1!A6:A10 作为 行来源 属性。

在 运行 菜单上单击 运行子过程/用户窗体。

当您单击 ComboBox1 列表中的项时,ComboBox2 的列表会自动出现。

有关组合框控件的其他信息,请单击下面的文章编号,以查看 Microsoft 知识库中相应的文章:

161598(http://support.microsoft.com/kb/161598/)OFF: 如何将数据添加到组合框或列表框,Excel 或 Word 中

211446(http://support.microsoft.com/kb/211446/)XL2000: TextColumn 属性显示仅在第一列

211899(http://support.microsoft.com/kb/211899/)XL2000: 问题在列表框控件中设置列标题

213717(http://support.microsoft.com/kb/213717/)XL2000: 运行时错误 DropDown 方法使用组合框

213718(http://support.microsoft.com/kb/213718/)XL2000: 如何显示时显示一个用户窗体是一个组合框列表

213721(http://support.microsoft.com/kb/213721/)XL2000: 如何从列表框或组合框中删除所有项目

213722(http://support.microsoft.com/kb/213722/)XL2000: 如何使用 TextColumn 属性

213752(http://support.microsoft.com/kb/213752/)XL2000: 使用 AddItem 方法将行来源是数据绑定时导致错误 213756(http://support.microsoft.com/kb/213756/)XL2000: 使用 RemoveItem 方法与列表框或组合框控件 回到顶端 框架控件

使用 框架 控件在用户窗体中的逻辑上相关的项进行分组。框架 控件经常用于组 选项按钮 控件。

如何循环访问一框架控件上的所有控件

要使用 的 Each…Next 循环访问一 框架 控件中的所有控件,请按照下列步骤,请执行以下操作:

启动 Excel,然后打开一个新的空白工作簿。

在 工具 菜单上指向 宏,然后单击 Visual Basic 编辑器。

在 插入 菜单上单击 用户窗体 插入您的工作簿中的用户窗体。

将 框架 控件添加到用户窗体。

将一个 选项按钮 控件添加到 框架 控件。

重复此步骤可在 框架 控件中添加更多的两个 选项按钮 控件。

双击要打开代码窗口的 框架 控件 框架 控件。

在代码窗口中键入下面的代码的 框架 Click 事件:

Private Sub Frame1_Click()

Dim Ctrl As Control

For Each Ctrl In Frame1.Controls

Ctrl.Enabled = Not Ctrl.Enabled

Next

End Sub 在 运行 菜单上单击 运行子过程/用户窗体。

在该过程用户窗体中单击 框架 控件。

第一次单击 框架 控件 框架 控件中的所有控件都都不可用。如果您单击 框架 再次控制,该控件将再次可用。回到顶端 选项按钮控件

使用 数值调节钮 控件组可以使一组选项之间的一个所选内容。您可以使用以下技术组 选项按钮 控件的任意一个:

框架 控件

组名 属性

注意在 值、是 值和 True 值表明选择了一个 选项按钮。关闭 值、否 值和 False 值表明未选中一个 选项按钮。

如何确定选项按钮控件是在一框架控件上时选择了该选项按钮控件

当使用 框架 控件分组 OptionButtons 控件时,您可以确定由 框架 控件中的所有控件间都循环并检查每个控件的 Value 属性,可以选择该 选项按钮 控件。若要确定已选中该 选项按钮 控件,请按照下列步骤操作:

启动 Excel,然后打开一个新的空白工作簿。在 工具 菜单上指向 宏,然后单击 Visual Basic 编辑器。

在 插入 菜单上单击 用户窗体 插入您的工作簿中的用户窗体。

将 框架 控件添加到用户窗体。

将一个 选项按钮 控件添加到 框架 控件。

重复此步骤可在 框架 控件中添加更多的两个 选项按钮 控件。

添加 框架 控件的外部用户窗体上的 命令按钮 控件。

双击要为用户窗体显示代码窗口的 命令按钮 控件。

在代码窗口中键入下面的代码为 CommandButton1 Click 事件:

Private Sub CommandButton1_Click()

For Each x In Frame1.Controls

If x.Value = True Then

MsgBox x.Caption

End If

Next

End Sub 在 运行 菜单上单击 运行子过程/用户窗体。

在 用户窗体 中,单击一个的 选项按钮 控件,然后单击 CommandButton1。会显示一个消息框包含当前所选的 选项按钮 控件的标题。

如何确定已选中该选项按钮控件

下面的目的是示例的确定在分组 1 中选择该 选项按钮 控件。若要进行用户具有两组 选项按钮 控件的窗体,请按照下列步骤操作:

启动 Excel,然后打开一个新的空白工作簿。

在 工具 菜单上指向 宏,然后单击 Visual Basic 编辑器。

在 插入 菜单上单击 用户窗体 插入您的工作簿中的用户窗体。

将 框架 控件添加到用户窗体。

在 框架 控件中添加 选项按钮 控件。

重复此步骤可在 框架 控件中添加更多的两个 选项按钮 控件。

为每个 选项按钮 控件键入 组名 属性中的 分组 1。

重复步骤 4 和 5,创建包含三个 选项按钮 控件的第二个 框架 控件。

在第二个 框架 控件中每个 选项按钮 控件 组名 属性中键入 Group2。

添加 框架 控件外部用户窗体上的 命令按钮 控件。

双击要为用户窗体显示代码窗口的 命令按钮 控件。

在代码窗口中键入下面的代码为 CommandButton1 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 在 运行 菜单上单击 运行子过程/用户窗体。

在该过程用户窗体中单击 Group1,中的一个 选项按钮 控件,然后单击 CommandButton1。会显示一个消息框包含当前选定的 选项按钮 控件的标题。

有关选项按钮控件的其他信息,请单击下面的文章编号,以查看 Microsoft 知识库中相应的文章:

213724(http://support.microsoft.com/kb/213724/)XL2000: 问题的选项按钮使用 TripleState 属性

回到顶端 复选框控件

使用 复选框 控件,可以指示真或假值。与在其中一个复选标记将出现一个 复选框 控件表示值为 True。与没有复选标记将出现一个 复选框 表示值为 False。TrueTripleState 属性的值是否 CheckBox 控件也可以具有值为 Null。其值为 Null 的 复选框 控件显示为不可用。

注意在 值、是 值和 True 值表明 CheckBox 控件处于选定状态。关闭 值、否 值和 False 值表明清除了 复选框 控件。

如何检查复选框控件的值

要使用 Value 属性返回一个 复选框 控件的当前值,请按照下列步骤,请执行以下操作:

启动 Excel,然后打开一个新的空白工作簿。

在 工具 菜单上指向 宏,然后单击 Visual Basic 编辑器。

在 插入 菜单上单击 用户窗体 插入您的工作簿中的用户窗体。

向用户窗体中添加一个 复选框 控件。

对于 CheckBox1 属性列表中选择 True 作为 TripleState 属性。

双击要显示 复选框 控件在代码窗口 复选框 控件。

在代码窗口中键入下面的代码为 CheckBox1 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 在 运行 菜单上单击 运行子过程/用户窗体。

当您单击 复选框 控件的 复选框 控件更改以反映当前值的标题。回到顶端 切换按钮控件

切换按钮 控件都有用作 命令按钮 的控件相同的外观,直到您单击它。当您单击 切换按钮 控件时,它似乎被按下或向下推入。切换按钮 控件的 Value 属性是 True 时选择了按钮和 False 时未选中按钮。TrueTripleState 属性的值是否 切换按钮 控件也可以具有值为 Null。切换按钮 控件的 Null 值,它显示为不可用。

注意在 值、是 值和 True 值表示选中了 切换按钮 控件。关闭 值、否 值和 False 值表明未选中 切换按钮 控件。

如何获取切换按钮控件的值

若要获取的 切换按钮 控件的值,请按照下列步骤操作:

启动 Excel,然后打开一个新的空白工作簿。

在 工具 菜单上指向 宏,然后单击 Visual Basic 编辑器。

在 插入 菜单上单击 用户窗体 插入您的工作簿中的用户窗体。

添加在用户窗体上的 切换按钮 控件。

向用户窗体中添加一个 Label 控件。

双击要打开 切换按钮 控件在代码窗口 切换按钮 控件。

在代码窗口中键入下面的代码为 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 在 运行 菜单上单击 运行子过程/用户窗体。

当您单击 切换按钮 控件的用户窗体更改的背景色。

如何创建一组互斥的切换按钮控件

本示例将使用 MouseUp 事件来设置一个变量,并调用 ExclusiveToggleButtons 过程。ExclusiveToggleButtons 过程确定处于选中状态,然后取消其他的 切换按钮 控件。若要进行的一组互斥的 切换按钮 控件,请按照下列步骤操作: 启动 Excel,然后打开一个新的空白工作簿。

在 工具 菜单上指向 宏,然后单击 Visual Basic 编辑器。

在 插入 菜单上单击 模块。

模块代码窗口中键入以下代码:

' 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 在 插入 菜单上单击 用户窗体 插入您的工作簿中的用户窗体。

将 框架 控件添加到用户窗体。

在 框架 控件中添加一个 切换按钮 控件。

重复此步骤可在 框架 控件中添加两个更多的 切换按钮 控件。

双击要为用户窗体显示在代码窗口的 框架 控件。

模块代码窗口中键入下面的代码为 切换按钮 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 在 运行 菜单上单击 运行子过程/用户窗体。

当您单击 切换按钮 控件时,取消以前选定的 切换按钮 控件。回到顶端 TabStrip 控件

若要查看不同的信息的一组控件集使用 TabStrip 控件。

如何以编程方式控制 TabStrip 控件

若要将 图像 控件基于在选定的选项卡上的 背景色 属性,请按照下列步骤操作:

启动 Excel,然后打开一个新的空白工作簿。

在 工具 菜单上指向 宏,然后单击 Visual Basic 编辑器。

在 插入 菜单上单击 用户窗体 插入您的工作簿中的用户窗体。

向用户窗体中添加 TabStrip 控件。

添加 Image 控件覆盖 TabStrip 控件的基础的但是,不包括在选项卡。

在为 Image1 属性窗格中键入 和 H000000FF 和背景色 属性中。

双击要打开 TabStrip 控件在代码窗口 TabStrip 控件。

在代码窗口中键入下面的代码为 TabStrip1 更改 事件:

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 在 运行 菜单上单击 运行子过程/用户窗体。

图像 控件更改,具体取决于在 TabStrip 控件处于活动状态的页的颜色。

有关在 TabStrip 的其他信息控制,请单击下面的文章编号,以查看 Microsoft 知识库中相应的文章:

213254(http://support.microsoft.com/kb/213254/)XL2000: 如何使用用户窗体上的 TabStrip 控件

回到顶端 多页控件

使用 多页 控件处理大量可以将其分类到多个类别的信息。多页 控件组成的一个或多个 页 对象的每个包含一组不同的控件。您可以通过设置 多页 控件的 Value 属性以编程方式设置当前活动页。

如何以编程方式控制多页控件

若要添加 多页 控件和使用宏来对其进行控制,请按照下列步骤:

启动 Excel,然后打开一个新的空白工作簿。

在 工具 菜单上指向 宏,然后单击 Visual Basic 编辑器。

在 插入 菜单上单击 用户窗体 插入您的工作簿中的用户窗体。

向用户窗体中添加一个 多页 控件。

将 Label 控件添加到第一页中,在 多页 控件上。

将 TextBox 控件添加到第一页中,在 多页 控件上。

在 多页 控件上单击 Page2,然后重复第 5 步和第 6 步中添加一个 标签 控件和 TextBox 控件。

双击要打开代码窗口为 多页 控件的 多页 控件。

在代码窗口中键入下面的代码为 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 在代码窗口中键入 用户窗体初始化 事件的以下代码:

Private Sub UserForm_Initialize()

' Force Page1 to be active when UserForm is displayed.MultiPage1.Value = 0

Label1.Caption = ”“

End Sub 在 运行 菜单上单击 运行子过程/用户窗体。

在第一页上,文本框 控件中键入 测试。当单击 Page2 选项卡,清除 TextBox2,Label2 的标题更改为您在第一页上的 TextBox1 中所做的项(测试)。如何通过使用多页控件中创建一个向导界面

当某个任务需要多个增量步骤时,一个向导界面可以非常有效。使用 多页 控件可以创建一个向导界面,而不是使用多个用户窗体。本示例的操作对象是一个具有三个网页的 多页 控件。附加到用户窗体的 初始化 事件过程将禁用 Page2 和 Page3,并强制 多页 控件的第一页处于活动状态。

注意使用 Pages 集合中索引 多页 控件的网页,集合中的第一页时页零。此过程还将 命令按钮 控件的题注设置和禁用该

注意分配给 CommandButton1 的 Click 事件过程控制功能的该 的功能 按钮。若要使用 多页 控件创建一个向导界面,请按照下列步骤操作:

启动 Excel,然后打开一个新的空白工作簿。

在 工具 菜单上指向 宏,然后单击 Visual Basic 编辑器。

在 插入 菜单上单击 用户窗体 插入您的工作簿中的用户窗体。

向用户窗体中添加一个 多页 控件。

用鼠标右键单击 第一页 选项卡,然后单击 新建页 以将 Page3 添加到 多页 控件。

添加不在 多页 控件上的用户窗体上的 命令按钮 控件。

重复此步骤可添加在用户窗体上的第二个 命令按钮 控件。

双击要打开代码在 用户窗体 的用户窗体窗口。

在代码窗口中键入 用户窗体初始化 事件的以下代码:

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 = ”

CommandButton1.Enabled = False

CommandButton2.Caption = “Next>”

End Sub

' Procedure for the “

Select Case MultiPage1.Value

Case 1

' If Page2 is active...With MultiPage1

.Pages(0).Enabled = True

' Enable Page1..Value = MultiPage1.Value1 ' 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 在 运行 菜单上单击 运行子过程/用户窗体。当您单击 Next>,Page2 处于激活状态,并在 一次激活 Page3 并且 CommandButton2 的标题更改为”完成“。回到顶端 滚动条控件

当您希望更改的情况如 Label 控件的另一个控件显示值时,您可以使用 滚动条 的控件。

如何更改基于滚动条控件的值的 Label 控件

若要更改 标签 控件的 标题 属性以当前 滚动条 控件的 Value 属性的设置,请按照下列步骤操作:

启动 Excel,然后打开一个新的空白工作簿。

在 工具 菜单上指向 宏,然后单击 Visual Basic 编辑器。

在 插入 菜单上单击 用户窗体 插入您的工作簿中的用户窗体。

向用户窗体中添加 滚动条 控件。

向用户窗体中添加一个 Label 控件。

双击要打开代码窗口 滚动条 控件的 滚动条 控件。

在代码窗口中键入下面的代码为 ScrollBar1 Change 事件:

Private Sub ScrollBar1_Change()

Label1.Caption = ScrollBar1.Value

End Sub 在 运行 菜单上单击 运行子过程/用户窗体。

通过使用 滚动条 控件滚动时,Label1 将更新为 滚动条 控件的当前值。回到顶端

数值调节钮控件

像一个 滚动条 控件的 数值调节钮 控件经常使用以递增或递减如 Label 控件的另一个控件的值。SmallChange 属性用于确定多少 数值调节钮 控件的值发生更改时被单击。

如何添加一个递增的数值调节钮控件或递减 TextBox 控件中存储的日期

若要添加一个递增的 数值调节钮 控件或递减 TextBox 控件中存储的日期,请按照下列步骤操作:

启动 Excel,然后打开一个新的空白工作簿。

在 工具 菜单上指向 宏,然后单击 Visual Basic 编辑器。

在 插入 菜单上单击 用户窗体 插入您的工作簿中的用户窗体。

向用户窗体中添加 数值调节钮 控件。

向用户窗体中添加 文本框 控件。

双击要打开代码窗口,该 数值调节钮 控件的 数值调节钮 控件。

在代码窗口中键入下面的代码为 SpinButton1 SpinUp 事件:

Private Sub SpinButton1_SpinUp()

TextBox1.Text = DateValue(TextBox1.Text)+ 1

End Sub 在代码窗口中键入下面的代码为 SpinButton1 SpinDown 事件:

Private Sub SpinButton1_SpinDown()

TextBox1.Text = DateValue(TextBox1.Text)Last Review: January 11, 2006frmMultiSelectMulti

RowSource

Sheet1!A1:A8 Add a CommandButton control to the UserForm.Double-click the CommandButton control to display the Code window for the UserForm.In the Code window, type the following code for the CommandButton1 Click event: 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

On the Run menu, click Run Sub/UserForm.Select one or more items in the list.Click CommandButton1.After you click CommandButton1, every item that you selected in the ListBox control appears in a separate meage box.After all the selected items appear in a meage box, the UserForm is automatically closed.How to use the RowSource property to populate a ListBox control with cells on a worksheet To use the RowSource property to populate a ListBox control from a range of cells on a worksheet, follow these steps:

Start Excel, and then open a new blank workbook.In cells A1:A5 on Sheet1, type the values that you want to use to populate the ListBox control.On the Tools menu, point to Macro, and then click Visual Basic Editor.On the Insert menu, click UserForm to insert a UserForm in your workbook.Add a ListBox control to the UserForm.Add a CommandButton control to the UserForm.Double-click the CommandButton control to display the Code window for the UserForm.In the Code window, type the following code for the CommandButton1 Click event: Private Sub CommandButton1_Click()

ListBox1.RowSource = ”=Sheet1!A1:A5“ End Sub On the Run menu, click Run Sub/UserForm.NoteListBox1 does not contain any values.Click CommandButton1.ListBox1 is populated with the values in cells A1:A5 on Sheet1.How to populate a ListBox control with values in an array This example shows you how to populate a ListBox control with an array variable.You must aign the values from the array to the ListBox control one item at a time.Typically, this proce requires that you use a looping structure, such as a For…Next loop.To populate a ListBox control with an array variable, follow these steps:

Start Excel, and then open a new blank workbook.On the Tools menu, point to Macro, and then click Visual Basic Editor.On the Insert menu, click UserForm to insert a UserForm in your workbook.Add a ListBox control to the UserForm.On the Insert menu, click Module to insert a module sheet.In the Code window, type the following code: 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

On the Tools menu, click Macros, click PopulateListBox, and then click Run.The PopulateListBox procedure builds a simple array, and then adds the items in the array to the ListBox control by using the AddItem method.Then, the UserForm appears.How to use a horizontal range of cells on a worksheet to populate a ListBox control If you set the RowSource property of a ListBox control to a horizontal range of cells, only the first value appears in the ListBox control.To populate a ListBox control from a horizontal range of cells by using the AddItem method, follow these steps:

Start Excel, and then open a new blank workbook.In cells A1:E1 on Sheet1, type the values that you want to use to populate the ListBox control.On the Tools menu, point to Macro, and then click Visual Basic Editor.On the Insert menu, click UserForm to insert a UserForm in your workbook.Add a ListBox control to the UserForm.On the Insert menu, click Module to insert a module sheet.In the Code window, type the following code: Sub PopulateListWithHorizontalRange()

For Each x In Sheet1.Range(”A1:E1“)

UserForm1.ListBox1.AddItem x.Value

Next

UserForm1.Show

End Sub On the Tools menu, click Macros, click PopulateListWithHorizontalRange, and then click Run.The macro procedure loops through cells A1:E5 on Sheet1, adding the values to ListBox1 one at a time.NoteListBox1 is not bound to cells A1:E5 on Sheet1.How to return multiple values from a ListBox control that is bound to multiple columns of data You can format ListBox controls to display more than one column of data.This means that the ListBox control displays more than one item on each line of the list.To return multiple values from the selected item in the list, follow these steps: Start Excel, and then open a new blank workbook.Type the following data in the cells that are indicated on Sheet1: Collapse this tableExpand this table A1: Year B1: Region C1: Sales A2: 1996 B2: North C2: 140 A3: 1996 B3: South C3: 210 A4: 1997 B4: North C4: 190 A5: 1997 B5: South C5: 195 On the Tools menu, point to Macro, and then click Visual Basic Editor.On the Insert menu, click UserForm to insert a UserForm in your workbook.Add a Label control to the UserForm.Add a ListBox control to the UserForm.Right-click the ListBox, and then click Properties.Type or select the values that are indicated for the following properties of the ListBox control as listed in the following table:

Property

Value

----------------------------

BoundColumn

ColumnCount

ColumnHeads

True

RowSource

Sheet1!A2:A5 Double-click the ListBox control to display the Code window for the ListBox control.In the Code window, type the following code: 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 On the Run menu, click Run Sub/UserForm.When you click an entry in the ListBox control, the label changes to display all three of the items in that entry.How to remove all the items from a ListBox control that is bound to a worksheet To remove all the items from a ListBox control that is bound to a worksheet, clear the value that is stored in the RowSource property.To remove items from a ListBox control that is bound to a worksheet, follow these steps:

Start Excel, and then open a new blank workbook.In cells A1:A5 on Sheet1, type the values that you want to use to populate the ListBox control.On the Tools menu, point to Macro, and then click Visual Basic Editor.On the Insert menu, click UserForm to insert a UserForm in your workbook.Add a ListBox control to the UserForm.Right-click the ListBox control, and then click Properties.In the RowSource property, type Sheet1!A1:A5.Add a CommandButton control to the UserForm.Double-click the CommandButton control to display the Code window for the CommandButton control.In the Code window, type the following code for the CommandButton1 Click event: Private Sub CommandButton1_Click()

ListBox1.RowSource = ”“

End Sub On the Run menu, click Run Sub/UserForm.The ListBoxcontrol that you added to the UserForm is populated with the values that you entered on Sheet1.Click CommandButton1.All the items are removed from ListBox1.How to remove all the items from a ListBox control that is not bound to a worksheet There is no single VBA command that removes all the items from a ListBox control if the list is not bound to a worksheet.To remove all the items from a ListBox control that is populated from a Visual Basic array, follow these steps:

Start Excel, and then open a new blank workbook.On the Tools menu, point to Macro, and then click Visual Basic Editor.On the Insert menu, click UserForm to insert a UserForm in your workbook.Add a ListBox control to the UserForm.On the Insert menu, click Module to insert a module sheet.In the Code window, type the following code: 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

Add a CommandButton control to the UserForm.Double-click the CommandButton control to display the Code window for the CommandButton control.In the Code window, type the following code for the CommandButton1 Click event: Private Sub CommandButton1_Click()

For i = 1 To ListBox1.ListCount

ListBox1.RemoveItem 0

Next I

End Sub On the Tools menu, click Macros, click PopulateListBox, and then click Run.The ListBox control is populated, and then the UserForm appears.Click CommandButton1.All the items are removed from ListBox1.For additional information about the ListBox control, click the following article numbers to view the articles in the Microsoft Knowledge Base:

161598(http://support.microsoft.com/kb/161598/)OFF: How to add data to a ComboBox or ListBox in Excel or Word

211446(http://support.microsoft.com/kb/211446/)XL2000: TextColumn property displays only the first column 211896(http://support.microsoft.com/kb/211896/)XL2000: How to simulate combination List-Edit control for UserForms

211899(http://support.microsoft.com/kb/211899/)XL2000: Problems setting column headings in ListBox control

213721(http://support.microsoft.com/kb/213721/)XL2000: How to remove all items from a ListBox or ComboBox

213722(http://support.microsoft.com/kb/213722/)XL2000: How to use the TextColumn property

213723(http://support.microsoft.com/kb/213723/)XL2000: How to return values from a List box that displays multiple columns

213746(http://support.microsoft.com/kb/213746/)XL2000: How to fill List Box control with multiple ranges

213748(http://support.microsoft.com/kb/213748/)XL2000: How to populate one List Box based on another List Box

213752(http://support.microsoft.com/kb/213752/)XL2000: Using the AddItem method causes an error when RowSource is data bound

213756(http://support.microsoft.com/kb/213756/)XL2000: Using the RemoveItem method with ListBox or ComboBox control

213759(http://support.microsoft.com/kb/213759/)XL2000: How to determine which items are selected in a List Box Back to the top ComboBox controls You can use the ComboBox control as a drop-down list box, or as a combo box where you can select a value in a list or type a new value.The Style property determines if the ComboBox control acts as a drop-down list box or a combo box.Note All the examples in the previous section for the ListBox control can also be applied to the ComboBox control, except for the ”How to obtain the selected items in a multiple select ListBox control“ example.How to add a new item to the list if the ComboBox control is not bound to a worksheet When you type a value that is not already in the list in the ComboBox control, you may want to add the new value to the list.To add the new value that you typed in the ComboBox control if the ComboBox control is not bound to the worksheet, follow these steps: Start Excel, and then open a new blank workbook.On the Tools menu, point to Macro, and then click Visual Basic Editor.On the Insert menu, click UserForm to insert a UserForm in your workbook.Add a ComboBox control to the UserForm.On the Insert menu, click Module to insert a module sheet.In the Code window, type the following code: 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 Add a CommandButton control to the UserForm.Double-click the CommandButton control to display the Code window for the CommandButton control.In the Code window, type the following code for the CommandButton1 Click event: 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 On the Tools menu, click Macros, click PopulateListBox, and then click Run.The ComboBox control is populated, and then the UserForm appears.In the ComboBox control, type Mangoes(or any value that is not already in the list).Click CommandButton1.The new value that you typed now appears at the end of the list.How to add a new item to the list if the ComboBox control is bound to a worksheet When a user types a value that is not already in the list in the ComboBox control, you may want to add the new value to the list.To add the new value that you typed in the ComboBox control to the list, follow these steps:

Start Excel, and then open a new blank workbook.In cells A1:A5 on Sheet1, type the values that you want to use to populate the ComboBox control.Select cells A1:A5 on Sheet1.On the Insert menu, point to Name, and then click Define.In the Names in workbook box, type ListRange , and then click OK.This creates the defined name ListRange.You can use the defined name ListRange to bind the RowSource property of the ComboBox control to the worksheet.On the Tools menu, point to Macro, and then click Visual Basic Editor.On the Insert menu, click UserForm to insert a UserForm in your workbook.Add a ComboBox control to the UserForm.In the Properties for ComboBox1, type Sheet1!ListRange as the RowSource property.Add a CommandButton control to the UserForm.Double-click the CommandButton control to display the Code window for the CommandButton control.In the Code window, type the following code for the CommandButton1 Click event: 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“).Addre(external:=True)

End If

End Sub On the Run menu, click Run Sub/UserForm.The UserForm appears on Sheet1.In the ComboBox control, type a value that is not already in the list.Click CommandButton1.The new item that you typed in the ComboBox control is added to the list, and the list that the ComboBox control is bound to is expanded to include cells A1:A6.How to display the list of a ComboBox control when the UserForm appears Sometimes, it may be useful to display the list of a ComboBox control when a UserForm first appears.The following example uses the Activate event of the UserForm.To display the list of a ComboBox control, follow these steps:

Start Excel, and then open a new blank workbook.In cells A1:A5 on Sheet1, type the values that you want to use to populate the ComboBox control.On the Tools menu, point to Macro, and then click Visual Basic Editor.On the Insert menu, click UserForm to insert a UserForm in your workbook.Add a ComboBox control to the UserForm.In the Properties for ComboBox1, type Sheet1!A1:A5 as the RowSource property.Double-click the UserForm to display the Code window for the UserForm.In the Code window, type the following code for the CommandButton Click event: Private Sub UserForm_Activate()

ComboBox1.DropDown

End Sub On the Run menu, click Run Sub/UserForm.The UserForm appears on Sheet1, and you can see the list for ComboBox1.How to display the list of one ComboBox control when you make a selection in another ComboBox control To automatically display the list of one ComboBox control when a choice is made in another ComboBox control, follow these steps:

Start Excel, and then open a new blank workbook.In cells A1:A10 on Sheet1, type the values that you want to use to populate the ComboBox control.On the Tools menu, point to Macro, and then click Visual Basic Editor.On the Insert menu, click Module.In the Code window for the module, type the following code: Sub DropDown_ComboBox()

UserForm1.ComboBox2.DropDown

End Sub On the Insert menu, click UserForm to insert a UserForm in your workbook.Add a ComboBox control to the UserForm.In the Properties for ComboBox1, type Sheet1!A1:A5 as the RowSource property.Double-click the ComboBox control to open the Code window for the ComboBox control.In the Code window for the ComboBox control, type the following code for the ComboBox Click event:

Private Sub ComboBox1_Click()

Application.OnTime Now, ”DropDown_ComboBox"

End Sub Add a second ComboBox control to the UserForm.In the Properties for ComboBox2, type Sheet1!A6:A10 as the RowSource property.On the Run menu, click Run Sub/UserForm.When you click an item in the ComboBox1 list , the list for ComboBox2 automatically appears.For additional information about the ComboBox control, click the following article numbers to

《Excel使用VBA窗体的教程.docx》
将本文的Word文档下载,方便收藏和打印
推荐度:
Excel使用VBA窗体的教程
点击下载文档
相关专题 vba使用窗体操作表格 窗体 教程 EXCEL vba使用窗体操作表格 窗体 教程 EXCEL
[其他范文]相关推荐
    [其他范文]热门文章
      下载全文