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

设置单选数据

代码粘贴即可

相关推荐
朝新_5 小时前
【实战】动态 SQL + 统一 Result + 登录校验:图书管理系统(下)
xml·java·数据库·sql·mybatis
一个假的前端男6 小时前
uniapp 3端轮播
前端·javascript·uni-app
百***92026 小时前
java进阶1——JVM
java·开发语言·jvm
迦蓝叶6 小时前
RDF 与 RDFS:知识图谱推理的基石
java·人工智能·数据挖掘·知识图谱·语义网·rdf·rdfs
百锦再6 小时前
选择Rust的理由:从内存管理到抛弃抽象
android·java·开发语言·后端·python·rust·go
yaoxin5211236 小时前
238. Java 集合 - 使用 ListIterator 遍历 List 元素
java·python·list
爱分享的Shawn_Salt6 小时前
IntelliJ IDEA初始化指南
java·ide·intellij-idea
Dxxyyyy6 小时前
零基础学JAVA--Day32(ArrayList底层+Vector+LinkedList)
java·开发语言
Pluchon6 小时前
硅基计划6.0 柒 JavaEE 浅谈JVM&GC垃圾回收
java·jvm·数据结构·java-ee·gc
whatever who cares7 小时前
在Java/Android中,List的属性和方法
android·java