在 Excel 下拉清單中選擇多個項目 - 完整指南
Excel 下拉清單是確保資料一致性和易於輸入的絕佳工具。但是,預設情況下,它們限制您只能選擇一項。但是,如果您需要從同一個下拉清單中選擇多個項目怎麼辦?本綜合指南將探討在 Excel 下拉清單中啟用多項選擇、管理重複項、設定自訂分隔符號以及定義這些清單的範圍的方法。
啟用下拉清單中的多項選擇
本節提供兩種方法來幫助您在 Excel 中的下拉清單中啟用多項選擇。
使用VBA程式碼
若要允許在下拉清單中進行多項選擇,您可以使用 Visual Basic應用程序 (VBA) 在 Excel 中。該腳本可以修改下拉清單的行為,使其成為多項選擇清單。請按以下步驟操作。
第 1 步:開啟工作表(程式碼)編輯器
- 開啟包含要為其啟用多項選擇的下拉清單的工作表。
- 右鍵單擊工作表標籤並選擇 查看代碼 從上下文菜單。
步驟2:使用VBA程式碼
現在複製以下 VBA 程式碼並將其貼上到開啟的工作表(程式碼)視窗中。
VBA 代碼:在 Excel 下拉清單中啟用多項選擇。
Private Sub Worksheet_Change(ByVal Target As Range)
'Updated by Extendoffice 20240118
Dim xRng As Range
Dim xValue1 As String
Dim xValue2 As String
Dim delimiter As String
Dim TargetRange As Range
Set TargetRange = Me.UsedRange ' Users can change target range here
delimiter = ", " ' Users can change the delimiter here
If Target.Count > 1 Or Intersect(Target, TargetRange) Is Nothing Then Exit Sub
On Error Resume Next
Set xRng = TargetRange.SpecialCells(xlCellTypeAllValidation)
If xRng Is Nothing Then Exit Sub
Application.EnableEvents = False
xValue2 = Target.Value
Application.Undo
xValue1 = Target.Value
Target.Value = xValue2
If xValue1 <> "" And xValue2 <> "" Then
If Not (xValue1 = xValue2 Or _
InStr(1, xValue1, delimiter & xValue2) > 0 Or _
InStr(1, xValue1, xValue2 & delimiter) > 0) Then
Target.Value = xValue1 & delimiter & xValue2
Else
Target.Value = xValue1
End If
End If
Application.EnableEvents = True
On Error GoTo 0
End Sub
結果
當您返回工作表時,下拉清單將允許您選擇多個選項,請參閱下面的示範:
上面的VBA程式碼:
- 適用於目前工作表中的所有資料驗證下拉列表,包括現有的和將來建立的。
- 防止您在每個下拉清單中多次選擇相同的項目。
- 使用逗號作為所選項目的分隔符號。若要使用其他分隔符,請 查看此部分以更改分隔符.
只需點擊幾下即可使用 Kutools for Excel
如果您對 VBA 不滿意,還有一個更簡單的選擇: Excel的Kutools's 多選下拉列表 特徵。這個用戶友好的工具簡化了在下拉清單中啟用多個選擇的過程,使您可以自訂分隔符號並輕鬆管理重複項,以滿足您的不同需求。
後 安裝 Kutools for Excel,轉到 庫工具 標籤,選擇 下拉列表 > 多選下拉列表。然後需要進行如下配置。
- 指定包含需要從中選擇多個項目的下拉清單的範圍。
- 指定下拉清單儲存格中所選項目的分隔符號。
- 點擊 OK 完成設置。
結果
現在,當您按一下指定範圍內帶有下拉清單的儲存格時,其旁邊會出現一個列錶框。只需點擊項目旁邊的“+”按鈕即可將其新增至下拉儲存格中,然後按一下「-」按鈕即可刪除不再需要的任何項目。請參閱下面的演示:
- Check the 插入分隔符號後自動換行 如果您想在儲存格內垂直顯示所選項目,請選擇此選項。如果您喜歡水平列表,請不要選取此選項。
- Check the 啟用搜索 如果您想將搜尋欄新增至下拉清單中,請選擇此選項。
- 要應用此功能,請 下載並安裝 Kutools for Excel 第一。
多選下拉清單的更多操作
本節收集在資料驗證下拉清單中啟用多個選擇時可能需要的不同場景。
允許下拉清單中出現重複的項目
當下拉清單中允許進行多項選擇時,重複可能會成為問題。上面的 VBA 程式碼不允許下拉清單中出現重複的項目。如果您需要保留重複的項目,請嘗試本節中的 VBA 程式碼。
VBA程式碼:允許資料驗證下拉清單中重複
Private Sub Worksheet_Change(ByVal Target As Range)
'Updated by Extendoffice 20240118
Dim xRng As Range
Dim xValue1 As String
Dim xValue2 As String
Dim delimiter As String
Dim TargetRange As Range
Set TargetRange = Me.UsedRange ' Users can change target range here
delimiter = ", " ' Users can change the delimiter here
If Target.Count > 1 Or Intersect(Target, TargetRange) Is Nothing Then Exit Sub
On Error Resume Next
Set xRng = TargetRange.SpecialCells(xlCellTypeAllValidation)
If xRng Is Nothing Then Exit Sub
Application.EnableEvents = False
xValue2 = Target.Value
Application.Undo
xValue1 = Target.Value
Target.Value = xValue2
If xValue1 <> "" And xValue2 <> "" Then
Target.Value = xValue1 & delimiter & xValue2
End If
Application.EnableEvents = True
On Error GoTo 0
End Sub
結果
現在,您可以從目前工作表的下拉清單中選擇多個項目。若要重複下拉清單儲存格中的某個項目,請繼續從清單中選取該項目。看截圖:
從下拉清單中刪除任何現有項目
從下拉清單中選擇多個項目後,有時可能需要從下拉清單儲存格中刪除現有項目。本節提供另一段 VBA 程式碼來幫助您完成此任務。
VBA程式碼:從下拉清單儲存格中刪除任何現有項目
Private Sub Worksheet_Change(ByVal Target As Range)
'Updated by Extendoffice 20240118
Dim xRngDV As Range
Dim TargetRange As Range
Dim oldValue As String
Dim newValue As String
Dim delimiter As String
Dim allValues As Variant
Dim valueExists As Boolean
Dim i As Long
Dim cleanedValue As String
Set TargetRange = Me.UsedRange ' Set your specific range here
delimiter = ", " ' Set your desired delimiter here
If Target.CountLarge > 1 Then Exit Sub
' Check if the change is within the specific range
If Intersect(Target, TargetRange) Is Nothing Then Exit Sub
On Error Resume Next
Set xRngDV = Target.SpecialCells(xlCellTypeAllValidation)
If xRngDV Is Nothing Or Target.Value = "" Then
' Skip if there's no data validation or if the cell is cleared
Application.EnableEvents = True
Exit Sub
End If
On Error GoTo 0
If Not Intersect(Target, xRngDV) Is Nothing Then
Application.EnableEvents = False
newValue = Target.Value
Application.Undo
oldValue = Target.Value
Target.Value = newValue
' Split the old value by delimiter and check if new value already exists
allValues = Split(oldValue, delimiter)
valueExists = False
For i = LBound(allValues) To UBound(allValues)
If Trim(allValues(i)) = newValue Then
valueExists = True
Exit For
End If
Next i
' Add or remove value based on its existence
If valueExists Then
' Remove the value
cleanedValue = ""
For i = LBound(allValues) To UBound(allValues)
If Trim(allValues(i)) <> newValue Then
If cleanedValue <> "" Then cleanedValue = cleanedValue & delimiter
cleanedValue = cleanedValue & Trim(allValues(i))
End If
Next i
Target.Value = cleanedValue
Else
' Add the value
If oldValue <> "" Then
Target.Value = oldValue & delimiter & newValue
Else
Target.Value = newValue
End If
End If
Application.EnableEvents = True
End If
End Sub
結果
此 VBA 程式碼可讓您從下拉清單中選擇多個項目,並輕鬆刪除已選取的任何項目。選擇多個項目後,如果您想刪除特定項目,只需從清單中再次選擇即可。
設定自訂分隔符
上述VBA程式碼中分隔符號設定為逗號。您可以將此變數修改為任何首選字符,以用作下拉清單選擇的分隔符。您可以這樣做:
可以看到,上面的VBA程式碼都有下面這行:
delimiter = ", "
您只需根據需要將逗號更改為任何分隔符號即可。例如,您想要用分號分隔項目,請將行改為:
delimiter = "; "
delimiter = vbNewLine
設定指定範圍
上述VBA程式碼適用於目前工作表中的所有下拉清單。如果您只想將VBA程式碼套用至特定範圍的下拉列表,則可以在上述VBA程式碼中指定範圍,如下所示。
可以看到,上面的VBA程式碼都有下面這行:
Set TargetRange = Me.UsedRange
您只需將該行更改為:
Set TargetRange = Me.Range("C2:C10")
在受保護的工作表中執行
假設您使用密碼“保護工作表”123”並將下拉清單單元格設定為“解鎖」 在啟動保護之前,從而確保多選功能在保護後保持活動狀態。但是,上述 VBA 程式碼在這種情況下無法運作,本節介紹另一個專門用於處理多選功能的 VBA 腳本在受保護的工作表中。
VBA程式碼:在下拉清單中啟用多重選擇而不重複
Private Sub Worksheet_Change(ByVal Target As Range)
'Updated by Extendoffice 20240118
Dim xRng As Range
Dim xValue1 As String
Dim xValue2 As String
Dim delimiter As String
Dim TargetRange As Range
Dim isProtected As Boolean
Dim pswd As Variant
Set TargetRange = Me.UsedRange ' Set your specific range here
delimiter = ", " ' Users can change the delimiter here
If Target.Count > 1 Or Intersect(Target, TargetRange) Is Nothing Then Exit Sub
' Check if sheet is protected
isProtected = Me.ProtectContents
If isProtected Then
' If protected, temporarily unprotect. Adjust or remove the password as needed.
pswd = "yourPassword" ' Change or remove this as needed
Me.Unprotect Password:=pswd
End If
On Error Resume Next
Set xRng = TargetRange.SpecialCells(xlCellTypeAllValidation)
If xRng Is Nothing Then
If isProtected Then Me.Protect Password:=pswd
Exit Sub
End If
Application.EnableEvents = False
xValue2 = Target.Value
Application.Undo
xValue1 = Target.Value
Target.Value = xValue2
If xValue1 <> "" And xValue2 <> "" Then
If Not (xValue1 = xValue2 Or _
InStr(1, xValue1, delimiter & xValue2) > 0 Or _
InStr(1, xValue1, xValue2 & delimiter) > 0) Then
Target.Value = xValue1 & delimiter & xValue2
Else
Target.Value = xValue1
End If
End If
Application.EnableEvents = True
On Error GoTo 0
' Re-protect the sheet if it was protected
If isProtected Then
Me.Protect Password:=pswd
End If
End Sub
透過在 Excel 下拉清單中啟用多項選擇,您可以大幅增強工作表的功能和靈活性。無論您熟悉 VBA 編碼還是喜歡 Kutools 等更簡單的解決方案,您現在都可以將標準下拉清單轉換為動態的多選工具。有了這些技能,您現在就可以建立更動態且使用者友好的 Excel 文件。對於那些渴望深入研究 Excel 功能的人,我們的網站擁有豐富的教學。 在這裡了解更多 Excel 提示和技巧.
相關文章
在Excel下拉列表中鍵入時自動完成
如果您有一個包含大值的數據驗證下拉列表,則需要在列表中向下滾動以查找合適的列表,或直接在列錶框中鍵入整個單詞。 如果在下拉列表中鍵入第一個字母時有允許自動完成的方法,一切將變得更加容易。 本教程提供了解決問題的方法。
在Excel中從另一個工作簿創建下拉列表
在工作簿中的工作表之間創建數據驗證下拉列表非常容易。 但是,如果數據驗證所需的列表數據位於另一個工作簿中,您將怎麼辦? 在本教程中,您將詳細了解如何從Excel中的另一個工作簿創建拖放列表。
在Excel中創建可搜索的下拉列表
對於具有眾多價值的下拉列表,找到合適的價值並非易事。 以前,我們已經介紹了一種在下拉框中輸入第一個字母時自動完成下拉列表的方法。 除了自動完成功能之外,您還可以使下拉列表可搜索,以提高在下拉列表中查找適當值時的工作效率。 為了使下拉列表可搜索,請嘗試本教程中的方法。
在Excel下拉列表中選擇值時自動填充其他單元格
假設您已經根據單元格區域B8:B14中的值創建了一個下拉列表。 在下拉列表中選擇任何值時,都希望在選定單元格中自動填充單元格範圍C8:C14中的相應值。 為了解決該問題,本教程中的方法將對您有所幫助。
最佳辦公生產力工具
🤖 | Kutools 人工智慧助手:基於以下內容徹底改變數據分析: 智慧執行 | 生成代碼 | 建立自訂公式 | 分析數據並產生圖表 | 呼叫 Kutools 函數... |
熱門特色: 尋找、突出顯示或識別重複項 | 刪除空白行 | 合併列或儲存格而不遺失數據 | 沒有公式的回合 ... | |
超級查詢: 多條件VLookup | 多值VLookup | 跨多個工作表的 VLookup | 模糊查詢 .... | |
高級下拉列表: 快速建立下拉列表 | 依賴下拉列表 | 多選下拉列表 .... | |
欄目經理: 新增特定數量的列 | 移動列 | 切換隱藏列的可見性狀態 | 比較範圍和列 ... | |
特色功能: 網格焦點 | 設計圖 | 大方程式酒吧 | 工作簿和工作表管理器 | 資源庫 (自動文字) | 日期選擇器 | 合併工作表 | 加密/解密單元格 | 按清單發送電子郵件 | 超級濾鏡 | 特殊過濾器 (過濾粗體/斜體/刪除線...)... | |
前 15 個工具集: 12 文本 工具 (添加文本, 刪除字符,...) | 50+ 圖表 類型 (甘特圖,...) | 40+ 實用 公式 (根據生日計算年齡,...) | 19 插入 工具 (插入二維碼, 從路徑插入圖片,...) | 12 轉化 工具 (數字到單詞, 貨幣兌換,...) | 7 合併與拆分 工具 (高級合併行, 分裂細胞,...) | ... 和更多 |
使用 Kutools for Excel 增強您的 Excel 技能,體驗前所未有的效率。 Kutools for Excel 提供了 300 多種進階功能來提高生產力並節省時間。 點擊此處獲取您最需要的功能...
Office選項卡為Office帶來了選項卡式界面,使您的工作更加輕鬆
- 在Word,Excel,PowerPoint中啟用選項卡式編輯和閱讀,發布者,Access,Visio和Project。
- 在同一窗口的新選項卡中而不是在新窗口中打開並創建多個文檔。
- 將您的工作效率提高 50%,每天為您減少數百次鼠標點擊!