Deletes every 2nd worksheet

aaron1000

New Member
Joined
Jun 9, 2011
Messages
10
Hi Folks, I created code to delete ALL sheets between sheet name 4 and 200....however when executed its only deleting every 2nd worksheet.

I can't see why its skipping every 2nd sheet. Code as follows:

Sub delextraworksheets()
Application.DisplayAlerts = False
For I = 4 To 200
Worksheets(I).Delete
Next I
Application.DisplayAlerts = True
End Sub

Appreciate any help.

cheers
Aaron
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Code:
Sub delextraworksheets()
 Application.DisplayAlerts = False
 For i = 4 To 200 [B][COLOR="Red"]Step 2[/COLOR][/B]
 Worksheets(i).Delete
 Next i
 Application.DisplayAlerts = True
End Sub
 
Upvote 0
I can't see why its skipping every 2nd sheet.
You start with your pointer at 4.

You delete Sheet(4), so the old Sheet(5) becomes the new Sheet(4) and the old Sheet(6) becomes the new Sheet(5).

You increment your pointer to 5 and it deletes the new Sheet(5) which was the old sheet Sheet(6) before you deleted Sheet(4), so now you've deleted the original Sheet(4) and the original Sheet(6).

Another way of explaining it is that as your pointer increments, the next sheet along is shuffled to the left to replace the sheet you just deleted, and it 'passes' the pointer as it goes in the opposite direction, and it never gets considered as a candidate for deletion.

It's a similar problem to that of deleting rows or columns: you can do it from top-to-bottom or left-to-right, but it's simpler to do it from bottom-to-top or right-to-left.
 
Upvote 0
Code:
Sub delextraworksheets()
 Application.DisplayAlerts = False
 [COLOR="Red"]For i = 200 To 4 Step -2[/COLOR]
 Worksheets(i).Delete
 Next i
 Application.DisplayAlerts = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,568
Messages
6,179,595
Members
452,927
Latest member
whitfieldcraig

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