Loop through spreadsheets and run individual code

lyonder2000

New Member
Joined
May 24, 2011
Messages
9
Hello All,

I have a workbook that will have some raw data on about 40 worksheets. I want to clean the data up and make it look presentable. Sheet1 is unique, Sheet2 is unique, Sheet3-40 are all the same (format-wise). I've been trying to create a loop that uses a Select Case statement to determine if the Sheet is Sheet 1, 2, or other and format it accordingly.

For Each Sh In Worksheets
Select Case Sh.Name
Case Is = "QRY_OPEN_WRK_ORDRS"
Sheets("QRY_OPEN_WRK_ORDRS").Activate
Columns("A:I").Select
Selection.ColumnWidth = 19.29
Range("A1:I1").Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorDark1
.TintAndShade = -0.149998474074526
.PatternTintAndShade = 0
Range("A1").Select
End With
Case Is ="QRY_TL_OPEN_WOs_by_VNDR"
Sheets("QRY_TL_OPEN_WOs_by_VNDR").Activate
Columns("B:B").EntireColumn.AutoFit
Range("A1:B1").Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorDark1
.TintAndShade = -0.149998474074526
.PatternTintAndShade = 0
End With
Range("D1").Select
Case "ACCT_274201"
Columns("A:F").Select
Selection.ColumnWidth = 10.57
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Range("H1").Select
End Select
Next Sh


However, the first case (Case Is = "QRY_OPEN_WRK_ORDRS") formatting info is being used to format ALL the worksheets. I cannot figure out what I'm doing wrong or if I'm doing this the best way.
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Firstly, where have you put that code - in a General module or in a Worksheet module. If the latter you will need to qualify any property you use with the relevant worksheet (otherwise the worksheet containing the code will be used). Secondly when posting code please use code tags:

BB Code List - MrExcel Message Board
 
Upvote 0
I have this saved in my personal workbook. I've created other macros and saved them similarly in my personal workbook and they work without fail.

(sorry about the missing code tags)
 
Upvote 0
You aren't referencing the worksheets in the loop.
Code:
    For Each sh In Worksheets
        Select Case sh.Name
            Case Is = "QRY_OPEN_WRK_ORDRS"
                sh .Columns("A:I").ColumnWidth = 19.29
                With sh.Range("A1:I1").Interior
                    .Pattern = xlSolid
                    .PatternColorIndex = xlAutomatic
                    .ThemeColor = xlThemeColorDark1
                    .TintAndShade = -0.149998474074526
                    .PatternTintAndShade = 0
                End With
                Application.Goto sh.Range("a1")
            Case Is = "QRY_TL_OPEN_WOs_by_VNDR"
                sh.Columns("B:B").EntireColumn.AutoFit
                With sh.Range("A1:B1").Interior
                    .Pattern = xlSolid
                    .PatternColorIndex = xlAutomatic
                    .ThemeColor = xlThemeColorDark1
                    .TintAndShade = -0.149998474074526
                    .PatternTintAndShade = 0
                End With
                Application.Goto sh.Range("D1")
            Case "ACCT_274201"
                With sh.Columns("A:F")
                    .ColumnWidth = 10.57
                    .HorizontalAlignment = xlCenter
                    .VerticalAlignment = xlBottom
                    .WrapText = False
                    .Orientation = 0
                    .AddIndent = False
                    .IndentLevel = 0
                    .ShrinkToFit = False
                    .ReadingOrder = xlContext
                    .MergeCells = False
                End With
                Application.Goto sh.Range("H1")
        End Select
    Next sh
 
Upvote 0
In the code I posted I've added sheet references where they are needed.

For example here I've addded the sheet reference sh.
Code:
sh.Columns("A:I").ColumnWidth = 19.29
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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