Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
187 views
in Technique[技术] by (71.8m points)

excel - Trying to change color of rows to white

Trying to change row color to white. Sorry about that

Sub Change_Row_Color

Dim ws As Worksheet
Dim rng As Range

Set rng = ws.Range("A13:Q299") 
Set ws = ThisWorkbook.Sheets("Job Card Master")

If ws.rng.Cells.ColorIndex = 4 Then
    rng.Cells.ColorIndex = 1        
End If

End sub

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Couple of things

  1. You are using the ws object before initializing it.
  2. Since you want to change it to white, I guess you want 2 instead of 1?

Is this what you are trying?

Option Explicit

Sub Change_Row_Color()
    Dim ws As Worksheet
    Dim rng As Range
    Dim i As Long, j As Long
    
    Set ws = ThisWorkbook.Sheets("Job Card Master")
    Set rng = ws.Range("A13:Q299")
    
    With ws
        For i = 1 To rng.Rows.Count
            For j = 1 To rng.Columns.Count
                If rng.Cells(i, j).Interior.ColorIndex = 4 Then
                    .Range(rng.Cells(i, 1), rng.Cells(i, 17)).Interior.Pattern = xlNone
                    Exit For
                End If
            Next j
        Next i
    End With
End Sub

To change the color of the entire row and not just A to Q, change the line

.Range(rng.Cells(i, 1), rng.Cells(i, 17)).Interior.ColorIndex = 2

to

.Rows(rng.Cells(i, 1).Row).Interior.ColorIndex = 2

NOTE:

Remember, White doesn't mean No Fill. If you are trying to remove the color then try this

.Interior.Pattern = xlNone 

instead of

.Interior.ColorIndex = 2

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

63 comments

56.6k users

...