如何在Excel中创建具有多选或多值的下拉列表?
Excel是一个功能强大的电子表格工具,被个人、企业和组织广泛使用。Excel最实用的功能之一是创建下拉列表,它可以大大简化数据输入,并确保不同单元格或列之间的一致性。在本教程中,我们将重点介绍如何在Excel中创建具有多选或多值的下拉列表。当您希望允许用户从列表中选择多个选项时,此功能特别有用。我们将逐步引导您完成创建此类下拉列表的过程,您将学习如何根据您的特定需求对其进行自定义。在本教程结束时,您将更好地理解如何使用Excel的下拉列表功能,并能够将其应用于您自己的电子表格。
创建具有多选或多值的下拉列表
在这里,我们只需将VAB代码插入到工作表中即可完成任务。因此,让我们来看一个简单的过程,了解如何在Excel中创建具有多选或多值的下拉列表。
步骤1
考虑任何您拥有数据验证列表的Excel工作表。首先,右键单击工作表名称并选择“查看代码”以打开VBA应用程序。然后将下面提到的代码复制到文本框中,如下所示。
右键点击 > 查看代码 > 复制代码。
代码
Private Sub Worksheet_Change(ByVal Target As Range) Dim xRng As Range Dim xValue1 As String Dim xValue2 As String If Target.Count > 1 Then Exit Sub On Error Resume Next Set xRng = Cells.SpecialCells(xlCellTypeAllValidation) If xRng Is Nothing Then Exit Sub Application.EnableEvents = False If Not Application.Intersect(Target, xRng) Is Nothing Then xValue2 = Target.Value Application.Undo xValue1 = Target.Value Target.Value = xValue2 If xValue1 <> "" Then If xValue2 <> "" Then If xValue1 = xValue2 Or _ InStr(1, xValue1, ", " & xValue2) Or _ InStr(1, xValue1, xValue2 & ",") Then Target.Value = xValue1 Else Target.Value = xValue1 & ", " & xValue2 End If End If End If End If Application.EnableEvents = True End Sub
步骤2
从现在开始,我们可以为数据验证列表选择多个值。
注意 -
使用以下代码允许在下拉列表中进行多选,而不会创建重复项(您可以通过再次选择项目来删除它)。
代码Private Sub Worksheet_Change(ByVal Target As Range)
Dim xRng As Range
Dim xValue1 As String
Dim xValue2 As String
Dim semiColonCnt As Integer
Dim xType As Integer
If Target.Count > 1 Then Exit Sub
On Error Resume Next
xType = 0
xType = Target.Validation.Type
If xType = 3 Then
Application.ScreenUpdating = False
Application.EnableEvents = False
xValue2 = Target.Value
Application.Undo
xValue1 = Target.Value
Target.Value = xValue2
If xValue1 <> "" Then
If xValue2 <> "" Then
If xValue1 = xValue2 Or xValue1 = xValue2 & ";" Or xValue1 = xValue2 & "; " Then ' leave the value if only one in list
xValue1 = Replace(xValue1, "; ", "")
xValue1 = Replace(xValue1, ";", "")
Target.Value = xValue1
ElseIf InStr(1, xValue1, "; " & xValue2) Then
xValue1 = Replace(xValue1, xValue2, "") ' removes existing value from the list on repeat selection
Target.Value = xValue1
ElseIf InStr(1, xValue1, xValue2 & ";") Then
xValue1 = Replace(xValue1, xValue2, "")
Target.Value = xValue1
Else
Target.Value = xValue1 & "; " & xValue2
End If
Target.Value = Replace(Target.Value, ";;", ";")
Target.Value = Replace(Target.Value, "; ;", ";")
If Target.Value <> "" Then
If Right(Target.Value, 2) = "; " Then
Target.Value = Left(Target.Value, Len(Target.Value) - 2)
End If
End If
If InStr(1, Target.Value, "; ") = 1 Then ' check for ; as first character and remove it
Target.Value = Replace(Target.Value, "; ", "", 1, 1)
End If
If InStr(1, Target.Value, ";") = 1 Then
Target.Value = Replace(Target.Value, ";", "", 1, 1)
End If
semiColonCnt = 0
For i = 1 To Len(Target.Value)
If InStr(i, Target.Value, ";") Then
semiColonCnt = semiColonCnt + 1
End If
Next i
If semiColonCnt = 1 Then ' remove ; if last character
Target.Value = Replace(Target.Value, "; ", "")
Target.Value = Replace(Target.Value, ";", "")
End If
End If
End If
Application.EnableEvents = True
Application.ScreenUpdating = True
End If
End Sub
结论
在本教程中,我们使用了一个简单的示例来演示如何在Excel中创建具有多选或多值的下拉列表,以突出显示特定数据集。
广告