Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11
Default Deleting Worksheets in VBA

Good afternoon everyone.
I have yet another problem that is doing my head in.
I am trying to programmatically delete all worksheets in a workbook except
the first one.
I have tried the following:

Dim p As Integer
Dim q As Integer
p = Worksheets.Count
If p = 2 Then
For q = 2 To p
Worksheets(q).Delete
Next q
End If

This however gives me a 'Subscript out of range' if there are more than 2
sheets in the workbook The Debugger tells me that there is something wrong
with:

Worksheets(q).Delete

Hope this is enough information.
Thanks in advance for any help.

KJ


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,080
Default Deleting Worksheets in VBA

You need to count backwards so that the worksheet index doesn't change after
each deletion:

For q = p To 2 Step -1

--

Vasant


"Knut Dahl" wrote in message
...
Good afternoon everyone.
I have yet another problem that is doing my head in.
I am trying to programmatically delete all worksheets in a workbook except
the first one.
I have tried the following:

Dim p As Integer
Dim q As Integer
p = Worksheets.Count
If p = 2 Then
For q = 2 To p
Worksheets(q).Delete
Next q
End If

This however gives me a 'Subscript out of range' if there are more than 2
sheets in the workbook The Debugger tells me that there is something wrong
with:

Worksheets(q).Delete

Hope this is enough information.
Thanks in advance for any help.

KJ




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11
Default Deleting Worksheets in VBA

Ah yeah of course.

Thanks so much. Really appreciate it.

KJ

"Vasant Nanavati" <vasantn *AT* aol *DOT* com wrote in message
...
You need to count backwards so that the worksheet index doesn't change
after
each deletion:

For q = p To 2 Step -1

--

Vasant


"Knut Dahl" wrote in message
...
Good afternoon everyone.
I have yet another problem that is doing my head in.
I am trying to programmatically delete all worksheets in a workbook
except
the first one.
I have tried the following:

Dim p As Integer
Dim q As Integer
p = Worksheets.Count
If p = 2 Then
For q = 2 To p
Worksheets(q).Delete
Next q
End If

This however gives me a 'Subscript out of range' if there are more than 2
sheets in the workbook The Debugger tells me that there is something
wrong
with:

Worksheets(q).Delete

Hope this is enough information.
Thanks in advance for any help.

KJ






  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,624
Default Deleting Worksheets in VBA

Say you have 10 sheets. Once you iterate 5 times, deleting sheets 2-6,
you have only 5 sheets left in the Worksheets collection (1,7,8,9,10).
But on the next iteration, q = 6, so Worksheets(q) is out of range. Try:


For q = p To 2 Step -1


instead.

In article ,
"Knut Dahl" wrote:

Good afternoon everyone.
I have yet another problem that is doing my head in.
I am trying to programmatically delete all worksheets in a workbook except
the first one.
I have tried the following:

Dim p As Integer
Dim q As Integer
p = Worksheets.Count
If p = 2 Then
For q = 2 To p
Worksheets(q).Delete
Next q
End If

This however gives me a 'Subscript out of range' if there are more than 2
sheets in the workbook The Debugger tells me that there is something wrong
with:

Worksheets(q).Delete

Hope this is enough information.
Thanks in advance for any help.

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Deleting Worksheets in VBA

another option is to always delete the second sheet

for q = 2 to p
worksheets(2).Delete
Next

--
Regards.
Tom Ogilvy



"Knut Dahl" wrote in message
...
Ah yeah of course.

Thanks so much. Really appreciate it.

KJ

"Vasant Nanavati" <vasantn *AT* aol *DOT* com wrote in message
...
You need to count backwards so that the worksheet index doesn't change
after
each deletion:

For q = p To 2 Step -1

--

Vasant


"Knut Dahl" wrote in message
...
Good afternoon everyone.
I have yet another problem that is doing my head in.
I am trying to programmatically delete all worksheets in a workbook
except
the first one.
I have tried the following:

Dim p As Integer
Dim q As Integer
p = Worksheets.Count
If p = 2 Then
For q = 2 To p
Worksheets(q).Delete
Next q
End If

This however gives me a 'Subscript out of range' if there are more than

2
sheets in the workbook The Debugger tells me that there is something
wrong
with:

Worksheets(q).Delete

Hope this is enough information.
Thanks in advance for any help.

KJ








Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Deleting worksheets in a macro Donna[_2_] Excel Worksheet Functions 2 January 29th 10 04:58 AM
Deleting worksheets David Excel Discussion (Misc queries) 2 January 19th 07 08:19 PM
Help deleting worksheets Wilhelmutt Excel Discussion (Misc queries) 2 April 19th 05 03:23 AM
Deleting worksheets Alan L. Wagoner Excel Programming 4 January 24th 05 03:19 PM
Deleting worksheets Oscar Excel Programming 1 July 23rd 04 04:36 AM


All times are GMT +1. The time now is 12:08 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"