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

设置单选数据

代码粘贴即可

相关推荐
Rain的Java大神之路2 分钟前
面试官:设计一个自动售货机系统。我:别急着写代码,先问5个问题|状态机+防超卖+断电自愈,一套答法讲透
java·后端·面试
挖掘狂人3 分钟前
飞书群聊已读,是怎么实现的?
java·程序员·架构
Brookty8 分钟前
HTML基础知识:表格布局、表单数据提交、元素标识与资源相对路径
前端·html
徐小夕21 分钟前
开源!我们用知识图谱重构了电子档案,AI 驱动全文智能关联
前端·vue.js·github
不一样的少年_43 分钟前
用了这 Skill,前端兄弟都觉得好
前端·后端·ai编程
恋猫de小郭1 小时前
Flutter Native Interop 稳定了,Platform Channel 要退场了
android·前端·flutter
奶油话梅糖1 小时前
容器启停 2.5 毫秒:11 天 1,700 Star 的 Kiri,用一个 900 KB 的 PID 1 干掉了 runc 和 dockerd
前端·人工智能·rust
吴名氏.2 小时前
Java转AI Agent教程(第十五周)政策时效:过期政策不该被召回,也不能被删掉
java·人工智能·python·ai·ai agent
文丑颜不良啊11 小时前
LeetCode2981~3000题目解析
java·leetcode