Link to home
Start Free TrialLog in
Avatar of fb1990
fb1990

asked on

VBA - Code to add color background to a row

Hello Expert,
Can someone please help me with an Excel VBA snippet to add background color to a header?  I want the background color to only be applied to cells where there are values.  The number of columns can change.


Thanks.

ASKER CERTIFIED SOLUTION
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of fb1990
fb1990

ASKER

@Martin Liss.  Thanks for this solution. My header is on row 4.  Can you please help modify this code to apply the background color to row 4 starting from column A.
In this code
If Not IsEmpty(Cells(1, lngCol)) Then
        Cells(1, lngCol).Interior.Color = RGB(255, 105, 180)
    End If
Next

Open in new window

The 1s are the row so change them to
If Not IsEmpty(Cells(4, lngCol)) Then
        Cells(4, lngCol).Interior.Color = RGB(255, 105, 180)
    End If
Next

Open in new window

You’re welcome and I’m glad I was able to help.

If you expand the “Full Biography" section of my profile you’ll find links to some articles I’ve written that may interest you, including a new one concerning Easy Ways to do Hard Things.

Marty - Microsoft MVP 2009 to 2017
        Experts Exchange Most Valuable Expert (MVE) 2015 and 2017
        Experts Exchange Distinguished Expert in Excel 2018, 2021, 2022
        Experts Exchange Distinguished Expert in Microsoft Office 2022
        Experts Exchange Distinguished Expert in VBA 2022
        Experts Exchange Top Expert VBA 2018 to 2022
        Experts Exchange Top Expert Visual Basic Classic 2012 to 2022
Avatar of fb1990

ASKER

Thanks for sharing your knowledge with me.