Sheet loop Sheets("A's").Select

Peterfc2

Active Member
Joined
Jan 2, 2004
Messages
394
Office Version
  1. 2013
Platform
  1. Windows
I have 26 sheets named A's to Z's
macro edit

sub

my code etc

Sheets("A's").Select

end sub

The only change is the code is Sheets("A's").Select

So I need a loop that just changes - Sheets("A's").Select to Sheets("B's").Select etc.
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Hi Peterfc2-

So there are two ways you could do this- version 1 just goes from 1st tab to 2nd, 3rd... regardless of their alphabetical order. Version 2 goes from A to B, to C, etc...

Code:
Public Sub sampleCodeV1()
Dim wb As Workbook
Dim wsCounter As Long
Set wb = ThisWorkbook
For wsCounter = 1 To 26
    wb.Sheets(wsCounter).Select
Next
End Sub

Code:
Public Sub sampleCodeV2()
Dim wb As Workbook
Dim wsCounter As Long
Dim colLetter As String
Set wb = ThisWorkbook
For wsCounter = 1 To 26
    colLetter = Replace(Replace(wb.Sheets(1).Range("A1")(1, wsCounter).Address, "1", ""), "$", "")
    wb.Sheets(colLetter & "'s").Select
Next
End Sub

Hope this helps,
TheSilkCode
 
Upvote 0
Hi Peterfc2-

So there are two ways you could do this- version 1 just goes from 1st tab to 2nd, 3rd... regardless of their alphabetical order. Version 2 goes from A to B, to C, etc...

Code:
Public Sub sampleCodeV1()
Dim wb As Workbook
Dim wsCounter As Long
Set wb = ThisWorkbook
For wsCounter = 1 To 26
    wb.Sheets(wsCounter).Select
Next
End Sub

Code:
Public Sub sampleCodeV2()
Dim wb As Workbook
Dim wsCounter As Long
Dim colLetter As String
Set wb = ThisWorkbook
For wsCounter = 1 To 26
    colLetter = Replace(Replace(wb.Sheets(1).Range("A1")(1, wsCounter).Address, "1", ""), "$", "")
    wb.Sheets(colLetter & "'s").Select
Next
End Sub

Hope this helps,
TheSilkCode

Code:
For wsCounter = 1 To 26

If ActiveSheet.Range("a3") = "" Then
Exit Sub
End If

    colLetter = Replace(Replace(wb.Sheets(1).Range("A1")(1, wsCounter).Address, "1", ""), "$", "")
    wb.Sheets(colLetter & "'s").Select

Works fine up to the I's. I have no data in the I's ActiveSheet.Range("a3") so I put in a exit sub than does not apply now. What do I replace the exit sub with to make it carry on to the J's?
One other thing the last Next should it be Next wsCounter
Peter
 
Upvote 0

Forum statistics

Threads
1,215,011
Messages
6,122,680
Members
449,091
Latest member
peppernaut

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