打开Exlce,

确定,然后

右击查看代码,把这段代码复制到新建的文件里面

此时Excel会给出提示,选择否,,系统会提示保存,在保存的时候选择启用宏的工作簿然后保存,此时Excel下拉框多选就搞定了,最后,代码如下:

Option Explicit

Sub Worksheet_Change(ByVal Target As Range)
'让数据有效性选择 可以多选,重复选
Dim rngDV As Range
Dim oldVal As String
Dim newVal As String
If Target.Count > 1 Then GoTo exitHandler On Error Resume Next
Set rngDV = Cells.SpecialCells(xlCellTypeAllValidation)
On Error GoTo exitHandler If rngDV Is Nothing Then GoTo exitHandler If Intersect(Target, rngDV) Is Nothing Then
'do nothing
Else
Application.EnableEvents = False
newVal = Target.Value
Application.Undo
oldVal = Target.Value
Target.Value = newVal
If oldVal = "" Then
Else
If newVal = "" Then
Else
Target.Value = oldVal _
& ", " & newVal
End If
End If
End If exitHandler:
Application.EnableEvents = True
End Sub

优化后的代码

Option Explicit

Sub Worksheet_Change(ByVal Target As Range)
'让数据有效性选择 可以多选,重复选
Dim rngDV As Range
Dim oldVal As String
Dim newVal As String
If Target.Count > 1 Then GoTo exitHandler On Error Resume Next
Set rngDV = Cells.SpecialCells(xlCellTypeAllValidation)
On Error GoTo exitHandler If rngDV Is Nothing Then GoTo exitHandler If Intersect(Target, rngDV) Is Nothing Then
'do nothing
Else
Application.EnableEvents = False
newVal = Target.Value
Application.Undo
oldVal = Target.Value
Target.Value = newVal
If oldVal = "" Then
Else
If newVal = "" Then
Else
If Target.Column <> 2 And Target.Column <> 3 And Target.Column <> 5 Then
Dim oldValArray
oldValArray = Split(oldVal, ",")
Dim exitVal As Boolean
exitVal = False
Dim i As Integer
Dim resultVal As String
For i = 0 To UBound(oldValArray)
If oldValArray(i) = newVal Then
exitVal = True
Else
If resultVal = "" Then
resultVal = oldValArray(i)
Else
resultVal = resultVal & "," & oldValArray(i)
End If
End If
Next
If exitVal = False Then
If oldVal = newVal Then
Target.Value = resultVal
Else
Target.Value = resultVal & "," & newVal
End If
Else
Target.Value = resultVal
End If
End If
End If
End If
End If exitHandler:
Application.EnableEvents = True
End Sub

  

   

转载自:https://www.cnblogs.com/boosasliulin/p/5970120.html

Excel怎么下拉框多选的更多相关文章

  1. jquery实现下拉框多选

    一.说明 本文是利用EasyUI实现下拉框多选功能,在ComboxTree其原有的基础上对样式进行了改进,样式表已上传demo,代码如下 二.代码 <!DOCTYPE html PUBLIC & ...

  2. combobox中动态加入几个checkbox,实现下拉框多选

    combobox中动态加入几个checkbox,实现下拉框多选,将一个checkbox选中时其内容就会在combobox中显示出来,将另一个checkbox选中时其内容会跟在第一个checkbox的内 ...

  3. vue实现下拉框全选和输入匹配

    实际项目中的一个需求: 点击文本框,弹出带有复选框的选项,然后获取选中项的数据,传给后面的一个功能.在文本框输入内容,也会动态的匹配下拉列表,并且列表带有全选功能. 朴素的效果图: 我选择了用vue实 ...

  4. 使用poi解决导出excel内下拉框枚举项较多的问题

    废话少说,直接上代码: package com.fst.attachment.controller; import java.io.FileOutputStream; import org.apach ...

  5. easyui combotree下拉框多选赋值

    发现jquery.easyui.min.js 1.3.4版本的用setValues给多选下拉框赋值不成功,只能用1.3.1版本的 Html代码: <input id="ProductL ...

  6. checkboxlist 下拉框多选功能 ,模拟dropdownlist带复选框效果

    前台代码 01.<html xmlns="http://www.w3.org/1999/xhtml"> 02.<head runat="server&q ...

  7. ops-web运维平台-create.jsp-mootools下拉框-复选框

    create.jsp页面的,body部分 <body onload="Page.init('${pageError}','${pageMessage}',${isSubmit},tru ...

  8. 表id关联数据获取至页面,制作下拉框多选进行数据多项获取(字段处理)

     这周完成了一张表单,重点碰到以下问题: 1.freemaker获取年份的type值取year,类型直接为Long,传至后台和获取数据不需要转换: 2.freemaker获取日期type值为date, ...

  9. angularJs实现下拉框多选

    话不多说,直接上干货. 肯定需要下拉选插件.必须引入的是   注意 先后顺序 select2.css select2-bootstrap.css select2.min.js angular.min. ...

随机推荐

  1. Visual studio 2019 preview & C# 8 initial experience

    Visual studio 2019 preview & C# 8 initial experience       using System; using static System.Con ...

  2. List of numerical libraries,Top Numerical Libraries For C#

    Top Numerical Libraries For C# AlgLib (http://alglib.net) ALGLIB is a numerical analysis and data pr ...

  3. 在Unity中使用Lua脚本

    前言:为什么要用Lua首先要说,所有编程语言里面,我最喜欢的还是C#,VisualStudio+C#,只能说太舒服了.所以说,为什么非要在unity里面用Lua呢?可能主要是闲的蛋疼.....另外还有 ...

  4. 认识 Thymeleaf

  5. C#关键字as出现的错误

    ObjectCache cache = MemoryCache.Default; string cacheData1 = cache["key1"] as string;//得不到 ...

  6. 三,APIView、GenericAPIView、Mixins总结

    概述 APIView是DRF的视图层中最基本的类,它相当于Django中的View类,其他视图类都是通过继承APIView实现的. GenericAPIView继承于APIView,在其父类的基础上为 ...

  7. PPT的感想

    ①double:使用double类型的数值进行计算, 其结果是不精确的.因为double类型的数值占用64个二进制数,除去最高位表示正负符号的位,在最低位上一定会与实际数据存在误差. 这个涉及到二进制 ...

  8. RecycleView 使用自定义CardLayouManager内容无法滚动问题

    1.开始一直反应不过来一个问题:RecycleView不是自带滚动效果吗?为啥子条目还不能全部滚动,显示出来呢? 意识到:只有当RecycleView 适配器中条目数量特别多,才可以滚动. 然而自己的 ...

  9. cdnbest节点如何升级

    点节点列表==>选中要升级版本的节点==>更多操作==>升级节点 点击后会显示成功,这个时候就只需要等待节点自动升级就好,升级快时几分钟,慢可能就更久点了 但也有特殊情况会升级失败, ...

  10. Appium1.6启动ios9.3报错Original error: Sdk '9.3.5' was not in list of simctl sdks

    问题: 使用Apppium1.6启动ios9.3报错Original error: Sdk '9.3.5' was not in list of simctl sdks   我的启动配置如下 {   ...