Quantcast
Channel: An Excel Blog For The Real World
Viewing all articles
Browse latest Browse all 117

How To Alternate Row Colors in Excel

$
0
0
Excel Alternate Row Colors on Cell Range

Implementing Row Banding (Alternating Colors)

While Excel does not have a dedicated row banding button to alternate row colors, there are a few creative ways we can obtain this effect. In this article we will walk through three solutions that will provide a way forward for how you might want to accomplish this:

  1. Using an Excel Table

  2. Using Conditional Formatting

  3. Using a VBA Macro

Excel Alternate Row Colors on Cell Range

If you are


Free Article Example File Download

If you would like to download the example file that covers all methods used within this article please click the below button to gain access to my Example Files download page. As with all my downloadable files here on TheSpreadsheetGuru, you need to be a member of my Excel Tips Newsletter. You can get signed up quickly by clicking the button below.

  ⭳ Download Example File

Only my newsletter subscribers get access to example files.
Click the Download button above to quickly sign-up for free!

 

Method 1: Utilize An Excel Table

An Excel Table is an object you can insert to allow for your data to be dynamically referenced throughout your spreadsheet. There are limitations that come with the Table object (such as every column has to have a unique heading), but if you can live with some of the restrictions, this is a great way to alternate row colors automatically.

Converting your spreadsheet range to a table object is as easy as

  1. Select your data range

  2. Navigate to the Insert Tab on your Ribbon Menu

  3. Click the Table Button

  4. Tell the dialog box if your selection included headers

  5. Click OK

Alternatively, you can select your data range and use the keyboard shortcut Ctrl + t to get to the insert Table dialog box.

If you do not like one of the default Table Style options, you can create your own Table format to get specific row colors. Just click New Table Style at the very bottom of the Table Styles gallery in Excel Ribbon.

Method 2: Conditional Formatting

If you don’t want to utilize an Excel table, you can alternatively utilize conditional formatting rules to get alternating colors.

The only disadvantage to this method is (as of this writing) we cannot reference named ranges in conditional formatting rules. This means if you add a new row to the bottom of the table at a later date, you will need to update the conditional formatting data range to ensure the new row is included in the rule. Note, if you insert a row inside your currently formatted rows, the conditional formatting rule will automatically adjust to include it.

To create this color banding, you will need to select the cell range you are targeting and add a new Conditional Formatting rule.

  1. Navigate to the Home Tab

  2. Click the Conditional Formatting menu button

  3. Select New Rule…

  4. In the New Formatting Rule dialog box, select “Use a formula to determine which cells to format

  5. Enter one of the MOD functional rules noted below:

    • For even-numbered rows, you’ll want to use the following formula: =MOD(ROW(),2)=0

    • For odd-numbered rows, you’ll want to use the following formula: =MOD(ROW(),2)>0

  6. Click the Format Button and select the specific formatting you wish to apply (I just chose a fill color of gray)

  7. Click OK Button

  8. Repeat steps 1-7 for the secondary color

Conditional Formatting to Alternate Colors

Once you’ve created both your primary and secondary banding conditional formatting rules, you should see the alternating colors automatically apply.

Method 3: VBA Coded Macro

Maybe you are looking for a solution that is more ad-hoc or on-demand. For times where you want to quickly format a table before sending it off to the executives, having a personal macro that can band your data in a pinch might be the right answer for your needs.

VBA Macro Code To Alternate Row Colors

The below code allows you to select a range on your spreadsheet and quickly alternate two different colors across the rows. Just programmatically define the two color codes you wish to use (you can reference VB colors or an RGB color code for more flexibility/variety) at the beginning of the code. The remaining VBA code loops through each row and alternates the fill colors applied based on odd/even rows. If you would like to learn a bit more about the technique used to determine odd/even rows, you can check out this article: VBA To Determine If Number Is Odd Or Even.

Sub AlternateRowColors()
'PURPOSE: Alternate row fill colors based on selected range
'SOURCE: www.TheSpreadsheetGuru.com/the-code-vault

Dim rng As Range
Dim x As Long
Dim LightColorCode As Long
Dim DarkColorCode As Long

'Define Colors (Input)
  LightColorCode = vbWhite
  DarkColorCode = RGB(242, 242, 242)

'Ensure a Range is Selected
  If TypeName(Selection) <> "Range" Then Exit Sub

'Store Selected range to a variable
  Set rng = Selection

'Check for more than 1 row selected
  If rng.Rows.Count = 1 Then Exit Sub

'Loop through each row in selection and color appropriately
  For x = 1 To rng.Rows.Count
    If x Mod 2 = 0 Then
      rng.Rows(x).Interior.Color = DarkColorCode 'Even Row
    Else
      rng.Rows(x).Interior.Color = LightColorCode 'Odd Row
    End If
  Next x

End Sub

Free Article Example File Download

If you would like to download the example file that covers all methods used within this article please click the below button to gain access to my Example Files download page. As with all my downloadable files here on TheSpreadsheetGuru, you need to be a member of my Excel Tips Newsletter. You can get signed up quickly by clicking the button below.

  ⭳ Download Example File

Only my newsletter subscribers get access to example files.
Click the Download button above to quickly sign-up for free!

 

I Hope This Helped!

Hopefully, I was able to explain how you can use a number of different methods in Excel to alternate row colors in a given cell range. If you have any questions about these techniques or suggestions on how to improve them, please let me know in the comments section below.


About The Author

Hey there! I’m Chris and I run TheSpreadsheetGuru website in my spare time. By day, I’m actually a finance professional who relies on Microsoft Excel quite heavily in the corporate world. I love taking the things I learn in the “real world” and sharing them with everyone here on this site so that you too can become a spreadsheet guru at your company.

Through my years in the corporate world, I’ve been able to pick up on opportunities to make working with Excel better and have built a variety of Excel add-ins, from inserting tickmark symbols to automating copy/pasting from Excel to PowerPoint. If you’d like to keep up to date with the latest Excel news and directly get emailed the most meaningful Excel tips I’ve learned over the years, you can sign up for my free newsletters. I hope I was able to provide you some value today and hope to see you back here soon! - Chris


Viewing all articles
Browse latest Browse all 117