原创

excel下拉菜单可多选

1. 新建启用宏的工作表,后缀为:xlsm

2. 鼠标选择需要多选择的单元格(如第6列第二行),菜单-> 数据 -> 数据验证 ->

2.1 验证条件:序列

2.2 来源: 输入数据(手动输入或通过原有数据列)

3. 选择菜单-> 开发工具 -> Visual Basic -> 对象 -> sheet1 -> 右键 -> 查看代码,复制以下代码:

注意内容中:   【 If Target.Column = 6 Then 】其中的6,代表需多选的第6列,按实际情况修改此数字即可!

4. 多选时,多次选择即可!选择错了,删除重新选择即可!

******

Private Sub Worksheet_Change(ByVal Target As Range)
' Developed by Jimmy Zhang
' www.jimmy2k.top
Dim rngDV As Range
Dim oldVal As String
Dim newVal As String
If Target.Count > 1 Then GoTo exitHandler

On Error Resume Next
Set rngDV = Cells.SpecialCells(xlCellTypeAllValidation)
On Error GoTo exitHandler

If rngDV Is Nothing Then GoTo exitHandler

If Intersect(Target, rngDV) Is Nothing Then
'do nothing
Else
Application.EnableEvents = False
newVal = Target.Value
Application.Undo
oldVal = Target.Value
Target.Value = newVal
If Target.Column = 6 Then
If oldVal = "" Then
'do nothing
Else
If newVal = "" Then
'do nothing
Else
Target.Value = oldVal _
& ", " & newVal
'      NOTE: you can use a line break,
'      instead of a comma
'      Target.Value = oldVal _
'        & Chr(10) & newVal
End If
End If
End If
End If

exitHandler:
Application.EnableEvents = True
End Sub

******


    效果图 :

正文到此结束
本文目录