VBA Conditional Formatting with xlExpression

Josh_197

New Member
Joined
Dec 15, 2021
Messages
2
Office Version
  1. 2016
Platform
  1. Windows
Hi -

I have written the following code to try and create a toggle each time the code is executed for conditional formatting. I am running into an error and have tried a few different variations of this code. Any advice is greatly appreciated.


Sub change_alternateshading()

Select Case Selection.FormatConditions.Add Type:=xlExpression

Case "=MOD(ROW(),2)"
Selection.FormatConditions.Delete

Case Else
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=MOD(ROW(),2)"
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 Select
End Sub


Thanks!
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Hi & welcome to MrExcel.
Will you only ever have one conditional format rule, on the cells you select?
 
Upvote 0
You could try this on a copy of your workbook
VBA Code:
Sub Josh()
   Dim i As Long
   Dim Flg As Boolean
   
   With Selection
      For i = 1 To .FormatConditions.Count
         If .FormatConditions(i).Formula1 = "=MOD(ROW(),2)" Then
            .FormatConditions(i).Delete
            Flg = True
         End If
      Next i
      If Not Flg Then
         .FormatConditions.Add Type:=xlExpression, Formula1:="=MOD(ROW(),2)"
         .FormatConditions(.FormatConditions.Count).SetFirstPriority
         With .FormatConditions(1).Interior
            .PatternColorIndex = xlAutomatic
            .ThemeColor = xlThemeColorDark1
            .TintAndShade = -4.99893185216834E-02
         End With
      End If
   End With
End Sub
Although you could easily get some very odd results, so I would not really recommend it.
 
Upvote 0

Forum statistics

Threads
1,214,908
Messages
6,122,187
Members
449,071
Latest member
cdnMech

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