Macro For Alternating row colour using Conditional Formatting

Answer_the_question

New Member
Joined
Nov 19, 2015
Messages
17
I am very inexperienced with VBA (essentially only know to use the record tool and google everything else), but I am trying to create a macro to alternate row colours in a selected range. I am currently using conditional formatting and this is what the code looks like right now

Sub Row_Shadding()
'
' Row_Shadding Macro
'


'
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=MOD(SUBTOTAL(103,$E$5:$E5),2)=0"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorDark1
.TintAndShade = -4.99893185216834E-02
End With
Selection.FormatConditions(1).StopIfTrue = False
End Sub


I use the subtotal function so that the conditional formatting will still work when I group rows, but I dont think this will work if i have a column in the counta range that has a blank cell.

Anyways the really issue is that I can't get the subtotal function to use the top left cell of my selection in the formula (currently have to manually change it to the correct cell after running the macro)

The conditional formatting also appears to only work on the number of rows I used during the initial recording (ie if when I recorded the macro there were 8 rows in the test table, when i apply the macro to a new table it only works on the first 8 rows even if there are 20 rows)

Any help would be greatly appreciated!

Thank you
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.

Forum statistics

Threads
1,214,599
Messages
6,120,448
Members
448,966
Latest member
DannyC96

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