Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

clear background color of every row in a worksheet with the

0

I'm still new to Excel VBA, so forgive me if this is a simple question.

I have a variable length (rows) excel spreadsheet where cells may be color-coded (fill color). When I enter the worksheet I would like to clear any background fill colors, but only for certain rows. The first column (A) contains a "1" if that is a row that I want to clear the background color of. So I need to loop thru the entire worksheet.

Sub Clear_Rows()
Dim icell As Range
For Each icell In Range("A:A")
       If icell.Value = 1 Then ActiveCell.Row.Interior.ColorIndex = -4142
Next icell
End Sub

I've played around with something like this for days. This last version, I'm getting an "Invalid Qualifier" error message on the ActiveCell.Row.

Thanks in advance

Charlie

Answer
Discuss

Answers

0
Selected Answer

Hello CharlieB and welcome to the forum.

Don't think of it as a simple question. If you don't ask you will not learn; and with Excel and VBA the learning never stops

You've got the right idea but some wrong syntax. Also, your code has the macro checking every cell in column "A". It is better and faster to only check the used cells. To that end you can use this code:

Sub New_Clear_Rows()

' macro written by WillieD24 for teachexcel.com
' this will remove entire row fill colour based on cell value

Dim LR_A As Long
Dim i As Long

' find last used row in column "A"
LR_A = Cells(Rows.Count, 1).End(xlUp).Row   ' find last used row
i = 1   ' used for row number
Application.ScreenUpdating = False   ' allows macro to run faster
Do Until i > LR_A ' only check used cells - faster than entire column
    If Cells(i, 1).Value = 1 Then
        Rows(i).Interior.ColorIndex = xlNone
    End If
    i = i + 1 ' increment row number
Loop
Application.ScreenUpdating = True
End Sub

Update June 13/23 :

While waiting for you to respond I corrected the minor errors in your code (changes in bold)

Sub Clear_Rows_Updated()

' macro updated by WillieD24 for teachexcel.com
' this will remove entire row fill colour based on cell value

Dim icell As Range
Dim R As Long

R = 1
Set icell = Cells(R, 1)

For Each icell In Range("A:A")
       If icell.Value = 1 Then
       Rows(R).Interior.ColorIndex = -4142
       End If
       R = R + 1
' this next line was added as a "stop" - replace "10" with the row number of your last used row
If R > 10 Then Exit Sub

Next icell

End Sub

I have also attached a sample file with Sheet1 using my code and Sheet2 using the updated version of your code. (just click the "buttons" on each sheet)

Hope this helps; if so please mark my answer as selected.

Cheers   :-)

Discuss

Discussion

Thank you so much for the help.  I tried it and, of course, it works! I'll need to study it a bit to make sure I understand it, but your comments were of immense help too. Thanks again.
CharlieB (rep: 4) Jun 13, '23 at 8:23 pm
Glad I could help. Thanks for selecting my answer.

Cheers   :-)
WillieD24 (rep: 557) Jun 13, '23 at 8:43 pm
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login