Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 59
Default deleting sheets

ok,
I am trying to delete all the sheets in a workbook.
I started with something like this:


Sub xxx()
Dim y As Excel.Worksheet
For x = 1 To Excel.ActiveWorkbook.Worksheets.Count
Set y = Excel.ActiveWorkbook.Worksheets(1)
y.Delete
Next x
End Sub


2 problems.
When you delete the sheet, you get a prompt. Is there a way to turn that
prompt off?

I guess I need at least 1 sheet. Is there a way to add a new sheet to the
end of the list of sheets. Then go
For x = 1 To Excel.ActiveWorkbook.Worksheets.Count -1

thanks on any help


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,939
Default deleting sheets

Give this a whirl...

Sub xxx()
Dim wksNew As Worksheet
Dim wks As Worksheet
On Error GoTo ErrorHandler
Set wksNew = Worksheets.Add
Application.DisplayAlerts = False
For Each wks In ActiveWorkbook.Worksheets
If wks.Name < wksNew.Name Then wks.Delete
Next wks
ErrorHandler:
Application.DisplayAlerts = True
End Sub
--
HTH...

Jim Thomlinson


"greg" wrote:

ok,
I am trying to delete all the sheets in a workbook.
I started with something like this:


Sub xxx()
Dim y As Excel.Worksheet
For x = 1 To Excel.ActiveWorkbook.Worksheets.Count
Set y = Excel.ActiveWorkbook.Worksheets(1)
y.Delete
Next x
End Sub


2 problems.
When you delete the sheet, you get a prompt. Is there a way to turn that
prompt off?

I guess I need at least 1 sheet. Is there a way to add a new sheet to the
end of the list of sheets. Then go
For x = 1 To Excel.ActiveWorkbook.Worksheets.Count -1

thanks on any help



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,123
Default deleting sheets

Try this greg

Sub xxx()
Dim ws As Worksheet
Dim sh As Worksheet

Set ws = Worksheets.Add

Application.DisplayAlerts = False

For Each sh In ActiveWorkbook.Worksheets
If sh.Name < ws.Name Then
sh.Delete
End If
Next sh
Application.DisplayAlerts = True
End Sub


--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"greg" wrote in message ...
ok,
I am trying to delete all the sheets in a workbook.
I started with something like this:


Sub xxx()
Dim y As Excel.Worksheet
For x = 1 To Excel.ActiveWorkbook.Worksheets.Count
Set y = Excel.ActiveWorkbook.Worksheets(1)
y.Delete
Next x
End Sub


2 problems.
When you delete the sheet, you get a prompt. Is there a way to turn that
prompt off?

I guess I need at least 1 sheet. Is there a way to add a new sheet to the
end of the list of sheets. Then go
For x = 1 To Excel.ActiveWorkbook.Worksheets.Count -1

thanks on any help


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 140
Default deleting sheets

in responce to your 1st question

I think it is Application.DisplayAlerts = False
might well be wrong

would have to test teh rest
Might post back later if i get the chance


"greg" wrote:

ok,
I am trying to delete all the sheets in a workbook.
I started with something like this:


Sub xxx()
Dim y As Excel.Worksheet
For x = 1 To Excel.ActiveWorkbook.Worksheets.Count
Set y = Excel.ActiveWorkbook.Worksheets(1)
y.Delete
Next x
End Sub


2 problems.
When you delete the sheet, you get a prompt. Is there a way to turn that
prompt off?

I guess I need at least 1 sheet. Is there a way to add a new sheet to the
end of the list of sheets. Then go
For x = 1 To Excel.ActiveWorkbook.Worksheets.Count -1

thanks on any help



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 59
Default deleting sheets

THANKS EVERYONE!!!!!!!!!!!!



"greg" wrote in message
...
ok,
I am trying to delete all the sheets in a workbook.
I started with something like this:


Sub xxx()
Dim y As Excel.Worksheet
For x = 1 To Excel.ActiveWorkbook.Worksheets.Count
Set y = Excel.ActiveWorkbook.Worksheets(1)
y.Delete
Next x
End Sub


2 problems.
When you delete the sheet, you get a prompt. Is there a way to turn that
prompt off?

I guess I need at least 1 sheet. Is there a way to add a new sheet to the
end of the list of sheets. Then go
For x = 1 To Excel.ActiveWorkbook.Worksheets.Count -1

thanks on any help






  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,939
Default deleting sheets

Geez Ron if you are going to steal my code you really need to give me credit...
vbg ;-)

It is almost spooky how similar our 2 solutions are. I must be reading too
many of your posts...
--
HTH...

Jim Thomlinson


"Ron de Bruin" wrote:

Try this greg

Sub xxx()
Dim ws As Worksheet
Dim sh As Worksheet

Set ws = Worksheets.Add

Application.DisplayAlerts = False

For Each sh In ActiveWorkbook.Worksheets
If sh.Name < ws.Name Then
sh.Delete
End If
Next sh
Application.DisplayAlerts = True
End Sub


--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"greg" wrote in message ...
ok,
I am trying to delete all the sheets in a workbook.
I started with something like this:


Sub xxx()
Dim y As Excel.Worksheet
For x = 1 To Excel.ActiveWorkbook.Worksheets.Count
Set y = Excel.ActiveWorkbook.Worksheets(1)
y.Delete
Next x
End Sub


2 problems.
When you delete the sheet, you get a prompt. Is there a way to turn that
prompt off?

I guess I need at least 1 sheet. Is there a way to add a new sheet to the
end of the list of sheets. Then go
For x = 1 To Excel.ActiveWorkbook.Worksheets.Count -1

thanks on any help



  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,123
Default deleting sheets

LOL

--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"Jim Thomlinson" wrote in message
...
Geez Ron if you are going to steal my code you really need to give me credit...
vbg ;-)

It is almost spooky how similar our 2 solutions are. I must be reading too
many of your posts...
--
HTH...

Jim Thomlinson


"Ron de Bruin" wrote:

Try this greg

Sub xxx()
Dim ws As Worksheet
Dim sh As Worksheet

Set ws = Worksheets.Add

Application.DisplayAlerts = False

For Each sh In ActiveWorkbook.Worksheets
If sh.Name < ws.Name Then
sh.Delete
End If
Next sh
Application.DisplayAlerts = True
End Sub


--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"greg" wrote in message ...
ok,
I am trying to delete all the sheets in a workbook.
I started with something like this:


Sub xxx()
Dim y As Excel.Worksheet
For x = 1 To Excel.ActiveWorkbook.Worksheets.Count
Set y = Excel.ActiveWorkbook.Worksheets(1)
y.Delete
Next x
End Sub


2 problems.
When you delete the sheet, you get a prompt. Is there a way to turn that
prompt off?

I guess I need at least 1 sheet. Is there a way to add a new sheet to the
end of the list of sheets. Then go
For x = 1 To Excel.ActiveWorkbook.Worksheets.Count -1

thanks on any help




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 Custom Views when Deleting Sheets ExcelMonkey Excel Programming 1 March 28th 07 06:11 AM
Deleting sheets Brian Matlack[_61_] Excel Programming 3 March 31st 06 07:03 PM
Deleting sheets TK[_3_] Excel Programming 2 August 31st 05 06:02 AM
deleting sheets 2&3 doug53098 Excel Discussion (Misc queries) 2 July 7th 05 08:22 PM
Deleting sheets Gary Adamson[_2_] Excel Programming 2 October 30th 03 07:24 PM


All times are GMT +1. The time now is 05:01 PM.

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

About Us

"It's about Microsoft Excel"