Excel 多选实现

代码

复制代码
Private 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 Target.Column = 5 Then
    If oldVal = "" Then
      Else
      If newVal = "" Then
        Else
        If InStr(oldVal, newVal) > 0 Then
        Target.Value = oldVal
        Else
      Target.Value = oldVal & ", " & newVal
      End If
        End If
    End If
  End If
End If

exitHandler:
Application.EnableEvents = True
End Sub

设置单选数据

代码粘贴即可

相关推荐
岁忧24 分钟前
(LeetCode 面试经典 150 题 ) 11. 盛最多水的容器 (贪心+双指针)
java·c++·算法·leetcode·面试·go
CJi0NG28 分钟前
【自用】JavaSE--算法、正则表达式、异常
java
一斤代码1 小时前
vue3 下载图片(标签内容可转图)
前端·javascript·vue
Hellyc1 小时前
用户查询优惠券之缓存击穿
java·redis·缓存
中微子1 小时前
React Router 源码深度剖析解决面试中的深层次问题
前端·react.js
光影少年1 小时前
从前端转go开发的学习路线
前端·学习·golang
今天又在摸鱼1 小时前
Maven
java·maven
老马啸西风1 小时前
maven 发布到中央仓库常用脚本-02
java·maven
代码的余温1 小时前
MyBatis集成Logback日志全攻略
java·tomcat·mybatis·logback
中微子1 小时前
React Router 面试指南:从基础到实战
前端·react.js·前端框架