VBA制作:下拉多选框,逗号隔开

1.重命名sheet1为主表,sheet2位复选,在复选表中添加要在下拉菜单中呈现的数据。

2.插入listbox控件,设置属性

点击excel菜单“开发工具”,点击插入,选择“列表框”,在excel中画一个列表框控件,鼠标右键此控件,选择属性,在打开的属性对话框里设置,操作如下:

ListStyle设置为:1-fmListStyleOption

Multiselect设置为:1-fmMultiSelectMulti

名称属性设置为:ListBox1


3.点击主表,再点击Excel菜单“开发工具”,选择“查看代码”,复制下面的代码

Private Sub ListBox1_Change()

Dim TXT As String

TXT = ""

For i = 0 To ListBox1.ListCount - 1

    If ListBox1.Selected(i) = True Then

        TXT = IIf(TXT = "", ListBox1.List(i), TXT & "," & ListBox1.List(i))

    End If

Next

ActiveCell.Value = TXT

End Sub

Private Sub CheckBox1_Click()

End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Dim DSoucre As String

If Not (Target.Column = 1 And Target.Row > 1) Then '控制行、列中单元格应用位置

    ListBox1.Visible = False

    Exit Sub

End If

DSoucre = "复选!a2:a6" '复选内容

With ListBox1

    .ListFillRange = DSoucre

    .ListStyle = fmListStyleOption

    .MultiSelect = fmMultiSelectMulti

    .Top = Target.Top + Target.Height

    .Left = Target.Left

    .Width = Target.Width

    ListBox1.Visible = True

End With

End Sub

你可能感兴趣的:(VBA制作:下拉多选框,逗号隔开)