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

时间:2020-03-11 14:10:08

标签: excel vba

我正在尝试自动化大量报告,该过程的一个步骤涉及根据B列中的值更改行颜色。

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

我一直在尝试操作代码,基本上已经编写了自己的科学怪人代码,所以我确定这里的某个地方是错误的。请帮忙!

2020-03-11 05:54:26,174 JMXINSTRUMENTS-Threading [{"timestamp":"1583906066","label":"Threading","ObjectName":"java.lang:type\u003dThreading","attributes":[{"name":"CurrentThreadUserTime","value":18600000000},{"name":"ThreadCount","value":152},{"name":"TotalStartedThreadCount","value":1138},{"name":"CurrentThreadCpuTime","value":20804323112},{"name":"PeakThreadCount","value":164},{"name":"DaemonThreadCount","value":136}]}]

2 个答案:

答案 0 :(得分:1)

只需结束这个问题即可:更改

ColorRow = 39

cell.EntireRow.Interior.ColorIndex = 39

也许更好,例如

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

答案 1 :(得分:0)

您还可以尝试工作表事件-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