Formula to change color of entire row in Excel 2010

I want to build a macro that will loop through the end of an  Excel file regardless of the number of entries  and change color of each of rows if certain conditions are met. For example,  if column 3 = "X" and the quantity in column 7 <=3, the entire row will be highlighted. This macro must be activated when the worksheet is opened. Thanks.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Saurabh Singh TeotiaCommented:
Just put the following code in the sheet module of whichever worksheet you want this feature for..and it will do what you are looking for when you select or active that worksheet..

So let's say you want this for sheet1 then put this in the sheet1 module...

Private Sub Worksheet_Activate()
    Dim lrow As Long
    lrow = Cells(Cells.Rows.Count, 3).End(xlUp).Row
    I = 1
    Do Until I > lrow

        If UCase(Cells(I, 3).Value) = "X" And Cells(I, 7).Value <= 7 Then Rows(I).Interior.ColorIndex = 3
        I = I + 1


End Sub

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Martin LissOlder than dirtCommented:
Please don't give me any points but there's a typo in Saurab's  answer.

If UCase(Cells(I, 3).Value) = "X" And Cells(I, 7).Value <= 7 Then Rows(I).Interior.ColorIndex = 3

should be

If UCase(Cells(I, 3).Value) = "X" And Cells(I, 7).Value <= 3 Then Rows(I).Interior.ColorIndex = 3
Roy CoxGroup Finance ManagerCommented:
Why not use Conditional Formatting instead, it's automatic and does not require VBA?

Place this formula in the Conditional Formatting Formula Box


Then select the fill colour and set the applies to range.
Samuel SmithCommented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.