繁体   English   中英

VBA 根据单元格值更改行颜色

[英]VBA Change row color based on cell value

我正在尝试自动化大量报告,该过程的一个步骤涉及根据 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.ColorIndex = 39

或者也许更好,比如

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

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

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

暂无
暂无

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

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