Formatting alternate row colours when some rows are merged in Column A

TheRedCardinal

Board Regular
Joined
Jul 11, 2019
Messages
243
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
I have a variation on the below code which colours my rows different shades of blue.

VBA Code:
Sub StripeyPresentation()

Dim Rng As Range
Set Rng = Selection

With Rng.FormatConditions.Add(Type:=xlExpression, Formula1:="=MOD(ROW(),2)=0")
    .Interior.Color = RGB(208, 216, 232)
    .Borders.LineStyle = xlContinuous
    .Borders.ThemeColor = 1
    .Borders.Weight = xlThin
End With

With Rng.FormatConditions.Add(Type:=xlExpression, Formula1:="=MOD(ROW(),2)=1")

    .Interior.Color = RGB(233, 237, 244)
    .Borders.LineStyle = xlContinuous
    .Borders.ThemeColor = 1
    .Borders.Weight = xlThin

End With

End Sub

My problem is some of my cells in the first column are merged, the pattern is not fixed and nor is the number of merged cells. But I want all of the subsequent columns to be the same colour as the cell in column A.

Here is a picture:

1580516405752.png


So row 1 is good.

Row A2:A4 are merged and one colour, but B2:E4 have alternate colours.

Is my request even possible?
 

Attachments

  • 1580516402161.png
    1580516402161.png
    19.8 KB · Views: 14

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Try this Conditional Formatting (converted to vba if required)

A_MrExcel.xlsm
ABCDE
11datadatadatadata
22datadatadatadata
3datadatadatadata
4datadatadatadata
53datadatadatadata
64datadatadatadata
75datadatadatadata
88datadatadatadata
9datadatadatadata
10datadatadatadata
11datadatadatadata
12datadatadatadata
Shade rows
Cells with Conditional Formatting
CellConditionCell FormatStop If True
A1:E12Expression=MOD(COUNTA($A$1:$A1),2)=0textYES
A1:E12Expression=MOD(COUNTA($A$1:$A1),2)=1textYES
 
Upvote 0
Try this Conditional Formatting (converted to vba if required)

A_MrExcel.xlsm
ABCDE
11datadatadatadata
22datadatadatadata
3datadatadatadata
4datadatadatadata
53datadatadatadata
64datadatadatadata
75datadatadatadata
88datadatadatadata
9datadatadatadata
10datadatadatadata
11datadatadatadata
12datadatadatadata
Shade rows
Cells with Conditional Formatting
CellConditionCell FormatStop If True
A1:E12Expression=MOD(COUNTA($A$1:$A1),2)=0textYES
A1:E12Expression=MOD(COUNTA($A$1:$A1),2)=1textYES

That looks right to me thanks!

I'll have a go at converting it to VBA!
 
Upvote 0
Could be something like this.

VBA Code:
Sub StripeyPresentation_v2()
  Dim Rng As Range
  
  Set Rng = Range("A1").CurrentRegion
  With Rng
    .FormatConditions.Delete
    With .FormatConditions.Add(Type:=xlExpression, Formula1:="=MOD(COUNTA(" & Rng.Cells(1).Address & ":" & Rng.Cells(1).Address(0, 1) & "),2)=0")
      .Interior.Color = RGB(208, 216, 232)
      .Borders.LineStyle = xlContinuous
      .Borders.ThemeColor = 1
      .Borders.Weight = xlThin
    End With
    With .FormatConditions.Add(Type:=xlExpression, Formula1:="=MOD(COUNTA(" & Rng.Cells(1).Address & ":" & Rng.Cells(1).Address(0, 1) & "),2)=1")
      .Interior.Color = RGB(233, 237, 244)
      .Borders.LineStyle = xlContinuous
      .Borders.ThemeColor = 1
      .Borders.Weight = xlThin
    End With
  End With
End Sub
 
Upvote 0
If you are looking for a straight (direct) VBA macro that colors the cell only when you run it...
VBA Code:
Sub AlternateColorsWithMergeAreas()
  Dim Rw As Long, Cnt As Long, Colr As Long
  With Range("A1").CurrentRegion
    .EntireColumn.Interior.Color = xlNone
    .EntireColumn.Borders.LineStyle = xlNone
    .Columns.Borders(xlInsideVertical).Weight = xlThin
    .Columns.Borders(xlInsideHorizontal).Weight = xlThin
    .BorderAround , xlThin
    Colr = RGB(208, 216, 232)
    Do
      Colr = RGB(208, 216, 232) + RGB(233, 237, 244) - Colr
      Cnt = Cells(Rw + 1, "A").MergeArea.Rows.Count
      Cells(Rw + 1, "A").Resize(Cnt, .Columns.Count).Interior.Color = Colr
      Rw = Rw + Cnt
    Loop While Rw < .Rows.Count
  End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,991
Messages
6,122,628
Members
449,095
Latest member
bsb1122

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top