跳至主要内容

在 Excel 下拉列表中选择多个项目 – 完整指南

Author: Siluvia Last Modified: 2025-07-31

Excel 下拉列表是确保数据一致性和输入便捷性的绝佳工具。然而,默认情况下,它们只允许选择一个项目。但如果你需要从同一个下拉列表中选择多个项目怎么办?本综合指南将探讨在 Excel 下拉列表中启用多选、管理重复项、设置自定义分隔符以及定义这些列表范围的方法。

A screenshot of the animated demo showing multiple selections in an Excel drop-down list.

提示:在应用以下方法之前,请确保你已事先在工作表中创建了下拉列表。如果你想了解如何创建数据验证下拉列表,请按照本文中的说明进行操作:如何在 Excel 中创建数据验证下拉列表

启用下拉列表中的多选功能

本节提供了两种方法,帮助你在 Excel 的下拉列表中启用多选功能。

使用 VBA 代码

要允许在下拉列表中进行多选,可以使用 Excel 中的“Visual Basic for Applications”(VBA)。该脚本可以修改下拉列表的行为,使其成为一个多选列表。请按照以下步骤操作。

步骤 1:打开工作表(代码)编辑器
  1. 打开包含你想启用多选功能的下拉列表的工作表。
  2. 右键单击工作表标签,并从上下文菜单中选择“查看代码”。
    A screenshot of the View Code option in the context menu of a sheet tab in Excel
步骤 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

A screenshot of the VBA code pasted into the Excel VBA editor

结果

当你返回到工作表时,下拉列表将允许你选择多个选项,请参见下面的演示:

A screenshot of the animated demo showing multiple selections in an Excel drop-down list

注意:上述 VBA 代码:
  • 适用于当前工作表中的所有数据验证下拉列表,包括现有的和未来创建的。
  • 防止你在每个下拉列表中多次选择相同的项目。
  • 使用逗号作为所选项的分隔符。要使用其他分隔符,请查看此部分以更改分隔符

只需点击几下即可使用 Kutools for Excel

如果你不熟悉 VBA,一个更简单的替代方案是“Kutools for Excel”的“使下拉列表可多次选择”功能。这个用户友好的工具简化了在下拉列表中启用多选的过程,让你能够轻松自定义分隔符并管理重复项,满足你的不同需求。

Kutools for Excel 提供了超过 300 种高级功能,简化复杂任务,提升创造力与效率。 通过集成 AI 能力,Kutools 能够精准自动执行任务,让数据管理变得轻松简单。Kutools for Excel 的详细信息...         免费试用...

安装 Kutools for Excel 后,转到“Kutools”选项卡,选择“下拉列表” > “使下拉列表可多次选择”。然后你需要按如下方式配置。

  1. 指定包含下拉列表的区域,从中你需要选择多个项目。
  2. 指定下拉列表单元格中所选项的分隔符。
  3. 单击“确定”完成设置。
结果

现在,当你点击指定区域内的带有下拉列表的单元格时,旁边会出现一个列表框。只需点击项目旁边的“+”按钮即可将它们添加到下拉单元格中,点击“-”按钮即可移除你不再需要的任何项目。请参见下面的演示:

A screenshot showing a demo of managing multiple selections in an Excel drop-down list using 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
结果

现在你可以从当前工作表中的下拉列表中选择多个项目。要在下拉列表单元格中重复某个项目,继续从列表中选择该项目。请参见截图:

A screenshot of the animated demo showing duplicate selections in an Excel drop-down list


从下拉列表中移除任何现有项

从下拉列表中选择多个项目后,有时你可能需要从下拉列表单元格中移除现有项。本节提供了另一段 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 代码允许你从下拉列表中选择多个项目,并轻松移除你已经选择的任何项目。在选择多个项目后,如果你想移除特定的一个,只需再次从列表中选择它即可。

A screenshot of the animated demo showing how to remove existing items from a drop-down list in Excel


设置自定义分隔符

在上述 VBA 代码中,分隔符设置为逗号。你可以将此变量修改为你需要的任何字符,用作下拉列表选择的分隔符。以下是具体操作方法:

如你所见,上述 VBA 代码都有以下这一行:

delimiter = ", "

你只需要根据需要将逗号更改为任何分隔符。例如,如果你想用分号分隔项目,将该行更改为:

delimiter = "; "
注意:要在这些 VBA 代码中将分隔符更改为换行符,请将该行更改为:
delimiter = vbNewLine

设置指定区域

上述 VBA 代码适用于当前工作表中的所有下拉列表。如果你只想让 VBA 代码适用于某些范围内的下拉列表,可以在上述 VBA 代码中按如下方式指定范围。

如你所见,上述 VBA 代码都有以下这一行:

Set TargetRange = Me.UsedRange

你只需要将该行更改为:

Set TargetRange = Me.Range("C2:C10")
注意:这里 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
注意:在代码中,确保将 pswd = "yourPassword" 这一行中的“yourPassword”替换为你实际用于保护工作表的密码。例如,如果你的密码是“abc123”,那么该行应为 pswd = "abc123"。

通过在 Excel 下拉列表中启用多选功能,你可以极大地增强工作表的功能性和灵活性。无论你是熟悉 VBA 编码还是更倾向于像 Kutools 这样更直接的解决方案,你现在都可以将标准下拉列表转换为动态的多选工具。有了这些技能,你现在已经具备了创建更具动态性和用户友好性的 Excel 文档的能力。对于那些渴望深入探索 Excel 功能的人,我们的网站拥有丰富的教程。在这里发现更多 Excel 技巧和窍门

最佳 Office 办公效率工具

🤖 Kutools AI 助手:基于智能执行,彻底革新数据分析 生成代码创建自定义公式分析数据并生成图表调用 Kutools Functions
热门功能:查找、选中项的背景色或标记重复项|删除空行|合并列或单元格且不丢失数据|四舍五入(无公式)...
高级 LOOKUP多条件查找 (VLookup)多值查找 (VLookup)多表查找 (VLookup Across Multiple Sheets)模糊查找 (Fuzzy Lookup)...
高级下拉列表快速创建下拉列表依赖型下拉列表多选下拉列表...
列管理器添加指定数量的列移动列切换隐藏列的可见状态比较区域及列...
特色功能网格聚焦设计视图增强编辑栏工作簿 & 工作表管理器资源库(自动文本)|日期提取合并数据加密/解密单元格按列表发送电子邮件超级筛选特殊筛选(筛选粗体/倾斜/删除线等)...
热门15 大工具集12 款文本工具添加文本删除特定字符等)50+ 种图表 类型甘特图等)40+ 实用公式基于生日计算年龄等)19 款插入工具插入二维码按路径插入图片等)12 种转换工具小写金额转大写汇率转换等)7 款合并与分割工具高级合并行分割单元格等)...更多精彩等你发现
使用 Kutools,支持您的首选语言——包括英语、西班牙语、德语、法语、中文及40 多种其他语言!

用 Kutools for Excel 加速你的 Excel 技能,体验前所未有的高效办公。 Kutools for Excel 提供300 多项高级功能,助您提升效率,节省大量时间。点击此处,获取你最需要的功能...


Office Tab 为 Office 带来标签式界面,让你的工作更加轻松

  • 在 Word、Excel、PowerPoint 启用标签式编辑和阅读
  • 在同一窗口的新标签中打开和创建多个文档,无需新建窗口。
  • 办公效率提升50%,每天帮你减少上百次鼠标点击!