代码之家  ›  专栏  ›  技术社区  ›  Adriaan

已选择多个大小写的条件格式

  •  1
  • Adriaan  · 技术社区  · 6 年前

    我有一个如下所示的VBA,但当我复制超过1个单元格时,由于多次选择,我会得到一个错误。

    是否可以让案例逐个查看所选单元格?还是我的立场不对?

    Private Sub Worksheet_Change(ByVal Target As Range)
      If Not Intersect(Target, Range("$G$8:$OA$92")) Is Nothing Then
    
        With Target
         Select Case .Value
          Case Is = "Weekend"
              .Interior.ColorIndex = 48
          Case Is = "VRIJ", "ADV"
              .Interior.ColorIndex = 6
         End Select
    End With
    End If
    End Sub
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   user4039065 user4039065    6 年前

    循环遍历相交中的单元格。

    Private Sub Worksheet_Change(ByVal Target As Range)
        If Not Intersect(Target, Range("G8:OA92")) Is Nothing Then
            on error goto meh
            application.enableevents = false
            dim t as range
            for each t in Intersect(Target, Range("G8:OA92"))
                With t
                    Select Case lcase(.Value2)
                        Case "weekend"
                            .Interior.ColorIndex = 48
                        Case "vrij", "adv"
                            .Interior.ColorIndex = 6
                        case else
                            .interior.pattern = xlnone
                    End Select
                End With
            next t 
        End If 
    meh:
        application.enableevents = true
    End Sub