Run VBA code from specific sheet till the end of workbook

Schturman

Board Regular
Joined
May 28, 2022
Messages
61
Office Version
  1. 2019
Platform
  1. Windows
Hi
I use this code in module to add a conditional format to sheets:
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

It working fine, but it add CF to all existing sheets in the workbook.
Can someone change this part of code:
Code:
Dim ws As Worksheet
    For Each ws In ThisWorkbook.Worksheets
        ws.Select

that will allow me:
Variant 1 - run code from a specific sheet till the end of workbook
Variant 2 - run code from a specific sheet to another specific sheet. For example run ONLY on 3 sheet "January 04-09", "January 11-16", "January 18-23" or from "January 04-09" till "January 18-23"
Thanks
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
This should work.
Add
VBA Code:
If Left(ws.Name, 13) = "January 04-09" Or Left(ws.Name, 13) = "January 11-16" Or Left(ws.Name, 13) = "January 18-23" Then
before
VBA Code:
ws.Select
 
Upvote 0
I would create a small UserForm that would list all of the Sheets and allow me to choose which Sheets to run the Macro.
 
Upvote 0
VBA Code:
Sub RunCF_pre_selected()'VARIANT2'
sheetlist = Array("Sheet1", "Sheet3")
For i = LBound(sheetlist) To UBound(sheetlist)
Worksheets(sheetlist(i)).Activate
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
End Sub
Sub RunCF_from_to_end()'VARIANT1'
Dim sheet As Worksheet

For Each sheet In ActiveWorkbook.Worksheets
  If sheet.Index > 2 Then
 
   sheet.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
    End If
 Next
    
End Sub
 
Upvote 0
This should work.
Add
VBA Code:
If Left(ws.Name, 13) = "January 04-09" Or Left(ws.Name, 13) = "January 11-16" Or Left(ws.Name, 13) = "January 18-23" Then
before
VBA Code:
ws.Select
Cool! Thanks ! It working perfectly for specific sheets. Maybe you also know how to do the same for a range of sheets, for example start from "January 04-09" till "May 01-07" ?
I can use your code like this:
VBA Code:
If Left(ws.Name, 6) = "January" Or Left(ws.Name, 8) = "February" Or Left(ws.Name, 5) = "March" Then

and so on.. but it's longest way, do you know a shorter way to do this ?
Thanks
 
Upvote 0
VBA Code:
Sub RunCF_pre_selected()'VARIANT2'
sheetlist = Array("Sheet1", "Sheet3")
For i = LBound(sheetlist) To UBound(sheetlist)
Worksheets(sheetlist(i)).Activate
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
End Sub
Sub RunCF_from_to_end()'VARIANT1'
Dim sheet As Worksheet

For Each sheet In ActiveWorkbook.Worksheets
  If sheet.Index > 2 Then
 
   sheet.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
    End If
 Next
  
End Sub
Thank you ! Variant 1 worked, I needed only one line: If sheet.Index > 2 Then
Variant 2 not working... Don't know why...
1727405937212.png
 
Upvote 0
Ok, After some tries this part is also working fine:
VBA Code:
Sub RunCF_pre_selected()'VARIANT2'
sheetlist = Array("Sheet1", "Sheet3")
For i = LBound(sheetlist) To UBound(sheetlist)
Worksheets(sheetlist(i)).Activate

but only for specific sheets that I wrote, for example "January 04-09" and "May 01-07" and not between them..

Big thanks to all !
 
Upvote 0
If between means in terms of their position in the workbook, you could use this structure.

VBA Code:
Sub RunCF_between_sheets() 'VARIANT3'
    Dim FromSht As String, ToSht As String
    Dim i As Long

    FromSht = "January 04-09"
    ToSht = "May 01-07"
    
    For i = Worksheets(FromSht).Index To Worksheets(ToSht).Index
        Debug.Print Worksheets(i).Name
    Next i
End Sub
 
Upvote 0

Forum statistics

Threads
1,221,889
Messages
6,162,624
Members
451,778
Latest member
ragananthony7911

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