VBA code for each visible sheet?

jroo

Board Regular
Joined
May 22, 2003
Messages
157
Hi - I'd like to create a macro that would select each visible sheet in a workbook and have the top left cell selected, so that users see the top of each tab.

I know this accomplishes going to the top of each sheet...
Application.Goto Reference:="R1C1"


I'm sure a "For Each" statement can do what I'd want, but I'm not sure how to tweak the coad below (note: over 20 tabs in my workbook, some of which are hidden/protected, so there's no need to run code on those tabs)

Sub ShowMiscTabs()
Application.ScreenUpdating = False
Dim sh
For Each sh In Sheets(Array("Sheet1", "Sheet2", "Sheet3, "Sheet4"))
sh.Visible = True
Next
Application.ScreenUpdating = True
End Sub
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Code:
Dim ws as Worksheet
Application.ScreenUpdating = False
For Each ws in ThisWorkbook.Worksheets
If ws.Visible = True Then ws.Cells(1,1).Select
Next ws
Application.ScreenUpdating = True
 
Upvote 0
Hi - I tried that but got this error "Select method of range class failed" ... when I clicked debug, it went to this line

If ws.Visible = True Then ws.Cells(1,1).Select
 
Upvote 0
Hi - I tried that but got this error "Select method of range class failed" ... when I clicked debug, it went to this line

If ws.Visible = True Then ws.Cells(1,1).Select

Although you generally don't need to select a worksheet in VBA, sometimes you do have to. Try the version below:

Code:
Dim ws as Worksheet
Application.ScreenUpdating = False
For Each ws in ThisWorkbook.Worksheets
If ws.Visible = True Then 
ws.Select
Cells(1,1).Select
End If
Next ws
Application.ScreenUpdating = True
 
Upvote 0
Sub A1InAllSheets()
Dim tf As Boolean
tf = True
For Each sh In Sheets
If sh.Visible Then sh.Select tf
tf = False
Next
Range("A1").Select
ActiveSheet.Select
End Sub
 
Upvote 0
Why not use a WorkbookSheetActivate event? That would eliminate looping, and possibly having to repeat the code at some point.

Code:
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
    Range("A1").Select
End Sub

HTH,
 
Upvote 0
Thanks for the heads up Bob. (I generally don't bother with error handling in simple examples).
 
Upvote 0

Forum statistics

Threads
1,224,557
Messages
6,179,508
Members
452,918
Latest member
Davion615

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