在 Excel 下拉列表中選擇多個項目 – 完整指南
Excel 的下拉列表是確保數據一致性和簡化輸入的絕佳工具。然而,默認情況下,它們只允許選擇一個項目。但如果您需要從同一個下拉列表中選擇多個項目呢?本綜合指南將探討如何在 Excel 下拉列表中啟用多選、管理重複項、設置自定義分隔符以及定義這些列表的範圍。
啟用下拉列表中的多選功能
本節提供了兩種方法,幫助您在 Excel 的下拉列表中啟用多選功能。
使用 VBA 代碼
要允許在下拉列表中進行多選,您可以使用 Excel 中的“Visual Basic for Applications”(VBA)。該腳本可以修改下拉列表的行為,使其成為多選列表。請按以下步驟操作。
步驟 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,一個更簡單的替代方案是“Kutools for Excel”的“使下拉列表可多次選擇”功能。這個用戶友好的工具簡化了在下拉列表中啟用多選的功能,讓您可以輕鬆自定義分隔符並管理重複項,滿足不同的需求。
安裝 Kutools for Excel 後,轉到“Kutools”選項卡,選擇“下拉列表” > “使下拉列表可多次選擇”。然後您需要按如下方式進行配置。
- 指定包含您需要從中選擇多個項目的下拉列表的區域。
- 指定下拉列表單元格中所選項目的分隔符。
- 點擊“確定”完成設置。
結果
現在,當您點擊指定區域內具有下拉列表的單元格時,旁邊會出現一個列表框。只需點擊項目旁的“+”按鈕即可將它們添加到下拉單元格中,點擊“-”按鈕則可刪除不再需要的項目。請參閱下面的演示:
- 如果希望在單元格內垂直顯示所選項目,請勾選“插入分隔符後換行”選項。如果您偏好水平列出,請保持此選項未勾選。
- 如果希望為下拉列表添加搜索欄,請勾選“啟用搜索功能”選項。
- 要應用此功能,請先下載並安裝 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 AI 助手:基於智能執行方式革新數據分析:智能執行 | 生成代碼 | 創建自訂公式 | 分析數據並生成圖表 | 調用 Kutools 函數… |
熱門功能:查找、標記重複值或識別重複項 | 刪除空行 | 合併列或單元格而不丟失數據 | 四捨五入無需公式 ... | |
高級 LOOKUP: 多條件 VLookup | 多值 VLookup | 多表查找 | 模糊查找 .... | |
高級下拉列表:快速創建下拉列表 | 依賴下拉列表 | 多選下拉列表 .... | |
列管理器: 添加特定數量的列 | 移動列 | 切換隱藏列的可見狀態 | 比較區域和列 ... | |
特色功能:網格聚焦 | 設計檢視 | 增強編輯欄 | 工作簿與工作表管理器 | 資源庫(自動文本) | 日期提取器 | 合併資料 | 加密/解密儲存格 | 按列表發送電子郵件 | 超級篩選 | 特殊篩選(篩選粗體/斜體/刪除線...) ... | |
頂級 15 種工具集: 12 個文本工具(添加文本、刪除特定字符、...) | 50+ 圖表 類型(甘特圖、...) | 40+ 實用 公式(基於生日計算年齡、...) | 19 個插入工具(插入QR碼、根據路徑插入圖片、...) | 12 個轉換工具(金額轉大寫、匯率轉換、...) | 7 個合併與分割工具(高級合併行、分割儲存格、...) | ... 還有更多 |
使用 Kutools for Excel 提升您的 Excel 技巧,體驗前所未有的高效。 Kutools for Excel 提供超過 300 種高級功能來提高生產力並節省時間。 點擊這裡獲取您最需要的功能...
Office Tab 將標籤式界面帶到 Office,讓您的工作更加輕鬆
- 在 Word、Excel、PowerPoint、Publisher、Access、Visio 和 Project 中啟用標籤式編輯和閱讀。
- 在同一窗口的新標籤中打開和創建多個文檔,而不是在新窗口中。
- 將您的生產力提高 50%,每天為您減少數百次鼠標點擊!