Add Conditional formatting via VBA to a multiple sheets

Schturman

Board Regular
Joined
May 28, 2022
Messages
61
Office Version
  1. 2019
Platform
  1. Windows
Hello
I trying to add conditional format to a multiple sheets in the same cells
It should appear like this in each sheet:
1727291511275.png


I created this code in module, but something wrong here... Probably with the formula that I need to add...
VBA Code:
Option Explicit

Sub AddConForm()

Dim rng As Range
Set rng = ThisWorkbook.Sheets("januar 04-09,januar 11-16").Range("$D$7:$H$12,$D$16:$H$21,$D$25:$H$30,$D$34:$H$39,$D$43:$H$48,$D$52:$H$54")
 With rng
  .FormatConditions.Add Type:=xlExpression, Formula1:="=AND(OR(D7=$H$3,D7=$H$3&"*",D7=$H$3&"**"),D7>"")"
  .Interior.Color = RGB(0, 176, 240)
 End With
 
End Sub
Can someone help to fix it ?
I have 52 sheets with names like january 04-09, january 11-16 etc... I also have a list (by formula) of their names if it can help, like this:
january 04-09
january 11-16


The cells in each sheet is:
Code:
=$D$7:$H$12,$D$16:$H$21,$D$25:$H$30,$D$34:$H$39,$D$43:$H$48,$D$52:$H$54
The color is: #00B0F0 /RGB(0, 176, 240)
Thanks
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
this?
VBA Code:
Option Explicit

Sub RunCF()
Dim ws As Worksheet
    For Each ws In ThisWorkbook.Worksheets
        ws.Select

Dim rng As Range
Set rng = ActiveSheet.Range("$D$7:$H$12,$D$16:$H$21,$D$25:$H$30,$D$34:$H$39,$D$43:$H$48,$D$52:$H$54")
 With rng
  .FormatConditions.Add Type:=xlExpression, Formula1:="=AND(OR(D7=$H$3,D7=$H$3&""*"",D7=$H$3&""**""),D7>"""")"
  .Interior.Color = RGB(0, 176, 240)
 End With

 Next ws
    
End Sub
 
Upvote 0
this?
VBA Code:
Option Explicit

Sub RunCF()
Dim ws As Worksheet
    For Each ws In ThisWorkbook.Worksheets
        ws.Select

Dim rng As Range
Set rng = ActiveSheet.Range("$D$7:$H$12,$D$16:$H$21,$D$25:$H$30,$D$34:$H$39,$D$43:$H$48,$D$52:$H$54")
 With rng
  .FormatConditions.Add Type:=xlExpression, Formula1:="=AND(OR(D7=$H$3,D7=$H$3&""*"",D7=$H$3&""**""),D7>"""")"
  .Interior.Color = RGB(0, 176, 240)
 End With

 Next ws
   
End Sub
Thanks, it's cool and worked on all sheets, but it not added color to conditional formatting, instead it added color to cells (see screenshot):
1727300567726.png
 
Upvote 0
VBA Code:
Sub RunCF()
Dim ws As Worksheet
    For Each ws In ThisWorkbook.Worksheets
        ws.Select

Dim rng As Range
Set rng = ActiveSheet.Range("$D$7:$H$12,$D$16:$H$21,$D$25:$H$30,$D$34:$H$39,$D$43:$H$48,$D$52:$H$54")

With rng
 .FormatConditions.Add Type:=xlExpression, Formula1:= _
        "=AND(OR(D7=$H$3,D7=$H$3&""*"",D7=$H$3&""**""),D7>"""")"
   End With
    With rng.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = RGB(0, 176, 240)
        .TintAndShade = 0
    End With
 Next ws
    
End Sub
 
Upvote 0
VBA Code:
Sub RunCF()
Dim ws As Worksheet
    For Each ws In ThisWorkbook.Worksheets
        ws.Select

Dim rng As Range
Set rng = ActiveSheet.Range("$D$7:$H$12,$D$16:$H$21,$D$25:$H$30,$D$34:$H$39,$D$43:$H$48,$D$52:$H$54")

With rng
 .FormatConditions.Add Type:=xlExpression, Formula1:= _
        "=AND(OR(D7=$H$3,D7=$H$3&""*"",D7=$H$3&""**""),D7>"""")"
   End With
    With rng.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = RGB(0, 176, 240)
        .TintAndShade = 0
    End With
 Next ws
   
End Sub
Now it colorized another existed rule and added new rule to the end :)
1727304598313.png


I fixed it like this:

VBA Code:
Option Explicit

Sub RunCF()
Dim ws As Worksheet
    For Each ws In ThisWorkbook.Worksheets
        ws.Select

Range("$D$7:$H$12,$D$16:$H$21,$D$25:$H$30,$D$34:$H$39,$D$43:$H$48,$D$52:$H$54").Select
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
        "=AND(OR(D7=$H$3,D7=$H$3&""*"",D7=$H$3&""**""),D7>"""")"
        Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
        Selection.FormatConditions(1).StopIfTrue = False
         With Selection.FormatConditions(1).Interior
          .PatternColorIndex = xlAutomatic
          .Color = RGB(0, 176, 240)
          .TintAndShade = 0
         End With

 Next ws

End Sub
Now it add new rule exactly like I want...
Now I have another question..
This code add new rule to ALL sheets, how to change it to a few specific sheets or if I want that code will add new rule from specific sheet till the end ?
 
Upvote 0

Forum statistics

Threads
1,221,893
Messages
6,162,662
Members
451,781
Latest member
DylantheD

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