EXCEL 数据有效性下拉框,如何多选?谢谢,万分感激

2025-03-13 03:29:45
推荐回答(5个)
回答1:

Private Sub Worksheet_Change(ByVal Target As Range)

Dim rngDV As Range, oldVal As String, newVal As String
If Target.CountLarge > 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 = 5 Or Target.Column = 7 Or Target.Column = 9 Or Target.Column = 11 Then  '这里规定好哪一列的数据有效性是多选的,A列是第1列,依次类推,如3就是C列,7就是G列
        If oldVal = "" Then
        'do nothing
        Else
            If newVal = "" Then
            'do nothing
            Else
            If newVal = "全选" Then             '当新选择的值是全选时,目标值为全选
                Target.Value = "全选"
            Else
                If InStr(1, oldVal, "全选") <> 0 Then           '当目标值中有全选时,选择新的非全选值时为新的值
                Target.Value = newVal
                Else
                If InStr(1, oldVal, newVal) <> 0 Then  '重复选择视同删除
                    If InStr(1, oldVal, newVal) + Len(newVal) - 1 = Len(oldVal) Then '最后一个选项重复
                        Target.Value = Left(oldVal, Len(oldVal) - Len(newVal) - 1)
                    Else
                        Target.Value = Replace(oldVal, newVal & ",", "") '不是最后一个选项重复的时候处理逗号
                    End If
                Else '不是重复选项就视同增加选项
                    Target.Value = oldVal & "," & newVal
                End If
                End If
            End If
            End If
        End If
    End If
End If

exitHandler:
Application.EnableEvents = True

End Sub

使用VBA代码是可以实现复选的功能!

回答2:

多选? 做不了吧?

你可以将各种组合都写好,作为单个选项加入进去
例如:

北京
天津
上海
北京、天津
北京、上海、广州
……

回答3:

excel的有效性选项都是只能选一组的,不能复选,如果想用复选就必须把所有的组合都事先写好,然后放在有效性序列里供选择,且复选项中间不能用逗号分隔。没有简便方法

回答4:

数据有效性做不了多选,多选的话你的用vba做个多选框 ,确实需要的话你可以到excelhome去问问,看有没人帮你做,记得传个例子上去

回答5:

除非你将各种情况都作为序列的一种情况输入一遍,即如:
北京;天津;上海;广州;北京 天津;北京 上海;北京 广州;天津 上海;天津 广州;上海 广州;等等。