PDA

View Full Version : For Each Then Next



Steve Belsch
04-21-2020, 09:27 AM
VBA Experts,

I am running the following code:

'Delete all sheets except PD & Functions
Windows(File_Upload).Activate
Application.DisplayAlerts = False
For Each ws In Worksheets
If ws.Name <> Tab1 And ws.Name <> Tab2 And ws.Name <> Tab3 Then ws.Delete
Next

There are only three worksheets, but yet I am getting this error message and it is stopping at ws.Delete

Run-time-erro '9':
Sbuscript out of range

Any thoughts?

Thanks.
Steve

Artik
04-21-2020, 09:41 AM
Try to replace the line with the one below:
For Each ws In ActiveWorkbook.WorksheetsHas something changed?

Artik

Steve Belsch
04-21-2020, 10:13 AM
Thank you Artik.

That didn't work. Maybe I need to activate that workbook? How do I do that? I think I am already.

im File_Upload As String
File_Upload = ActiveWorkbook.Name

and then ...

windows(File_Upload).Activate

Fluff
04-21-2020, 11:48 AM
What are Tab1, Tab2 etc?
Are they variables, sheet names, or maybe sheet codenames?

Steve Belsch
04-21-2020, 12:27 PM
Fluff,

Thank you for your reply. I tried Artik's idea again and it worked.

Thank you.
Steve




Thank you Artik.

That didn't work. Maybe I need to activate that workbook? How do I do that? I think I am already.

im File_Upload As String
File_Upload = ActiveWorkbook.Name

and then ...

windows(File_Upload).Activate

Fluff
04-21-2020, 12:46 PM
Glad it's sorted & thanks for the feedback