» Row and column background color using VBA in Microsoft Excel
VBA macro tip contributed by Erlandsen Data Consulting offering Microsoft Excel Application development, template customization, support and training solutions
CATEGORY - Cells, Ranges, Rows, and Columns in VBA
VERSION - All Microsoft Excel Versions
Sub ShadeAlternateRows(rngTarget As Range, intColor As Integer, lngStep As Long)
' adds a background color = intColor to every lngStep rows in rngTarget
' example: ShadeAlternateRows Range("A1:D50"), 27, 2
' colors every 2 rows light yellow
Dim r As Long
If rngTarget Is Nothing Then Exit Sub
With rngTarget
.Interior.ColorIndex = xlColorIndexNone
' remove any previous shading
For r = lngStep To .Rows.Count Step lngStep
.Rows(r).Interior.ColorIndex = intColor
Next r
End With
End Sub
Sub ShadeAlternateColumns(rngTarget As Range, _
intColor As Integer, lngStep As Long)
' adds a background color = intColor to every lngStep column in rngTarget
' example: ShadeAlternateColumns Range("A1:J20"), 27, 2
' colors every 2 columns light yellow
Dim c As Long
If rngTarget Is Nothing Then Exit Sub
With rngTarget
.Interior.ColorIndex = xlColorIndexNone
' remove any previous shading
For c = lngStep To .Columns.Count Step lngStep
.Columns(c).Interior.ColorIndex = intColor
Next c
End With
End Sub
Book Store:
Recommended Books:
- Investing for Dummies, Third Edition
- Financial Statements: A Step-By-Step Guide to Understanding and Creating Financial Reports
- Lower Your Taxes - Big Time! : Wealth-Building, Tax Reduction Secrets from an IRS Insider
- Managing by the Numbers: A Commonsense Guide to Understanding and Using Your Company's Financials: An Essential Resource for Growing Businesses
- Who Moved My Cheese? An Amazing Way to Deal with Change in Your Work and in Your Life
- Your First Business Plan: A Simple Question and Answer Format Designed to Help You Write Your Own Plan (3rd Ed)
No comments have been submitted.

