【VBA案例010】下拉多选
大家好!今天将为大家介绍在Excel中如何实现下拉多选功能,让数据输入更加灵活高效。
下拉多选功能不仅提高了数据输入的灵活性,还减少了输入错误的可能性,为我们的数据处理工作带来了更高的效率。
也许你见过使用表单控件的方式实现下拉多选,但是通过数据验证和VBA,我们同样能够轻松创建具有下拉多选功能的工作表。
以下是VBA代码,详细解析请看文末视频。
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rngDV As Range
Dim oldVal As String
Dim newVal As String
Dim fenGeFu As String
If Target.CountLarge > 1 Then Exit Sub
fenGeFu = "," '规定用逗号分隔
On Error Resume Next
Set rngDV = Cells.SpecialCells(xlCellTypeAllValidation)
If rngDV Is Nothing Then Exit Sub
If Intersect(Target, rngDV) Is Nothing Then Exit Sub
Application.EnableEvents = False
newVal = Target.Value
Application.Undo
oldVal = Target.Value
Target.Value = newVal
If oldVal <> "" Then
If newVal <> "" Then
If InStr(fenGeFu & oldVal & fenGeFu, fenGeFu & newVal & fenGeFu) > 0 Then
If InStrRev(oldVal, newVal) + Len(newVal) - 1 = Len(oldVal) Then
Target.Value = Replace(oldVal, fenGeFu & newVal, "")
Else
Target.Value = Replace(oldVal, newVal & fenGeFu, "")
End If
Else
Target.Value = oldVal & fenGeFu & newVal
End If
End If
End If
Application.EnableEvents = True
End Sub