Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Runtime error 13 2

Status
Not open for further replies.

vba317

Programmer
Mar 5, 2009
708
US
I am getting a run time error 13 when I run my loops. I was hoping to figure out why.
I am trying to loop through 8 worksheets that are named Assists_1 through Assists_8.
Once a sheet has information populated in it the name of the sheet is changed to Assists_Name. So once the information is populated the only tabs remaining with the original names are empty so I would like to delete the sheets that don't have information in them.
Tom

Code:
For S = 1 To 8
        For Each ws In Workbooks("_Assists.xlt").Worksheets
            If ws.Name = "Assists_" & S Then ws.Delete
        Next ws
    Next S
 
What about this ?
Code:
For S = 1 To 8
    For Each ws In Workbooks("_Assists.xlt").Worksheets
        If ws.Name = "Assists_" & S Then ws.Delete: Exit For
    Next ws
Next S

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hi,

Think about it: if your loop goes 8 times, and you have deleted any worksheet, what will happen when the loop counter exceeds the number or worksheet remaining?

Whenever you delete ANY object in a loop, make your loop count from highest downward, ie in reverse! Do not use For Each...Next.

Skip,

[glasses]Just traded in my OLD subtlety...
for a NUance![tongue]
 
Both guys thanks for your input. I figured it out.

Code:
S = 15
Do
S = S - 1
goXl.Sheets(S).Select
If goXl.ActiveSheet.Name = "Assists_" & S Then goXl.Sheets("Assists_" & S).Delete
Loop Until S = 1
goXl.DisplayAlerts = True

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top