提示:其它语言是由 Google 机器翻译的。 你可以访问 English 版本。
登录
x
or
x
x
马上登记
x

or

如何在Excel中创建多个选择或值的下拉列表?

默认情况下,当您在工作表中创建数据验证下拉列表时,您只能在列表中每次只选择一个项目。 但是如果你想在下拉列表中选择多个值,你会怎么做? 在本文中,我们将向您展示如何增强下拉列表中的选择。

使用VBA代码创建带有多个选择的下拉列表


您可能感兴趣的是:

将多个工作表/工作簿合并到一个工作表/工作簿中:

将多个工作表或工作簿合并到一个工作表或工作簿中可能是您​​日常工作中的一项重大任务。 但是,如果你有 Kutools for Excel,其强大的效用 - 结合 可以帮助您将多个工作表,工作簿快速合并到一个工作表或工作簿中。

Kutools for Excel 包含了比300更方便的Excel工具。 免费试用60天无限制。 了解更多 立即下载免费试用版

Office选项卡在Office中启用选项卡式编辑和浏览,使您的工作更轻松......
Kutools for Excel - 最佳办公生产力工具将解决您的大部分Excel问题
  • 重用任何东西: 将最常用或最复杂的公式,图表和其他任何内容添加到您的收藏夹中,并在将来快速重复使用它们。
  • 超过20文本功能: 从文本字符串中提取数字; 提取或删除部分文本; 将数字和货币转换为英语单词...
  • 合并工具:多个工作簿和表格合二为一; 合并多个单元格/行/列而不丢失数据; 合并重复行和总和...
  • 拆分工具:根据价值将数据拆分为多个表格; 一个工作簿到多个Excel,PDF或CSV文件; 一列到多列......
  • 粘贴跳过 隐藏/过滤行; 数和总和 按背景颜色; 创建邮件列表和 通过Cell的价值发送电子邮件...
  • 超级过滤器: 创建高级过滤方案并应用于任何工作表; 排序 按周,日,频率等; 筛选 通过大胆,公式,评论......
  • 超过300强大的功能; 适用于Office 2007-2019和365; 支持所有语言; 在公司轻松部署; 全功能60天免费试用。


使用VBA代码创建带有多个选择的下拉列表

使用VBA方法,您的下拉列表可以在工作表中选择多个值而不是一个值。

1。 创建下拉列表后,例如,您的下拉列表位于sheet1中,右键单击Sheet1选项卡并单击 查看代码 在右键菜单中。 看截图:

2。 在里面 Microsoft Visual Basic for Applications 窗口中,双击Sheet1打开代码编辑器,然后将下面的VBA代码复制并粘贴到编辑器中。 看截图:

VBA代码:多个选择的下拉列表

Private Sub Worksheet_Change(ByVal Target As Range)
    'Updated: 2016/4/12
    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。 然后点击 文件 > 关闭并返回到Microsoft Excel 退出 Microsoft Visual Basic for Applications 窗口。

4。 转到您创建的下拉列表,您可以从列表中选择多个值,如下面的截图所示。

笔记:

1。 下拉列表中不允许重复的值。

2。 VBA代码只能用于当前打开的工作簿。 如果关闭并重新打开工作簿,VBA代码将自动从工作表中删除,并且多重选择不再可用。 因此,当您保存工作簿时,需要将工作簿保存为Excel Macro-Enabled工作簿格式。


相关文章:


Kutools for Excel - 最佳办公生产力工具提高80%的生产力

  • 重用: 快速插入 复杂的公式,图表 以及你以前用过的任何东西; 加密单元格 密码; 创建邮件列表 并发送电子邮件...
  • 超级方程式酒吧 (轻松编辑多行文字和公式); 阅读布局 (轻松读取和编辑大量单元格); 粘贴到过滤范围...
  • 合并单元格/行/列 不丢失数据; 分裂细胞含量; 组合重复的行/列...防止重复的细胞; 比较范围...
  • 选择复制或唯一 行; 选择空行 (所有细胞都是空的); 超级查找和模糊查找 在许多工作簿中; 随机选择......
  • 精确复制 多个单元格而不更改公式参考; 自动创建参考 多张表; 插入项目符号,复选框等等......
  • 提取文本,添加文本,按位置删除, 删除空间; 创建和打印分页小计; 在单元格内容和注释之间转换...
  • 超级过滤器 (将过滤方案保存并应用到其他工作表); 高级排序 按月/周/日,频率等; 特殊过滤器 用粗体,斜体......
  • 结合工作簿和工作表; 根据键列合并表; 将数据拆分为多个表格; 批量转换xls,xlsx和PDF...
  • 超过300强大的功能。 支持Office / Excel 2007-2019和365。 支持所有语言。 在您的企业或组织中轻松部署。 全功能60天免费试用。
