如何从下拉列表中选择多个项目到Excel中的单元格中?
下拉列表在Excel的日常工作中经常使用。 默认情况下,在下拉列表中只能选择一项。 但有时,您可能需要从下拉列表中将多个项目选择到一个单元格中,如下图所示。 如何在Excel中处理它?
从下拉列表中选择多个项目到带有VBA的单元格中
这里有一些VBA可以帮您解决此工作。
从单元格的下拉列表中选择重复项
1.创建下拉列表后,右键单击工作表选项卡以选择 查看代码 从上下文菜单。
2.然后在 Microsoft Visual Basic应用程序 窗口,将下面的代码复制并粘贴到空白脚本中。
VBA:从单元格的下拉列表中选择多个项目
Private Sub Worksheet_Change(ByVal Target As Range)
'UpdatebyExtendoffice20221111
Dim xRgVal As Range
Dim xStrNew As String
On Error Resume Next
Set xRgVal = Cells.SpecialCells(xlCellTypeAllValidation)
If (Target.Count > 1) Or (xRgVal Is Nothing) Then Exit Sub
If Intersect(Target, xRgVal) Is Nothing Then Exit Sub
If Target.Value = "" Then Exit Sub
Application.EnableEvents = False
xStrNew = Target.Value
Application.Undo
If xStrNew = Target.Value Then
Else
xStrNew = xStrNew & " " & Target.Value
Target.Value = xStrNew
End If
Application.EnableEvents = True
End Sub
3.保存代码并关闭窗口以返回到下拉列表。 现在,您可以从下拉列表中选择多个项目。
注意:
1.使用VBA,它可以按空格分隔项目,您可以更改 xStrNew = xStrNew& “” & 目标价值 他人根据需要更改定界符。 例如, xStrNew = xStrNew&“," & 目标价值 将用逗号分隔项目。
2.此VBA代码适用于工作表中的所有下拉列表。
从下拉列表中选择多个项目到一个单元格,无需重复
如果只想从下拉列表中选择一个唯一项,则可以重复上述步骤并使用以下代码。
VBA:从下拉列表到单元格中选择多个项目,无需重复
Private Sub Worksheet_Change(ByVal Target As Range)
'UpdatebyExtendoffice20221111
Dim I As Integer
Dim xRgVal As Range
Dim xStrNew As String
Dim xStrOld As String
Dim xFlag As Boolean
Dim xArr
On Error Resume Next
Set xRgVal = Cells.SpecialCells(xlCellTypeAllValidation)
If (Target.Count > 1) Or (xRgVal Is Nothing) Then Exit Sub
If Intersect(Target, xRgVal) Is Nothing Then Exit Sub
If Target.Value = "" Then Exit Sub
Application.EnableEvents = False
xFlag = True
xStrNew = " " & Target.Value & " "
Application.Undo
xStrOld = Target.Value
If InStr(1, xStrOld, xStrNew) = 0 Then
xStrNew = xStrNew & xStrOld & " "
Else
xStrNew = xStrOld
End If
Target.Value = xStrNew
Application.EnableEvents = True
End Sub
以上两个 VBA 代码都不支持删除部分单元格内容,只支持清除单元格的所有项。
从下拉列表中轻松快速地选择多个项目到一个单元格中
在 Excel 中,除了 VBA 代码,没有其他方法允许从单元格中的下拉列表中选择多个项目。 但是,那 多选下拉列表 的特点 Kutools for Excel 只需点击一下即可快速轻松地处理此工作。
Tips::在应用此工具之前,请先安装 Kutools for Excel。 立即前往免费下载.
步骤1:选择 Kutools > 下拉列表 > 多选下拉列表
第 2 步:在多选下拉列表对话框中,指定设置
- 设置范围;
- 指定分隔单元格中项目的分隔符;
- 决定文字方向;
- 单击“确定”。
结果:
备注:要使用多选下拉列表功能,您应该首先安装Kutools for Excel,请 点击下载并立即免费试用 30 天.
在Excel中轻松创建动态的2级或多级依赖下拉列表
|
在Excel中,创建2级或多级下拉列表很复杂。 在这里 动态下拉列表 实用程序 Kutools for Excel 可以帮你一个忙。 您唯一要做的就是按所示示例对数据进行排序,然后选择数据范围和输出范围,然后将其留给实用程序。 点击免费试用30天! |
Kutools for Excel:拥有300多个便捷的Excel加载项,可以在30天内免费试用。 |
最佳办公生产力工具
🤖 | Kutools 人工智能助手:基于以下内容彻底改变数据分析: 智能执行 | 生成代码 | 创建自定义公式 | 分析数据并生成图表 | 调用 Kutools 函数... |
热门特色: 查找、突出显示或识别重复项 | 删除空白行 | 合并列或单元格而不丢失数据 | 不使用公式进行四舍五入 ... | |
超级查询: 多条件VLookup | 多值VLookup | 跨多个工作表的 VLookup | 模糊查询 .... | |
高级下拉列表: 快速创建下拉列表 | 依赖下拉列表 | 多选下拉列表 .... | |
列管理器: 添加特定数量的列 | 移动列 | 切换隐藏列的可见性状态 | 比较范围和列 ... | |
特色功能: 网格焦点 | 设计图 | 大方程式酒吧 | 工作簿和工作表管理器 | 资源库 (自动文本) | 日期选择器 | 合并工作表 | 加密/解密单元格 | 按列表发送电子邮件 | 超级筛选 | 特殊过滤器 (过滤粗体/斜体/删除线...)... | |
前 15 个工具集: 12 文本 工具 (添加文本, 删除字符,...) | 50+ 图表 类型 (甘特图,...) | 40+ 实用 公式 (根据生日计算年龄,...) | 19 插入 工具 (插入二维码, 从路径插入图片,...) | 12 转化 工具 (小写金额转大写, 货币兑换,...) | 7 合并与拆分 工具 (高级组合行, 分裂细胞,...) | ... 和更多 |
使用 Kutools for Excel 增强您的 Excel 技能,体验前所未有的效率。 Kutools for Excel 提供了 300 多种高级功能来提高生产力并节省时间。 单击此处获取您最需要的功能...
Office Tab 为 Office 带来选项卡式界面,让您的工作更加轻松
- 在Word,Excel,PowerPoint中启用选项卡式编辑和阅读,发布者,Access,Visio和Project。
- 在同一窗口的新选项卡中而不是在新窗口中打开并创建多个文档。
- 每天将您的工作效率提高50%,并减少数百次鼠标单击!
Sort comments by
#42427
This comment was minimized by the moderator on the site
0
0
#42447
This comment was minimized by the moderator on the site
Report
0
0
#42269
This comment was minimized by the moderator on the site
0
0
#42343
This comment was minimized by the moderator on the site
Report
0
0
#41145
This comment was minimized by the moderator on the site
0
0
#39232
This comment was minimized by the moderator on the site
0
0
#39306
This comment was minimized by the moderator on the site
Report
0
0
#39344
This comment was minimized by the moderator on the site
0
0
#39775
This comment was minimized by the moderator on the site
Report
0
0
#39789
This comment was minimized by the moderator on the site
0
0
#39425
This comment was minimized by the moderator on the site
Report
0
0
#37741
This comment was minimized by the moderator on the site
0
0
#37742
This comment was minimized by the moderator on the site
Report
0
0
#37743
This comment was minimized by the moderator on the site
0
0
#35627
This comment was minimized by the moderator on the site
0
0
#35069
This comment was minimized by the moderator on the site
Report
0
0
#35070
This comment was minimized by the moderator on the site
Report
0
0
#41149
This comment was minimized by the moderator on the site
0
0
#31678
This comment was minimized by the moderator on the site
Report
0
0
#30250
This comment was minimized by the moderator on the site
0
0
#29970
This comment was minimized by the moderator on the site
1
0
There are no comments posted here yet