loop through all worksheets

O

ordnance1

My workbook has over 100 worksheets. I want to be able to execute the code
below, have it cycle through all worksheets and when done leave the
worksheet that was active when the code was started active.

Application.ScreenUpdating = False
Dim n As Single
For n = 1 To Sheets.Count
With Sheets(n)
.Activate
.Range("A3").Activate
End With
Next n
Application.ScreenUpdating = True
 
O

ozgrid.com

Sub LoopAllWS()
Dim ws As Worksheet
Dim wsStart As Worksheet

Set wsStart = ActiveSheet
For Each ws In Worksheet
With ws
'No need to use Select use .With code here
End With
Next ws
wsStart.Activate
End Sub
 
O

ordnance1

Thanks, but I get an Object Required error and the following line of text is
highlighted:

For Each ws In Worksheet
 
P

Per Jessen

There is a typo in the statement, use this:

For Each ws In Worksheets

Regards,
Per
 
J

Javed

My workbook has over 100 worksheets. I want to be able to execute the code
below, have it cycle through all worksheets and when done leave the
worksheet that was active when the code was started active.

Application.ScreenUpdating = False
Dim n As Single
    For n = 1 To Sheets.Count
        With Sheets(n)
            .Activate
            .Range("A3").Activate
        End With
    Next n
Application.ScreenUpdating = True

Probably you want to activate the first cell of each sheet so that
user get it right A1 on reaching sheets


You can use following code for speed

Sub YourSubName()
Dim ws As Worksheet
Dim wsStart As Worksheet

Set wsStart = ActiveSheet
For Each ws In Worksheet
If ws.Visible = xlSheetVisible Then
Application.GoTo ws.Range("a1"), True
End If
Next ws
wsStart.Activate
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top