Excel实现具有多选功能的下拉框(启用宏)

4,015 阅读1分钟
  1. 创建单选下拉框 打开Excel,点击 数据-数据验证-序列 ,并在文本框输入可选项(用英文逗号分隔)

image.png

  1. 右击表格名,选择 查看代码

image.png

  1. 输入代码,保存时选 ,便可实现多选

image.png

image.png

image.png

代码如下:

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
'do nothing
Else
Application.EnableEvents = False
newVal = Target.Value
Application.Undo
oldVal = Target.Value
Target.Value = newVal
If oldVal = "" Then
Else
If newVal = "" Then
Else
Target.Value = oldVal _
& ", " & newVal
End If
End If
End If

exitHandler:
Application.EnableEvents = True
End Sub

  1. 再次打开excel时,如果不能用了, 就点击 启用宏功能
  1. 注意:在删除单元格里内容的时候,不能删除部分,只能删除整个单元格(使用Delete键)