Apply code to all but one worksheet in workbook

linesite

New Member
Joined
Oct 10, 2009
Messages
41
I create a daily summary report each day that includes a pivot table with several worksheets based on filter pages within the pivot table. The pivot table includes a worksheet called "Executive Speeding Report" and "PT". Essentially the Executive Speeding Report worksheet contains the "raw" data while the "PT" worksheet is the pivot table portion of the workbook. The filter pages contain details exclusively to each division impacted by the summary report--these worksheets are labeled "Div. 041", "Div. 062", "Div. 089" based on driving behavior the previous day (see attached image). This daily workbook could contain as many as 30 worksheets or as few as 1 during each daily download.

Can someone help me add VBA to apply this conditional formatting macro to each worksheet in the workbook except the worksheet labeled "Executive Speeding Report"?

Dim myWB As Workbook
Dim myWS As Worksheet
Dim FirstCell, myRows, myColumns, myUsedRange As Range

Set myWB = ActiveWorkbook
Set myWS = myWB.ActiveSheet
Set FirstCell = myWS.Cells(5, 1)
Set myRows = myWS.Cells(Rows.Count, 1).End(xlUp).Rows
Set myColumns = myWS.Cells(5, Columns.Count).End(xlToLeft).Columns
Set myUsedRange = myWS.Range(myRows, myColumns)

Application.ScreenUpdating = False

myUsedRange.Select

