【问题标题】:EXCEL: Click One Cell and Highlight AnotherEXCEL:单击一个单元格并突出显示另一个
【发布时间】:2020-04-29 04:32:55
【问题描述】:
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    With Sheets("test")
     .Cells.Interior.ColorIndex = xlColorIndexNone
       Select Case Target.Address
        Case "$D$3" 
         .Range("D3").Interior.Color = RGB(195, 195, 195)
         .Range("J3").Interior.Color = RGB(195, 195, 195)
         .Range("V3").Interior.Color = RGB(195, 195, 195)
         Case "$J$3" 
         .Range("D3").Interior.Color = RGB(195, 195, 195)
         .Range("J3").Interior.Color = RGB(195, 195, 195)
         .Range("V3").Interior.Color = RGB(195, 195, 195)
         Case "$V$3" 
         .Range("D3").Interior.Color = RGB(195, 195, 195)
         .Range("J3").Interior.Color = RGB(195, 195, 195)
         .Range("V3").Interior.Color = RGB(195, 195, 195)


        End Select
    End With
End Sub

这段代码非常大而且很菜鸟。

可以像 Case "$D$3:"$J$3:"$V$3" 一样编辑此代码“Case "$D$3" - 它不起作用

还有这个:.Range("D3").Interior.Color = RGB(195, 195, 195)

像 D3:J3:P3 - 不好用

【问题讨论】:

    标签: excel cell highlight


    【解决方案1】:

    试试这个代码。

    阅读代码中的 cmets 并根据您的需要进行调整:

    Option Explicit
    
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    
        Dim evalRange As Range
        Dim highlightRange As Range
    
        ' Set the ranges addresses like this:
        '   if cells are contigous use ":" e.g. D3:E5 would evaluate D3, D4, D5, E3, E4 and E5
        '   if cells are non contigous use "," to separate each cell
        Set evalRange = Me.Range("D3,J3,V3")
        Set highlightRange = Me.Range("D3,J3,V3")
    
        ' This next line will remove the background of all the cells in the current sheet
        ' You can use "Me" to refer to the current sheet
        Me.UsedRange.Cells.Interior.ColorIndex = xlColorIndexNone
    
        ' We check if the Target which is the cell or cells selected intersects with the evaluated range defined at the beginning
        If Not Intersect(Target, evalRange) Is Nothing Then
            ' If it does, then we set the background color to all cells in the highlight range defined at the beginning
            highlightRange.Interior.Color = RGB(195, 195, 195)
        End If
    
    End Sub
    

    【讨论】:

      【解决方案2】:

      以下是对您的语法稍作改动的代码:

      使用 Intersect 功能检查和 IIF 用于 Toogle

          Option Explicit
          Private Sub Worksheet_SelectionChange(ByVal Target As Range)
              Dim isIntersect As Range
              Dim Listrange As Variant
              Listrange = Array("D3, J3, V3","D5, J4, V8")
              'Listrange = Array("D3, J3, V3","D5, J4, V8","....") ex:add another range
              Dim i As Integer
              For i = 0 To UBound(Listrange)
              With Range(Listrange(i))
                  Set isIntersect = Intersect(Target, .Cells)
                  .Interior.Color = IIf(isIntersect Is Nothing, xlNone, RGB(195, 195, 195))
              End With
      
              Next i
          End Sub
      

      注意:您可以使用 RGB(255, 195, 255) 或 16777215 代替 xlNone

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-01-21
        • 1970-01-01
        • 2020-06-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多