kte tab 201905

Office选项卡为Office提供选项卡式界面,使您的工作更轻松

  • 在Word,Excel,PowerPoint中启用选项卡式编辑和阅读,Publisher,Access,Visio和Project。
  • 在同一窗口的新选项卡中打开并创建多个文档,而不是在新窗口中。
  • 通过50%提高您的工作效率,每天为您减少数百次鼠标点击!
官方底部
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    Eni · 14 days ago
    Hi, ich bin totaler VBA Laie. Ich versuche den Code so zu modifizieren, dass
    a) die Mehrfachauswahl nicht in allen, sondern nur ein zwei Spalten aktiv ist
    b) ich Items auch wieder rausnehmen kann, zB in dem ich in der Listenauswahl das Item noch einmal anklicke (Beispiel: ich habe über die Mehrfachauswahl ausgewählt: A, D, X, Y... nun fällt mir auf, dass D nicht dazu gehört. Beim aktuellen Code müsste ich Eingaben entfernen und neu auswählen).
    Danke im Voraus!
  • To post as a guest, your comment is unpublished.
    wendy · 23 days ago
    I'm using the code below to allow multi-select on multiple worksheets but when I go to another worksheet in the workbook the multi-select goes away. When I save the file and come back in it will work for one tab with the code but again when I click on another tab with the code it no longer works. Any idea how to fix it so if i click on a worksheet with the VBA code it will always allow multi-select?
  • To post as a guest, your comment is unpublished.
    Randy · 1 years ago
    I'm trying to create 4 columns with drop down lists where I can select multiple values. How do I modify the "drop down list with multiple selections" VBA code so that when I click on a value that has already been entered it removes it from the cell? Thank you in advance.
    • To post as a guest, your comment is unpublished.
      crystal · 1 years ago
      Dear Randy,
      What do you mean "when I click on a value that has already been entered it removes it from the cell?"
      • To post as a guest, your comment is unpublished.
        Dez · 1 years ago
        I have the same question. My drop down list does not remember values selected. If someone clicks on a cell that has already been populated (not by them, but someone else) the selected values are cleared and the cell is blank again.
  • To post as a guest, your comment is unpublished.
    Johnna · 1 years ago
    I created a drop down list where multiple text selections could be chosen such as "nutrition" ,"weight", and "work" for each caller's reason to phone in. I have a summary page where I want to see how many of each reason were indicated in a particular month. What formula would I use to tell Excel to pull out and tally each of these separately in a given month? Currently, the way I have it set up, it only tallies correctly if I have one reason in the cell for each caller.
    • To post as a guest, your comment is unpublished.
      crystal · 1 years ago
      Good Day,
      Sorry can't help you solve this problem. Please let me know if you find the answer.
  • To post as a guest, your comment is unpublished.
    Nancy · 2 years ago
    I managed to use this code and successfully create multiple selection drop down boxes. It worked when I closed and re-opened on different days. However, now not all of the cells I originally selected are allowing multiple selection. Only ones done previously, despite using the code for the whole spreadsheet. Can you help?
    • To post as a guest, your comment is unpublished.
      yesenia · 1 years ago
      the cells are most likely locked, right click on all of them, go to format cells, protection, then uncheck the locked cell option
    • To post as a guest, your comment is unpublished.
      Lisa Thompson · 2 years ago
      I'm having the same problem.
  • To post as a guest, your comment is unpublished.
    Desiree · 2 years ago
    Hi all,

    I could do my drop down list perfectly, but my question is: when I select all the items nedded it goes one after another in an horizontal way through the cell, for example: yellow, green, black, red. But how can I make it look in a vertical way?, more like for example: Orange
    blanck
    yellow
    Red
    Because in horizontal the cell becomes pretty long when selecting lots of items.

    Could you please tell me if there's any way to do this?.

    Thank you,

    Desiree
  • To post as a guest, your comment is unpublished.
    Chloe · 2 years ago
    Hi all,

    I have this code on an excel sheet and its cleaning the contents from the drop down list when the cell is selected - I know what part of the code is doing it (the part that says 'fillRng.ClearContents') and I have tried to use some of the above to fix it unsuccessfully... I am new to VBA programming etc. Can anyone offer any help on how to change it so that it when the cell is selected it doesn't clear and entries wont be duplicated please??

    Option Explicit
    Dim fillRng As Range
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    Dim Qualifiers As MSForms.ListBox
    Dim LBobj As OLEObject
    Dim i As Long

    Set LBobj = Me.OLEObjects("ListBox1")
    Set Qualifiers = LBobj.Object

    If Target.Row > 3 And Target.Column = 3 Then
    Set fillRng = Target
    With LBobj
    .Left = fillRng.Left
    .Top = fillRng.Top
    .Width = fillRng.Width
    .Height = 155
    .Visible = True
    End With
    Else
    LBobj.Visible = False
    If Not fillRng Is Nothing Then
    fillRng.ClearContents
    With Qualifiers
    If .ListCount 0 Then
    For i = 0 To .ListCount - 1
    If fillRng.Value = "" Then
    If .Selected(i) Then fillRng.Value = .List(i)
    Else
    If .Selected(i) Then fillRng.Value = _
    fillRng.Value & ", " & .List(i)
    End If
    Next
    End If
    For i = 0 To .ListCount - 1
    .Selected(i) = False
    Next
    End With
    Set fillRng = Nothing
    End If
    End If

    End Sub
  • To post as a guest, your comment is unpublished.
    Ramon · 3 years ago
    Hi there,

    Code works fine. However, I can't seem to deselect an item. When I want to remove an item from the selection, it's just not removed. Does anybody else experience this problem too?
    • To post as a guest, your comment is unpublished.
      StPaulSue · 2 years ago
      delete the content in the cell, then reselect
    • To post as a guest, your comment is unpublished.
      THG · 2 years ago
      Was there a response to this issue. It is the same issue I am having. There doesn't seem to be a way to remove an item that has been selected.
  • To post as a guest, your comment is unpublished.
    Charity · 3 years ago
    This works well, but I am unable to remove an item once selected. Any suggestions in case I click on something accidently and need to remove it without (hopefully) clearing the whole cell and starting over?

    Also, for those seeking to define a column or columns, Contextures has a great addition to the code provided here that allows you to do that.
    http://www.contextures.com/excel-data-validation-multiple.html#column
    • To post as a guest, your comment is unpublished.
      Nirmala · 2 years ago
      [quote name="Charity"]This works well, but I am unable to remove an item once selected. Any suggestions in case I click on something accidently and need to remove it without (hopefully) clearing the whole cell and starting over?

      Also, for those seeking to define a column or columns, Contextures has a great addition to the code provided here that allows you to do that.
      http://www.contextures.com/excel-data-validation-multiple.html#column[/quote]

      Code works fine. However, I can't seem to deselect an item. When I want to remove an item from the selection, it's just not removed. Does anybody else experience this problem too?[/quote]

      Hi all,

      Any solutions found for this problem..please share..
  • To post as a guest, your comment is unpublished.
    stef · 3 years ago
    Hi I am currently using this formula and all columns with data validation have the multiple selection option now, however I want to restrict the multiple selection only to one column. Can someone edit this formula for me so the multiple selection can be applied only to Column4? Thanks :)

    Private Sub Worksheet_Change(ByVal Target As Range)
    'Updated: 2016/4/12
    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

    Any assistance will be appreciated!
  • To post as a guest, your comment is unpublished.
    Mervyn · 3 years ago
    @Cynthia,

    If still required, you should be able to do something like this to only ensure the code runs on specific columns, in my case, column 34 and 35:

    If (Target.Column 34 And Target.Column 35) Then Exit Sub

    'Put this code at the beginning after your dim statements
    • To post as a guest, your comment is unpublished.
      Dhina · 1 years ago
      If Target.Column <> 34 Then Exit Sub

      'Put this code at the beginning after your dim statements
    • To post as a guest, your comment is unpublished.
      CynthiaB · 2 years ago
      [quote name="Mervyn"]@Cynthia,

      If still required, you should be able to do something like this to only ensure the code runs on specific columns, in my case, column 34 and 35:

      If (Target.Column 34 And Target.Column 35) Then Exit Sub

      'Put this code at the beginning after your dim statements[/quote]


      Hi @Mervyn,

      Lost track of the thread completely, but thank you so much for your responses.

      I've tried applying the
      If (Target.Column 34 And Target.Column 35) Then Exit Sub
      (my version reads If (Target.Column4 And Target.Column5) Then Exit Sub
      as you supplied, but am getting a "Run-time error '438': Object doesn't support this property or method"" error on this new line.

      Here are the first few lines of my code:

      Private Sub Worksheet_Change(ByVal Target As Range)
      Dim xRng As Range
      Dim xValue1 As String
      Dim xValue2 As String
      If (Target.Column4 And Target.Column5) Then Exit Sub
      If Target.Count > 1 Then Exit Sub

      On Error Resume Next


      My worksheet only has 6 columns: Question | Answer | Category | Sub-Category | Tags | Photo link
      I only need multiple value drop downs in Sub-Category and Tags (columns 4 & 5).

      I'll keep looking for info as you suggested on 12/23, and will look at the link Charity provided.
  • To post as a guest, your comment is unpublished.
    Mervyn · 3 years ago
    Hi Cynthia,

    If the original author doesn't reply, I'll get you an answer but I'll only be in front of a computer on 29 Dec again. I'm also no VBA programmer. What you can do in the mean time is Google search how to identify the column number and only let the code run if data is edited in that specific column(s). I've done it but the code is on my work PC and can't recall it at the moment,maybe try putting a debug.print on target.column or something to that effect to see if it gives you the column number being edited.

    Sorry Jennifer, not sure about the issue you're having :(
  • To post as a guest, your comment is unpublished.
    Jennifer L Price · 3 years ago
    I was able to get the code to work, but then when I saved the document (with macros-enabled), closed it and returned, the code didn't work anymore (though it was still in there). I can't figure out what I've done wrong. Any ideas?
  • To post as a guest, your comment is unpublished.
    CynthiaB · 3 years ago
    Hi. Thank you for the code and the addition to limit duplicates.


    One more request - what addition/change would have to be made in order to allow multiple selection in only one or two specific columns? This code is re-adding lines of text to what should be 'plain' cells if I go to correct a typo, or make a change or addition to the text in the cell, as opposed to just behaving 'normally' and accepting the change (without re-adding the entire text again).

    For instance, column A is a 'plain' column. I write a sentence "What are the three itmes you want most?" Column B is a 'list' column where I only want to be able to pick one single value (in this case, let's say a child's name). Column C is another 'list' column where the user must be able to select multiple items (which this code allows me to do perfectly).

    As I go along, I realize that I've made a typo in column A and want to correct it. As this code stands, if I go in (double click, F2) and make the correction to the word "items", I end up with this result in my cell:"What are the three itmes you want most? What are the three items you want most?"

    thank you in advance for any help (from a user that REALLY likes VBA, but is still at the very earliest stages of learning!)
  • To post as a guest, your comment is unpublished.
    Mervyn · 3 years ago
    Just realised I didn't exit the loop in the new function if the condition has been set so we don't have to check other entries.
  • To post as a guest, your comment is unpublished.
    Mervyn · 3 years ago
    You can change the code in the following lines to prevent the duplicates:
    If xValue2 "" Then
    Target.Value = xValue1 & ", " & xValue2
    End If

    To:
    If xValue2 "" Then
    If CheckIfAlreadyAdded(xValue1, xValue2) = False Then
    Target.Value = xValue1 & ", " & xValue2
    Else
    Target.Value = xValue1
    End If
    End If

    And then add the following function:
    Private Function CheckIfAlreadyAdded(ByVal sText As String, sNewValue As String) As Boolean

    CheckIfAlreadyAdded = False

    Dim WrdArray() As String
    WrdArray() = Split(sText, ",")

    For i = LBound(WrdArray) To UBound(WrdArray)
    If Trim(WrdArray(i)) = Trim(sNewValue) Then CheckIfAlreadyAdded = True
    Next i

    End Function

    --
    There's probably better ways of coding it but it works for now.
  • To post as a guest, your comment is unpublished.
    MichaelB · 3 years ago
    It is great that this allows multiple selections but like @Yezdi commented, I am finding it will add one or several duplicates even if I don't choose them.

    So, at present, this is an 80% solution... one tweak away from perfect. I am not a VB coder or I'd offer the solution.
  • To post as a guest, your comment is unpublished.
    Yezdi Eks · 4 years ago
    Hi,

    Thanks for the solution and the code.

    But the next step is how to make sure that the user
    does not select "duplicate" values from the dropdown list.

    E.g. If there are 4 items in the list -
    orange, apple, banana, peach

    and if the user has already selected "orange", then excel
    should not allow the user to select "orange" OR that option
    should be removed from the remainder of the list.

    Can you please publish the code to accomplish this feature.

    Thanks.

    Yezdi
    • To post as a guest, your comment is unpublished.
      sunshine · 3 years ago
      Hi Yezdi,
      Thank you for your comment. The code was updated and no duplicate values allow in the drop-down list now.

      Thanks.

      Sunshine