How to Color Alternate Row for Merged Cells in Excel

If you work with large data sets in Microsoft Excel, you may find it helpful to format alternate rows with a different color. This can make it easier to scan the data and spot patterns or trends. You can use VBA code to apply alternative row colors for merged cells quite easily. In this article, I will show you how to alternate the row color for merged cells in Excel with ease. So, without having any further discussion, let’s begin.


How to Color Alternate Row for Merged Cells in Excel: Easy Steps

In the following dataset, the left-most column, Category has various product category names. There I used the Merge Cells command to merge consecutive rows. The merging of rows doesn’t have any particular pattern in row numbers. I will use this dataset to show you apply alternative the row color in Excel.


Step-1: Open Visual Basic Editor

I will use a VBA code to apply the alternative row color scheme for merged cells in Excel. To use the VBA code, you need to open the Visual Basic Editor first.

For that,

ā¶ Go to the Developer tab in the ribbon.

ā· Now click on the Visual Basic command in the Code group.

This will open the Visual Basic Editor straightway.

Alternatively, you can press the ALT + F11 keys to open the Visual Basic Editor.

Open VBA Editor to Color Alternate Row for Merged Cells in Excel


Step-2: Insert VBA Code in a New Module

Now you need to open a new module to insert the VBA code. To open a new Module,

ā¶ Go to the Insert tab.

ā· Select the Module command from the drop-down list.

Open New Module to Color Alternate Row for Merged Cells in Excel

āø Now copy the following VBA code.

Sub Alternate_Colors()

Ā  Dim xRw As Long
Ā  Dim xCnt As Long
Ā  Dim xColr As Long

Ā  With Range("B5").CurrentRegion
Ā Ā Ā  .EntireColumn.Interior.Color = xlNone
Ā Ā Ā  .EntireColumn.Borders.LineStyle = xlNone
Ā Ā Ā  .Columns.Borders(xlInsideVertical).Weight = xlThin
Ā Ā Ā  .Columns.Borders(xlInsideHorizontal).Weight = xlThin
Ā Ā Ā  .BorderAround , xlThin
Ā Ā Ā  xColr = RGB(233, 237, 244)
Ā Ā Ā  Do
Ā Ā Ā Ā Ā  xColr = RGB(233, 237, 244) + RGB(208, 216, 232) - xColr
Ā Ā Ā Ā Ā  xCnt = Cells(xRw + 5, "B").MergeArea.Rows.Count
Ā Ā Ā Ā Ā  Cells(xRw + 5, "B").Resize(xCnt, .Columns.Count).Interior.Color = xColr
Ā Ā Ā Ā Ā  xRw = xRw + xCnt
Ā Ā Ā  Loop While xRw < .Rows.Count - 1
Ā  End With

End Sub

ā¹ Paste it to the newly opened module.

Inserting VBA code to Color Alternate Row for Merged Cells in Excel


Step-3: Save Workbook as Macro-Enabled Workbook

To save the workbook with the VBA code,

ā¶ Go to the File tab in the ribbon.

ā· Then select the Save command.

Or you can press the CTRL + S keys together.

āø Next click No in the pop-up dialog box to proceed with the VBA code.

ā¹ Now select Excel Macro-Enabled Workbook (*.xlsm) option in the Save as type drop-down.

Color Alternate Row for Merged Cells in Excel


Step-4: Run VBA Code

Now all you need to do is run the code. To run the VBA code,

ā¶ Go to the Developer tab.

ā· Select the Macros command in the Code group.

Or you can press the ALT + F8 keys to open the Macro dialog box.

āø Next, click on the Run button in the Macro dialog box.

Thus, the VBA code will run. This code will alternate the row color for the merged cells just like in the following picture.

Color Alternate Row for Merged Cells in Excel


Practice Section

You will get an Excel sheet like the following screenshot, at the end of the provided Excel file where you can practice all the topics discussed in this article.


Download Practice Workbook

You can download the Excel file from the following link and practice along with it.


Conclusion

I have discussed steps to alternate row colors for merged cells in Excel. Please don’t hesitate to ask any questions in the comment section below. We will try to respond to all the relevant queries ASAP.


Related Articles


<< Go Back to Alternating Row Colors |Ā Highlight RowĀ |Ā Highlight in Excel | Learn Excel

Get FREE Advanced Excel Exercises with Solutions!
Mrinmoy Roy
Mrinmoy Roy

Mrinmoy Roy, a dedicated professional with a BSc in Electronics and Communication Engineering from Khulna University of Engineering & Technology, Bangladesh, brings over two years of expertise to the ExcelDemy project. As a prolific contributor, he has authored around 180 articles, showcasing his deep knowledge and passion for Microsoft Excel, Data Analysis, and VBA. His unwavering commitment to continuous learning, combined with versatile skills, renders him well-suited for roles in data management and spreadsheet solutions. He has interest... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo