原文地址:VBA控件常规使用--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 中删除所有项目。 |
|
来自: peng2009178 > 《peng》