zoukankan      html  css  js  c++  java
  • VBA 实现excel单元格多选

    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
    If Target.Column = 2 Or 3 Or 6 Then '数字是你想要多选的列是多少,多个用or连接。
    Application.Undo
    oldVal = Target.Value
    Target.Value = newVal
    If oldVal = "" Then
    Else
    If newVal = "" Then
    Else '去除重复的字段
    If InStr(1, oldVal, newVal) <> 0 Then
    If InStr(1, oldVal, newVal) + Len(newVal) - 1 = Len(oldVal) Then '最后一个选项重复
    Target.Value = Left(oldVal, Len(oldVal) - Len(newVal) - 1)
    Else
    Target.Value = Replace(oldVal, newVal & ",", "") '不是最后一个选项重复的时候处理逗号
    End If
    Else '不是重复选项就视同增加选项
    Target.Value = oldVal _
    & "," & newVal '可以是任意符号隔开
    End If
    End If
    End If
    End If
    End If
    exitHandler:
    Application.EnableEvents = True
    End Sub

  • 相关阅读:
    《杜教筛》
    《洛谷P4213 【模板】杜教筛(Sum)》
    《洛谷P1829 [国家集训队]Crash的数字表格 / JZPTAB》
    《纸牌问题》
    《洛谷P2522 [HAOI2011]Problem b》
    使用urlretrieve下载图片
    scrapy初探
    爬豆瓣电影名
    直接插入排序
    Windows python 3 安装OpenCV
  • 原文地址:https://www.cnblogs.com/longshanshan/p/14812323.html
Copyright © 2011-2022 走看看