1、选择 “开发工具”,点击下拉列表框插件。
2、在表格页面画一个大小合适的列表框。
3、右击列表框,选择”查看代码“,此时会弹出表格组件编辑器。
4、在右边代码框增加如下代码。
需要注意这里我只有一个ListBox1 ,用的默认名称,如果有多个需新增多个方法,同时属性名字要改成设计时组件的名称。如ListBox1,ListBox2
Private Sub ListBox1_Click()
If Reload Then Exit Sub '加载ListBox1
For i = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(i) = True Then t = t & "," & ListBox1.List(i)
Next
ActiveCell = Mid(t, 2)
End Sub
Private Sub ListBox1_Change()
If Reload Then Exit Sub '加载ListBox2
For i = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(i) = True Then t = t & "," & ListBox1.List(i)
Next
ActiveCell = Mid(t, 2)
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
With ListBox1
'第 2 列 且 单元格大于 1,因为表头的字段不需要进行多选
If ActiveCell.Column = 2 And ActiveCell.Row > 1 Then
t = ActiveCell.Value
Reload = True '如果是根据单元格的值修改列表框,则暂时屏蔽listbox的change事件。
For i = 0 To .ListCount - 1 '根据活动单元格内容修改列表框中被选中的内容
If InStr(t, .List(i)) Then
.Selected(i) = True
Else
.Selected(i) = False
End If
Next
Reload = False
.Top = ActiveCell.Top + ActiveCell.Height '以下语句根据活动单元格位置显示列表框
.Left = ActiveCell.Left
.Width = ActiveCell.Width
.Visible = True
Else
.Visible = False
End If
End With
End Sub
5、 点击 "调试" -> "编译"
6、再切换到表格页面,点击"退出设计"。
7、测试,可正常多选。