如何在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中建立具有多選或多值的下拉列表,以突出顯示特定資料集。

更新於: 2023年7月13日

828 次瀏覽

開啟您的 職業生涯

透過完成課程獲得認證

立即開始
廣告