myUsedRange.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=AND(TEXT($D5,""hh:mm:ss"")>""00:01:01"",OR(ISNUMBER(SEARCH(""6-"",$E5)),ISNUMBER(SEARCH(""7-"",$E5)),ISNUMBER(SEARCH(""9-"",$E5))),NUMBERVALUE(TEXT($G5,""##""))>63)"

With Selection.FormatConditions(1).Font
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
End With

With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 255
.TintAndShade = 0
End With

Cells.Select
Cells.EntireColumn.AutoFit

Application.ScreenUpdating = True

Range("A1").Select

End Sub
 

Attachments

  • Executive Speed Report_Pivot Table.png
    Executive Speed Report_Pivot Table.png
    3.2 KB · Views: 12

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
When you post code, please click the </> icon and paste it into the popup window.

A lot could be done to clean up your code, but for what you're asking, replace the line Set myWS = myWB.ActiveSheet with these 2 lines
VBA Code:
For Each myWS in myWB.WorkSheets
    If myWS.Name <>"Executive Speeding Report" Then
and insert these 2 lines just before End Sub
VBA Code:
    End If
Next
It might need a couple of other adjustments, it's easy to miss things in unformatted code.
 
Upvote 0
I made adjustments based on your response--is this correct?

VBA Code:
Sub Test_Add_CF_PT()

Dim myWB                                                        As Workbook
Dim myWS                                                        As Worksheet
Dim FirstCell, myRows, myColumns, myUsedRange                   As Range

Set myWB = ActiveWorkbook
Set myWS = myWB.ActiveSheet
Set FirstCell = myWS.Cells(5, 1)
Set myRows = myWS.Cells(Rows.Count, 1).End(xlUp).Rows
Set myColumns = myWS.Cells(5, Columns.Count).End(xlToLeft).Columns
Set myUsedRange = myWS.Range(myRows, myColumns)

Application.ScreenUpdating = False

For Each myWS In myWB.Worksheets
    If myWS.Name <> "Executive Speeding Report" Then

    myUsedRange.Select
      
    myUsedRange.FormatConditions.Add Type:=xlExpression, Formula1:= _
        "=AND(TEXT($D5,""hh:mm:ss"")>""00:01:01"",OR(ISNUMBER(SEARCH(""6-"",$E5)),ISNUMBER(SEARCH(""7-"",$E5)),ISNUMBER(SEARCH(""9-"",$E5))),NUMBERVALUE(TEXT($G5,""##""))>63)"
            
    With Selection.FormatConditions(1).Font
        .ThemeColor = xlThemeColorDark1
        .TintAndShade = 0
    End With
        
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 255
        .TintAndShade = 0
    End With
        
    Cells.Select
    Cells.EntireColumn.AutoFit
    
    Application.ScreenUpdating = True
        
    Range("A1").Select

End If

Next
    
End Sub
 
Upvote 0
I made this adjustment to the code. The macro appears to run through and applies conditional formatting but it does not autofit columns.

VBA Code:
Sub Test_Add_CF_PT()

Dim myWB                                                        As Workbook
Dim myWS                                                        As Worksheet
Dim FirstCell, myRows, myColumns, myUsedRange                   As Range

Set myWB = ActiveWorkbook
'Set myWS = myWB.ActiveSheet

For Each myWS In myWB.Worksheets
    If myWS.Name <> "Executive Speeding Report" Then
    
Set FirstCell = myWS.Cells(5, 1)
Set myRows = myWS.Cells(Rows.Count, 1).End(xlUp).Rows
Set myColumns = myWS.Cells(5, Columns.Count).End(xlToLeft).Columns
Set myUsedRange = myWS.Range(myRows, myColumns)

'Application.ScreenUpdating = False

'For Each myWS In myWB.Worksheets
'    If myWS.Name <> "Executive Speeding Report" Then

'    myUsedRange.Select
      
    myUsedRange.FormatConditions.Add Type:=xlExpression, Formula1:= _
        "=AND(TEXT($D5,""hh:mm:ss"")>""00:01:01"",OR(ISNUMBER(SEARCH(""6-"",$E5)),ISNUMBER(SEARCH(""7-"",$E5)),ISNUMBER(SEARCH(""9-"",$E5))),NUMBERVALUE(TEXT($G5,""##""))>63)"
            
    With myUsedRange.FormatConditions(1).Font
        .ThemeColor = xlThemeColorDark1
        .TintAndShade = 0
    End With
        
    With myUsedRange.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 255
        .TintAndShade = 0
    End With
        
'    Cells.Select
    Cells.EntireColumn.AutoFit
    
'    Application.ScreenUpdating = True
        
    Range("A1").Select

End If

Next
    
End Sub
 
Upvote 0
Here's your code cleaned up a bit, the formula for the cf rule looks as though it could be simplified as well but I've left that for now.
VBA Code:
Sub Test_Add_CF_PT()

Dim myWB As Workbook
Dim myWS As Worksheet
Dim myRows As Range, myColumn As Range, myUsedRange As Range
   
    Application.ScreenUpdating = False
    Set myWB = ActiveWorkbook

For Each myWS In myWB.Worksheets
    If myWS.Name <> "Executive Speeding Report" Then
        Set myRows = myWS.Cells(Rows.Count, 1).End(xlUp).Rows
        Set myColumns = myWS.Cells(5, Columns.Count).End(xlToLeft).Columns
        Set myUsedRange = myWS.Range(myRows, myColumns)
     
        With myUsedRange
            .FormatConditions.Add Type:=xlExpression, Formula1:= _
            "=AND(TEXT($D5,""hh:mm:ss"")>""00:01:01"",OR(ISNUMBER(SEARCH(""6-"",$E5)),ISNUMBER(SEARCH(""7-"",$E5)),ISNUMBER(SEARCH(""9-"",$E5))),NUMBERVALUE(TEXT($G5,""##""))>63)"
           
            With Selection.FormatConditions(1)
                With .Font
                    .ThemeColor = xlThemeColorDark1
                    .TintAndShade = 0
                End With
                With .Interior
                    .PatternColorIndex = xlAutomatic
                    .Color = 255
                    .TintAndShade = 0
                End With
            End With
        End With
        .Cells.EntireColumn.AutoFit
    End If
Next
    Application.ScreenUpdating = True
End Sub
edit:- the autofit problem that you encountered was as a result of removing the Select / Selection lines from the code. If you compare your version to mine, you will see the dot / period at the start of the autofit line, without this the autofit is being applied to the last selected sheet rather than the current sheet set by myWS.
Also, check the declarations at the top, (Dim ??? As ????), in the last line of your Dim's only myUsedRange is actually a range, the rest are variants.
 
Last edited:
Upvote 0
jasonb75--it worked. Your code recommendations and modifications will save me a bunch of time every day working with these reports. Thank you very much for your expertise and time!
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,159
Members
453,021
Latest member
Justyna P

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