簡體   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