Need help to run a macro in every worksheet in the work book.

anandks007

New Member
Joined
Apr 15, 2009
Messages
4
I am having an excel macro. But since I'm having close to 30 work sheets, I need to run the same macro in every sheet. My current macro works only for the first sheet. Can any one help me?

Code:
Sub WorkZ()
        For Each WS In Worksheets
        Columns("K:AE").Select
        Selection.Delete Shift:=xlToLeft
        Cells.Select
        With Selection.Font
            .Name = "Arial"
            .Size = 8
            .ColorIndex = 1
        End With
        Range("A2:D20").Select
        Range(Selection, Selection.End(xlDown)).Select
        Selection.NumberFormat = "#,##0.0"
        Selection.NumberFormat = "#,##0"
        Range("E2:E7").Select
        Range(Selection, Selection.End(xlDown)).Select
        Selection.NumberFormat = "0.000%"
        Selection.NumberFormat = "0.0000%"
        Selection.NumberFormat = "0.00000%"
        Selection.NumberFormat = "0.000000%"
        Range("G21").Select
        Columns("E:E").EntireColumn.AutoFit
        Columns("F:F").EntireColumn.AutoFit
        Columns("G:G").EntireColumn.AutoFit
        Columns("H:H").EntireColumn.AutoFit
        Columns("I:I").EntireColumn.AutoFit
        Rows("1:1").Select
        Selection.Font.Bold = True
        Columns("A:I").Select
        Selection.ColumnWidth = 5.29
        Columns("A:I").Select
        Columns("A:I").EntireColumn.AutoFit
        Range("A1").Select
        ActiveWorkbook.Save
        Next WS
End Sub
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Here's a start.
Code:
With WS
    .Columns("K:AE").Delete Shift:=xlToLeft
    With .Cells.Font
        .Name = "Arial"
        .Size = 8
        .ColorIndex = 1
    End With
 
etc...
End With
 
Upvote 0
HI
try the following modifications
Code:
Sub WorkZ()
Dim a As Long
For a = 1 To Sheets.Count
        Worksheets(a).Select
        Columns("K:AE").Delete Shift:=xlToLeft
        Cells.Select
        With Selection.Font
            .Name = "Arial"
            .Size = 8
            .ColorIndex = 1
        End With
        Range("A2:D20").Select
        Range(Selection, Selection.End(xlDown)).Select
        Selection.NumberFormat = "#,##0.0"
        Selection.NumberFormat = "#,##0"
        Range("E2:E7").Select
        Range(Selection, Selection.End(xlDown)).Select
        Selection.NumberFormat = "0.000%"
        Selection.NumberFormat = "0.0000%"
        Selection.NumberFormat = "0.00000%"
        Selection.NumberFormat = "0.000000%"
        Range("G21").Select
        Rows("1:1").Font.Bold = True
        Columns("A:I").ColumnWidth = 5.29
        Columns("A:I").EntireColumn.AutoFit
 Next a
          ActiveWorkbook.Save
          MsgBox "complete"
End Sub
Ravi
 
Upvote 0
HI
try the following modifications
Code:
Sub WorkZ()
Dim a As Long
For a = 1 To Sheets.Count
        Worksheets(a).Select
        Columns("K:AE").Delete Shift:=xlToLeft
        Cells.Select
        With Selection.Font
            .Name = "Arial"
            .Size = 8
            .ColorIndex = 1
        End With
        Range("A2:D20").Select
        Range(Selection, Selection.End(xlDown)).Select
        Selection.NumberFormat = "#,##0.0"
        Selection.NumberFormat = "#,##0"
        Range("E2:E7").Select
        Range(Selection, Selection.End(xlDown)).Select
        Selection.NumberFormat = "0.000%"
        Selection.NumberFormat = "0.0000%"
        Selection.NumberFormat = "0.00000%"
        Selection.NumberFormat = "0.000000%"
        Range("G21").Select
        Rows("1:1").Font.Bold = True
        Columns("A:I").ColumnWidth = 5.29
        Columns("A:I").EntireColumn.AutoFit
 Next a
          ActiveWorkbook.Save
          MsgBox "complete"
End Sub
Ravi

Oh my god... thanks a hundred times Ravi... you solved my problem.

Thanks....

I was sitting in the office for the last 4 hours thinking what to do next....
 
Upvote 0
What do you mean by 'not working'?

The code I posted should work, though I didn't translate all your code.

Ravi's will probably also work but there is absolutely no need to use Select/Selection anywhere to do what you want.
Code:
Sub WorkZ()
        For Each ws In Worksheets
            With ws
            
                .Columns("K:AE").Delete Shift:=xlToLeft
                
                With .Cells.Font
                
                    .Name = "Arial"
                    .Size = 8
                    .ColorIndex = 1
                End With
                
                With Range("A2:D" & .Range("A" & Rows.Count).End(xlUp).Row)
                
                    .NumberFormat = "#,##0"
                    
                End With
                
                With Range("E2:E" & .Range("E" & Rows.Count).End(xlUp).Row)
                    .NumberFormat = "0.000000%"
                End With
                
                .Columns("E:I").EntireColumn.AutoFit
            
                .Rows("1:1").Font.Bold = True
                
                .Columns("A:I").ColumnWidth = 5.29 ' why are you changing the width when you've already autofitted it
                                                                      ' and then autofit them again ?????
                .Columns("A:I").EntireColumn.AutoFit
                
            End With
            
        Next ws
        
        ActiveWorkbook.Save
        
End Sub
 
Upvote 0
What do you mean by 'not working'?

The code I posted should work, though I didn't translate all your code.

Ravi's will probably also work but there is absolutely no need to use Select/Selection anywhere to do what you want.

Hi... I am really sorry. Your code was working, but I typed it wrong. Thanks a lot for the help.
 
Upvote 0

Forum statistics

Threads
1,214,857
Messages
6,121,948
Members
449,056
Latest member
FreeCricketId

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