VBA Loop? for only specific worksheets, not the whole workbook

Suggettm

New Member
Joined
Jul 31, 2017
Messages
26
Below is a quick sample of my code that I have in my workbook. Not all the tabs are the same so for example I want the Macro to do specific things to the "MTD Glance" tab and a couple other tabs, but in the middle of my code, I have a sample of my daily tabs which are numbered "01" thru "31" and they do not change. My code repeats 31 times for those sections because I do not know how to isolate the loop to only those specific tabs "01" thru "31". Could someone please help me identify a loop to do just those sections?

I have other loops like "unprotect" in the document, but that goes throughout the entire document so I have no problems. Please help. Thanks.

On a side note, my autofilter is running a bit slow, but I feel that it is just all my conditional formatting, so I'll fix that after my loop problem.

Code:
'Each tab gets its own autofilterSheets("MTD Glance").Select
Worksheets("MTD Glance").Calculate
'Unfilters all rows
Sheets("MTD Glance").Select
ActiveSheet.AutoFilterMode = False
Sheets("MTD Glance").Select
Cells.EntireRow.Hidden = False


'Filters the data using autofilter
Sheets("MTD Glance").Select
Range("A:A").AutoFilter
Range("A:A").AutoFilter field:=1, Criteria1:="<>0"
'End Auto filter for tab
Sheets("MTD Glance").Select
Application.GoTo Reference:=Range("D1"), Scroll:=True




'Start worksheet code: Each tab gets its own autofilter
'Unfilters all rows
Sheets("01").Select
Worksheets("01").Calculate
ActiveSheet.AutoFilterMode = False
Cells.EntireRow.Hidden = False
'Filters the data using autofilter
Sheets("01").Select
Range("A:A").AutoFilter
Range("A:A").AutoFilter field:=1, Criteria1:="<>0"
'End Auto filter for tab
'Unhide + Hide Columns False = Unhide, True = Hide
'Cells.EntireColumn.Hidden = False


'Unhide Columns
Sheets("01").Select
Cells.EntireColumn.Hidden = False
Sheets("01").Select


  If Range("B1").Value <= "3" Then
    ActiveSheet.Cells.EntireColumn.Hidden = False
        Columns("P:X").EntireColumn.Hidden = True
        Columns("AO:BD").EntireColumn.Hidden = True
    ElseIf Range("B1").Value = "4" Then
     ActiveSheet.Cells.EntireColumn.Hidden = False
        Columns("R:X").EntireColumn.Hidden = True
        Columns("AS:BD").EntireColumn.Hidden = True
     ElseIf Range("B1").Value = "5" Then
     ActiveSheet.Cells.EntireColumn.Hidden = False
        Columns("T:X").EntireColumn.Hidden = True
        Columns("AW:BD").EntireColumn.Hidden = True
     ElseIf Range("B1").Value = "6" Then
     ActiveSheet.Cells.EntireColumn.Hidden = False
        Columns("V:X").EntireColumn.Hidden = True
        Columns("BA:BD").EntireColumn.Hidden = True
    ElseIf Range("B1").Value = "7" Then
     ActiveSheet.Cells.EntireColumn.Hidden = False
    End If


'Hide Columns
Sheets("01").Select
Columns("A:H").Select
Selection.EntireColumn.Hidden = True
Sheets("01").Select
Columns("BE:BM").Select
Selection.EntireColumn.Hidden = True
Application.GoTo Reference:=Range("H1"), Scroll:=True
'EndWorksheet Code


'Start worksheet code: Each tab gets its own autofilter
'Unfilters all rows
Sheets("02").Select
Worksheets("02").Calculate
ActiveSheet.AutoFilterMode = False
Cells.EntireRow.Hidden = False
'Filters the data using autofilter
Sheets("02").Select
Range("A:A").AutoFilter
Range("A:A").AutoFilter field:=1, Criteria1:="<>0"
'End Auto filter for tab
'Unhide + Hide Columns False = Unhide, True = Hide
'Cells.EntireColumn.Hidden = False
 'Unhide Columns
Sheets("02").Select
Cells.EntireColumn.Hidden = False
Sheets("02").Select


  If Range("B1").Value <= "3" Then
    ActiveSheet.Cells.EntireColumn.Hidden = False
        Columns("P:X").EntireColumn.Hidden = True
        Columns("AO:BD").EntireColumn.Hidden = True
    ElseIf Range("B1").Value = "4" Then
     ActiveSheet.Cells.EntireColumn.Hidden = False
        Columns("R:X").EntireColumn.Hidden = True
        Columns("AS:BD").EntireColumn.Hidden = True
     ElseIf Range("B1").Value = "5" Then
     ActiveSheet.Cells.EntireColumn.Hidden = False
        Columns("T:X").EntireColumn.Hidden = True
        Columns("AW:BD").EntireColumn.Hidden = True
     ElseIf Range("B1").Value = "6" Then
     ActiveSheet.Cells.EntireColumn.Hidden = False
        Columns("V:X").EntireColumn.Hidden = True
        Columns("BA:BD").EntireColumn.Hidden = True
    ElseIf Range("B1").Value = "7" Then
     ActiveSheet.Cells.EntireColumn.Hidden = False
    End If


'Hide Columns
Sheets("02").Select
Columns("A:H").Select
Selection.EntireColumn.Hidden = True
Sheets("02").Select
Columns("BE:BM").Select
Selection.EntireColumn.Hidden = True
Application.GoTo Reference:=Range("H1"), Scroll:=True
'EndWorksheet Code
 
Last edited by a moderator:

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Code:
Sub test()
Dim ws As Worksheet
Dim x As Integer
For x = 1 To 31
    Set ws = Sheets(Format(x, "00"))
    ws.Select
    'rest of your code here
Next x
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,198
Members
452,616
Latest member
intern444

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