如何在Excel中創建具有多個選擇或值的下拉列表?
默認情況下,您只能在 Excel 的數據驗證下拉列表中選擇一項。 如何在下拉列表中進行多項選擇,如下 gif 所示? 本文中的方法可以幫助您解決問題。
使用VBA代碼創建具有多個選擇的下拉列表
本節提供了兩個 VBA 代碼來幫助您在工作表的下拉列表中進行多項選擇。 這兩個VBA代碼可以實現:
您可以應用以下 VBA 代碼之一在 Excel 工作表的下拉列表中進行多項選擇。 請執行以下操作。
1. 打開包含要從中進行多項選擇的數據驗證下拉列表的工作表。 右鍵單擊工作表選項卡並選擇 查看代碼 從上下文菜單。
2。 在裡面 Microsoft Visual Basic for Applications 窗口,將下面的VBA代碼複製到代碼窗口中。 看截圖:
Private Sub Worksheet_Change(ByVal Target As Range)
'Updated by Extendoffice 2019/11/13
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
3。 按 其他 + Q 關閉鍵 Microsoft Visual Basic for Applications 窗口。
現在您可以從當前工作表的下拉列表中選擇多個項目。
假設您不小心選擇了一個項目並且需要在不清除整個單元格並重新開始的情況下將其刪除。 下面的 VBA 代碼 2 可以幫你一個忙。
VBA代碼2:允許在下拉列表中進行多項選擇而沒有重複(通過再次選擇刪除現有項目)
Private Sub Worksheet_Change(ByVal Target As Range)
'Updated by Extendoffice 2023/01/11
'Updated by Ken Gardner 2022/07/11
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的Kutools 為了你。 使用此功能,您可以根據需要從下拉列表中輕鬆選擇指定範圍,當前工作表,當前工作簿或所有打開的工作簿中的多個項目。
申請前 Excel的Kutools請 首先下載並安裝.
1。 點擊 庫工具 > 下拉列表 > 多選下拉列表 > 設置。 見截圖:
2。 在裡面 多選下拉列表設置 對話框,請進行以下配置。
- 2.1)在 適用於 部分。 在這種情況下,我選擇 當前工作表 來自 指定範圍 下拉列表;
- 2.2)在 文本方向 部分,根據需要選擇文本方向;
- 2.3)在 分離器 框,輸入一個定界符,將用於分隔多個值;
- 2.4)檢查 不要添加重複項 盒子裡 選項 如果您不想在下拉列表單元格中重複,請選擇“部分”。
- 2.5)點擊 OK 按鈕。 看截圖:
3.請點擊 庫工具 > 下拉列表 > 多選下拉列表 啟用該功能。
現在,您可以從當前工作表的下拉列表中或在步驟2中指定的任何範圍中選擇多個項目。
如果您想免費試用(30-day) 這個實用程序, 請點擊下載,然後按照上述步驟進行操作。
相關文章:
在Excel下拉列表中鍵入時自動完成
如果您有一個包含大值的數據驗證下拉列表,則需要在列表中向下滾動以查找合適的列表,或直接在列錶框中鍵入整個單詞。 如果在下拉列表中鍵入第一個字母時有允許自動完成的方法,一切將變得更加容易。 本教程提供了解決問題的方法。
在Excel中從另一個工作簿創建下拉列表
在工作簿中的工作表之間創建數據驗證下拉列表非常容易。 但是,如果數據驗證所需的列表數據位於另一個工作簿中,您將怎麼辦? 在本教程中,您將詳細了解如何從Excel中的另一個工作簿創建拖放列表。
在Excel中創建可搜索的下拉列表
對於具有眾多價值的下拉列表,找到合適的價值並非易事。 以前,我們已經介紹了一種在下拉框中輸入第一個字母時自動完成下拉列表的方法。 除了自動完成功能之外,您還可以使下拉列表可搜索,以提高在下拉列表中查找適當值時的工作效率。 為了使下拉列表可搜索,請嘗試本教程中的方法。
在Excel下拉列表中選擇值時自動填充其他單元格
假設您已經根據單元格區域B8:B14中的值創建了一個下拉列表。 在下拉列表中選擇任何值時,都希望在選定單元格中自動填充單元格範圍C8:C14中的相應值。 為了解決該問題,本教程中的方法將對您有所幫助。
最佳辦公效率工具
Kutools for Excel 解決了你的大部分問題,並將你的生產力提高了 80%
- 重用: 快速插入 複雜的公式,圖表 以及您以前使用過的任何東西; 加密單元 帶密碼 創建郵件列表 並發送電子郵件...
- 超級公式欄 (輕鬆編輯多行文本和公式); 閱讀版式 (輕鬆讀取和編輯大量單元格); 粘貼到過濾範圍...
- 合併單元格/行/列 不會丟失數據; 拆分單元格內容; 合併重複的行/列...防止細胞重複; 比較範圍...
- 選擇重複或唯一 行; 選擇空白行 (所有單元格都是空的); 超級查找和模糊查找 在許多工作簿中; 隨機選擇...
- 確切的副本 多個單元格,無需更改公式參考; 自動創建參考 到多張紙; 插入項目符號,複選框等...
- 提取文字,添加文本,按位置刪除, 刪除空間; 創建和打印分頁小計; 在單元格內容和註釋之間轉換...
- 超級濾鏡 (將過濾方案保存並應用於其他工作表); 高級排序 按月/週/日,頻率及更多; 特殊過濾器 用粗體,斜體...
- 結合工作簿和工作表; 根據關鍵列合併表; 將數據分割成多個工作表; 批量轉換xls,xlsx和PDF...
- 超過 300 項強大的功能. 支持 Office / Excel 2007-2021 和 365。支持所有語言。 在您的企業或組織中輕鬆部署。 完整功能 30 天免費試用。 60 天退款保證。

Office選項卡為Office帶來了選項卡式界面,使您的工作更加輕鬆
- 在Word,Excel,PowerPoint中啟用選項卡式編輯和閱讀,發布者,Access,Visio和Project。
- 在同一窗口的新選項卡中而不是在新窗口中打開並創建多個文檔。
- 將您的工作效率提高 50%,每天為您減少數百次鼠標點擊!
































