Format All Worksheets but formatting the same worksheet

nayseem

New Member
Joined
Mar 22, 2016
Messages
14
Good morning,

I am a complete newbie to Excel VBA but have been watching YouTube Videos and reading these forums to figure things out but I need your help as I am stuck...

I created the following macro and would like it to format all the worksheets in my spreadsheet (about 40 of them). However, the code below is formatting one of the worksheets and inserting 11 rows - not sure where the problem is.

The ideal goal would be to have this macro format all macros except my first three worksheets as they are my summary, instructions and initial macro pages.

Any suggestions? I would be happy to clarify the needs as well.

Thanks in advance!



Sub Macro1()
'
' Macro1 Macro
'
' Keyboard Shortcut: Ctrl+Shift+L
'


Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets


Columns("A:O").Select
Columns("A:O").EntireColumn.AutoFit
Columns("M:N").Select
Selection.ColumnWidth = 12.71
Rows("1:1").EntireRow.AutoFit
Rows("1:1").Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A1:O1").Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorLight2
.TintAndShade = 0.799981688894314
.PatternTintAndShade = 0
End With
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Range("F1").Select
ActiveCell.FormulaR1C1 = "Opportunity Report"
Range("H1").Select
ActiveCell.FormulaR1C1 = "=TODAY()+1"
Range("H1").Select
Columns("H:H").Select
Range("H2:H40").Activate
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
Formula1:="=""Due"""
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.Color = -16383844
.TintAndShade = 0
End With
Next ws
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 13551615
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False



End Sub
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
WTTBs

This first post is to help you out in the future.

when posting code please use code tags

remove the spaces

[ code ]

[ /code ]

rich
 
Upvote 0
Welcome to the Board!

The problem is, though you appear to be looping through your worksheets, you really aren't, as you aren't selecting/activating each worksheet. So if you have 11 sheets, you are really running the code 11 times on the same sheet (whatever is the active sheet when your run the macro).

Here is how you can loop through all your sheets, starting with the 4th sheet:
Code:
    Dim i As Long
'   Start at worksheet number 4
    For i = 4 To Worksheets.Count
        Sheets(i).Activate
        ' Your formatting code here
    Next i
