问题描述
Excel的下拉框无法多选
解决办法
为Excel文件添加VB代码,文件后缀需由.xlsx变为.xlsm,否则仍无法实现该功能
操作步骤
① 选中当前Sheet
② 右键选择查看代码
③ 将代码复制进去
④ 再将文件保存为.xlsm格式的即可
附代码如下
Option Explicit
Sub Worksheet_Change(ByVal Target As Range)
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
Else
Application.EnableEvents = False
newVal = Target.Value
Application.Undo
oldVal = Target.Value
Target.Value = newVal
If oldVal = "" Then
Else
If newVal = "" Then
Else
If InStr(oldVal, newVal) = 0 Then
Target.Value = oldVal _
& "," & newVal
Else
End If
End If
End If
End If
exitHandler:
Application.EnableEvents = True
End Sub