繁体   English   中英

Excel VBA-单元格颜色更改

[英]Excel VBA - Cell color Change

我正在尝试编写一个宏,该宏根据L列中的值更改颜色。如果L列中的单元格为YES,则Red突出显示B列单元格。 但是,下面的宏不起作用或失败。 它运行但不执行任何操作。

   Sub ColorMeElmo()
   Dim i As Long, r1 As Range, r2 As Range
   For i = 2 To 5
   Set r1 = Range("L" & i)
   Set r2 = Range("B" & i & ":B" & i)
   If r1.Value = "YES" Then r2.Interior.Color = vbRed
   Next i
   End Sub 

将其放在您要观看的表格中。

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim CCell As Range
    Dim sht As Worksheet
    Set CCell = Range("L:L")
    Set sht = Worksheets("Sheet1") 'EDIT
    If Target.Count = 1 Then
        If Not Application.Intersect(CCell, Range(Target.Address)) _
            Is Nothing Then
                If Target.Value = "YES" Then
                    sht.Cells(Target.Row, 2).Interior.Color = RGB(255, 0, 0)
                End If
        End If
    End If
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim CCell As Range
    Dim sht As Worksheet
    Set CCell = Range("L:L")
    Set sht = Worksheets("SheetName") 
    If Target.Count = 1 Then
        If Not Application.Intersect(CCell, Range(Target.Address)) _
            Is Nothing Then
                If Target.Value = "YES" Then
                    sht.Cells(Target.Row, 2).Interior.Color = RGB(255, 0, 0)
                End If
        End If
    End If
End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM