打开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. Haskell语言学习笔记(76)Data.Tree

    Data.Tree data Tree a = Node { rootLabel :: a, subForest :: Forest a } deriving (Eq, Read, Show) typ ...

  2. vue项目插入视频-mp4

    1. v.vue文件: <template> <div> <div class="contain"> <my-video :sources ...

  3. 30.深入理解abstract class和interface

  4. django 运行脚本

    转自:https://segmentfault.com/a/1190000006752130 runscript 命令会首先检查每个 app 下的 scripts 目录,如果找到对应名字的脚本就会执行 ...

  5. C# 模拟多线程下载文件

    原地址:http://www.cnblogs.com/Opiece/p/4803836.html 客户端 public static string strContent = ""; ...

  6. WIN7系统 如何上传文件到FTP服务器中

    https://zhidao.baidu.com/question/214644671.html

  7. 跨年操作--new Date()

    //时间在2017/12/31 17:00 --- 2018/1/1 06:00区间,提示用户无法操作公告. //time.js (function(){ var date = new Date(); ...

  8. 只需两步获取任何微信小程序源码

    http://baijiahao.baidu.com/s?id=1601969343738344659&wfr=spider&for=pc

  9. 大数据入门到精通2--spark rdd 获得数据的三种方法

    通过hdfs或者spark用户登录操作系统,执行spark-shell spark-shell 也可以带参数,这样就覆盖了默认得参数 spark-shell --master yarn --num-e ...

  10. java中继承thread类的其他类的start()方法与run()方法

    java中继承thread或者实现runnable接口的类必须重写run()方法. 如果其执行了start()方法,其实就是启动了线程的run()方法. 注意:如果是实现runnable接口的类是没有 ...