Note. You can also reduce your code a bit. You do not need to actually use Select or Selection that much. Most lines of code that end with Select and the next line begins with Selection can be combined, i.e.
this:
Code:
[COLOR=#333333]Columns("M:N").Select[/COLOR]
[COLOR=#333333]Selection.ColumnWidth = 12.71[/COLOR]
can be combined to this:
Code:
[COLOR=#333333]Columns("M:N").[/COLOR][COLOR=#333333]ColumnWidth = 12.71[/COLOR]
as well sections like this:
Code:
[COLOR=#333333]Range("H1").Select[/COLOR]
[COLOR=#333333]ActiveCell.FormulaR1C1 = "=TODAY()+1"[/COLOR]
can be combined like this:
Code:
[COLOR=#333333]Range("H1").[/COLOR][COLOR=#333333]FormulaR1C1 = "=TODAY()+1"[/COLOR]

By getting rid of a lot of the Select and Activate statements, your code will be shorter and run more efficiently.
 
Upvote 0
Try this.
Code:
Sub Macro1()
Dim ws As Worksheet

    For Each ws In ThisWorkbook.Worksheets
    
        With ws
            .Columns("A:O").EntireColumn.AutoFit
            .Columns("M:N").ColumnWidth = 12.71
            .Rows("1:1").EntireRow.AutoFit
            .Rows("1:1").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
            
            With .Range("A1:O1")
                With .Interior
                    .Pattern = xlSolid
                    .PatternColorIndex = xlAutomatic
                    .ThemeColor = xlThemeColorLight2
                    .TintAndShade = 0.799981688894314
                    .PatternTintAndShade = 0
                End With

                .HorizontalAlignment = xlCenter
                .VerticalAlignment = xlBottom
                .WrapText = False
                .Orientation = 0
                .AddIndent = False
                .IndentLevel = 0
                .ShrinkToFit = False
                .ReadingOrder = xlContext
                .MergeCells = False
            End With
            
            .Range("F1").Value = "Opportunity Report"
            .Range("H1").FormulaR1C1 = "=TODAY()+1"
            
            With .Range("H2:H40")
                .FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
                                      Formula1:="=""Due"""
                .FormatConditions(.FormatConditions.Count).SetFirstPriority
                With .FormatConditions(1).Font
                    .Color = -16383844
                    .TintAndShade = 0
                End With

                With .FormatConditions(1).Interior
                    .PatternColorIndex = xlAutomatic
                    .Color = 13551615
                    .TintAndShade = 0
                End With
                .FormatConditions(1).StopIfTrue = False
            End With
        End With
        
    Next ws

End Sub
 
Upvote 0
here is your code (cleaned up a little from the recording...excel is a bit over-zealous when it records).

It will now make your changes to each sheet and not multiple times to one sheet

Code:
Sub Macro1()
 '
 ' Macro1 Macro
 '
 ' Keyboard Shortcut: Ctrl+Shift+L
 '


 Dim ws As Worksheet
 For Each ws In ThisWorkbook.Worksheets
    ws.Select
     Columns("A:O").EntireColumn.AutoFit
     Columns("M:N").ColumnWidth = 12.71
     Rows("1:1").EntireRow.AutoFit
     Rows("1:1").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
     
     With Range("A1:O1")
        With .Interior
           .ThemeColor = xlThemeColorLight2
           .TintAndShade = 0.799981688894314
        End With
        .HorizontalAlignment = xlCenter
     End With
     Range("F1").Value = "Opportunity Report"
     Range("H1").FormulaR1C1 = "=TODAY()+1"
     Range("H2:H40").Select
     Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
     Formula1:="=""Due"""
     Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
     With Selection.FormatConditions(1).Font
        .Color = -16383844
        .TintAndShade = 0
     End With
 Next ws
    With Selection.FormatConditions(1).Interior
       .PatternColorIndex = xlAutomatic
       .Color = 13551615
       .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False
 End Sub
 
Upvote 0
I copied and pasted this into the VBA window and when I try to run it, it states "We couldn't do this for the selected range of cells. Select a single cell within a range of data and then try again."

So I highlighted the sheets and tried to run the macro again and it states this "Run-time error '1004'" - I have changed the text to red in which it is giving me the error for.

here is your code (cleaned up a little from the recording...excel is a bit over-zealous when it records).

It will now make your changes to each sheet and not multiple times to one sheet

Rich (BB code):
Sub Macro1()
 '
 ' Macro1 Macro
 '
 ' Keyboard Shortcut: Ctrl+Shift+L
 '


 Dim ws As Worksheet
 For Each ws In ThisWorkbook.Worksheets
    ws.Select
     Columns("A:O").EntireColumn.AutoFit
     Columns("M:N").ColumnWidth = 12.71
     Rows("1:1").EntireRow.AutoFit
     Rows("1:1").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
     
     With Range("A1:O1")
        With .Interior
           .ThemeColor = xlThemeColorLight2
           .TintAndShade = 0.799981688894314
        End With
        .HorizontalAlignment = xlCenter
     End With
     Range("F1").Value = "Opportunity Report"
     Range("H1").FormulaR1C1 = "=TODAY()+1"
     Range("H2:H40").Select
     Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
     Formula1:="=""Due"""
     Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
     With Selection.FormatConditions(1).Font
        .Color = -16383844
        .TintAndShade = 0
     End With
 Next ws
    With Selection.FormatConditions(1).Interior
       .PatternColorIndex = xlAutomatic
       .Color = 13551615
       .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False
 End Sub
 
Upvote 0
You don't need to select anything, in fact having multiple sheets selected could cause problems like the one you describe.
 
Upvote 0
The ideal goal would be to have this macro format all macros except my first three worksheets as they are my summary, instructions and initial macro pages.
Did you see my reply? The loop I showed you allows you to exclude the first three sheets.
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,693
Members
448,979
Latest member
DET4492

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