
地 址:上海市宝山66号
电 话:19979769347
网址:dsesh.com
在VBA中,列表我(wo)们可以使用ListBox对象来创建一个列表(biao)框,框添要向列表框添加内容,加内我们需要先(xian)创建一个ListBox对象,然(ran)后使用Add方法将内容添加到列表框中,以(yi)下是一个简单的示例:
1、打开Excel,按下Alt+F11键,打(da)开VBA编辑器。


2、在VBA编辑器中,点击菜单栏的插入,然(ran)后选择模块,在模块中输入以下代码:

Sub AddListItems() ' 定义变量 Dim objListBox As Object Dim strItem As String ' 创建(jian)一个新的列表框 Set objListBox = ActiveSheet.ListObjects.Add(xlSrcRange, Range("A1:B3"), , xlYes).ListObject ' 向列表框添加内容 strItem = "苹果" objListBox.DataBodyRange.Cells(1, 1).Value = strItem strItem = "香蕉" objListBox.DataBodyRange.Cells(2, 1).Value = strItem strItem = "橙子" objListBox.DataBodyRange.Cells(3, 1).Value = strItemEnd Sub3、按下Ctrl+S键保存代码。
4、回到Excel工作表,按下Alt+F8键,选择AddListItems,然后点击运行。
5、此时,你会看到一个新的(de)列表框出现在工作表(biao)中,包含了我(wo)们刚刚添加的(de)内容。
要从列表(biao)框中(zhong)删除某个项目,我们可以使用Remove方法,以下是一个示例:
1、在VBA编辑器中,输入以下代码:
Sub RemoveListItem() ' 定义变量 Dim objListBox As Object Dim strItem As String ' 获取当前活动的工作表(biao)和列表框对象 Set objListBox = ActiveSheet.ListObjects("ListBox1") ' 从列表框中删除指定的项目 strItem = "香蕉" If InStr(objListBox.DataBodyRange.Cells(2, 1).Value, strItem) > 0 Then objListBox.DataBodyRange.Cells(2, 1).EntireRow.Delete objListBox.DataBodyRange.Sort Key1:=objListBox.DataBodyRange, Order1:=xlAscending, _ Key2:=objListBox.ListColumns("列名").DataField, Order2:=xlAscending, _ Header:=xlNo ElseIf InStr(objListBox.DataBodyRange.Cells(3, 1).Value, strItem) > 0 Then objListBox.DataBodyRange.Cells(3, 1).EntireRow.Delete objListBox.DataBodyRange.Sort Key1:=objListBox.DataBodyRange, Order1:=xlAscending, _ Key2:=objListBox.ListColumns("列名").DataField, Order2:=xlAscending, _ Header:=xlNo ElseIf InStr(objListBox.DataBodyRange.Cells(4, 1).Value, strItem) > 0 Then objListBox.DataBodyRange.Cells(4, 1).EntireRow.Delete objListBox.DataBodyRange.Sort Key1:=objListBox.DataBodyRange, Order1:=xlAscending, _ Key2:=objListBox.ListColumns("列名").DataField, Order2:=xlAscending, _ Header:=xlNo ElseIf InStr(objListBox.DataBodyRange.Cells(5, 1).Value, strItem) > 0 Then objListBox.DataBodyRange.Cells(5, 1).EntireRow.Delete objListBox.DataBodyRange.Sort Key1:=objListBox.DataBodyRange, Order1:=xlAscending, _ Key2:=objListBox.ListColumns("列名").DataField, Order2:=xlAscending, _ Header:=xlNo ElseIf InStr(objListBox.DataBodyRange.Cells(6, 1).Value, strItem) > 0 Then objListBox.DataBodyRange.Cells(6, 1).EntireRow.Delete objListBox.DataBodyRange.Sort Key1:=objListBox.DataBodyRange, Order1:=xlAscending, _ Key2:=objListBox.ListColumns("列名").DataField, Order2:=xlAscending, _ Header:=xlNo ElseIf InStr(objListBox.DataBodyRange.Cells(7, 1).Value, strItem) > 0 Then objListBox.DataBodyRange.Cells(7, 1).EntireRow.Delete objListBox.DataBodyRange.Sort Key1:=objListBox.DataBodyRange, Order1:=xlAscending, _ Key2:=objListBox.ListColumns("列名").DataField, Order2:=xlAscending, _ Header:=xlNo ElseIf InStr(objListBox.DataBodyRange.Cells(8, 1).Value, strItem) > 0 Then objListBox.DataBodyRange.Cells(8, 1).EntireRow.Delete objListBox.DataBodyRange.Sort Key1:=objListBox.DataBodyRange, Order1:=xlAscending, _ Key2:=objListBox.ListColumns("列(lie)名").DataField, Order2:=xlAscending, _ Header:=xlNo ElseIf InStr(objListBox.DataBodyRange.Cells(9, 1).Value, strItem) > 0 Then objListBox.DataBodyRange.Cells(9, 1).EntireRow.Delete objListBox.DataBodyRange.Sort Key1:=objListBox.DataBodyRange, Order1:=xlAscending, _ Key2:=objListBox.ListColumns("列(lie)名(ming)").DataField, Order2:=xlAscending, _ Header:=xlNo ElseIf InStr(objListBox.DataBodyRange.Cells(10, 1).Value, strItem) > 0 Then objListBox.DataBodyRange.Cells(10, 1).EntireRow.Delete objListBox.DataBodyRange.Sort Key1:=objListBox.DataBodyRange, Order1:=xlAscending, _ Key2:=objListBox.ListColumns("列名").DataField, Order2:=xlAscending, _ Header:=xlNo ElseIf InStr(objListBox.DataBodyRange.Cells(11, 1).Value, strItem) > 0 Then objListBox.DataBodyRange.Cells(11, 1).EntireRow.Delete objListBox.DataBodyRange.Sort Key1:=objListBox