Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel VBA基于单元格值更改行颜色_Excel_Vba - Fatal编程技术网

Excel VBA基于单元格值更改行颜色

Excel VBA基于单元格值更改行颜色,excel,vba,Excel,Vba,我正在尝试自动化一个大型报告,其中一个步骤是根据B列中的值更改行颜色 基本上,如果B#=“SCC NUPSFTPDE”,那么我需要行颜色为浅蓝色。(我不太关心TBH的确切颜色) 我一直在尝试操作代码,并且基本上已经制作了我自己的弗兰肯斯坦代码,所以我确信它在这里的某个地方是错误的。请帮忙 Dim LastRow As Long Dim cell As Range sSheetName = ActiveSheet.Name With Worksheets(sSheetName) LastR

我正在尝试自动化一个大型报告,其中一个步骤是根据B列中的值更改行颜色

基本上,如果B#=“SCC NUPSFTPDE”,那么我需要行颜色为浅蓝色。(我不太关心TBH的确切颜色)

我一直在尝试操作代码,并且基本上已经制作了我自己的弗兰肯斯坦代码,所以我确信它在这里的某个地方是错误的。请帮忙

Dim LastRow As Long
Dim cell As Range
sSheetName = ActiveSheet.Name
With Worksheets(sSheetName)
    LastRow = .Cells(.Rows.Count, TEST_COLUMN).End(xlUp).Row
    **For Each cell In Range("B2:B" & LastRow)
    If cell.Value = "SCC NUPSFTPDE" Then
        ColorRow = 39**
    Else
        cell.EntireRow.Interior.ColorIndex = xlNone
    End If
Next
End With

只是想结束这个问题:改变

ColorRow = 39

或者更好,比如

cell.EntireRow.Interior.Color = RGB(129, 218, 239)

您也可以尝试工作表事件-
工作表更改
,它会在每次更改中自动应用颜色

Private Sub Worksheet_Change(ByVal Target As Range)

    Dim cell As Range
    Dim LastRow As Long

    With Me

        LastRow = .Cells(.Rows.Count, "B").End(xlUp).Row

        If Not Intersect(Target, .Range("B2:B" & LastRow)) Is Nothing Then

            For Each cell In Target

                Application.EnableEvents = False

                    If cell.Value = "SCC NUPSFTPDE" Then
                        cell.EntireRow.Interior.ColorIndex = 39
                    Else
                        cell.EntireRow.Interior.ColorIndex = xlNone
                    End If

                Application.EnableEvents = True

            Next cell

        End If

    End With

End Sub

ColorRow=39
->
cell.EntireRow.Interior.ColorIndex=39
?或
.Interior.Color=RGB(129、218、239)
要获得浅蓝色,请根据需要使用RGB。谢谢!成功了!
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim cell As Range
    Dim LastRow As Long

    With Me

        LastRow = .Cells(.Rows.Count, "B").End(xlUp).Row

        If Not Intersect(Target, .Range("B2:B" & LastRow)) Is Nothing Then

            For Each cell In Target

                Application.EnableEvents = False

                    If cell.Value = "SCC NUPSFTPDE" Then
                        cell.EntireRow.Interior.ColorIndex = 39
                    Else
                        cell.EntireRow.Interior.ColorIndex = xlNone
                    End If

                Application.EnableEvents = True

            Next cell

        End If

    End With

End Sub