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

I have a routine which adds sheet to a workbook. It
first detects if they are already there. If so, it
deletes them, then it creates names for them and adds
them back in. At the end of the routine I want to delete
them again. I am using the exact code for deleting the
sheets at the end of the routine as I am at the start.
However the sheets do not get deleted at the end of the
routine. When I typein ?sh2, I get a run time error.
Whey is this?

TempSh2Name = "AuditUFTempH"
TempSh3Name = "AuditUFTempV"

On Error Resume Next
Set sh2 = ActiveWorkbook.Sheets(TempSh2Name)
Set sh3 = ActiveWorkbook.Sheets(TempSh3Name)

If Not sh2 Is Nothing Then
Application.DisplayAlerts = False
sh2.Delete
Application.DisplayAlerts = True
End If

If Not sh3 Is Nothing Then
Application.DisplayAlerts = False
sh3.Delete
Application.DisplayAlerts = True
End If

With ActiveWorkbook
..Worksheets.Add(After:=.Worksheets
(.Worksheets.Count)).Name _
= TempSh2Name

..Worksheets.Add(After:=.Worksheets
(.Worksheets.Count)).Name _
= TempSh3Name
End With


If Not sh2 Is Nothing Then
Application.DisplayAlerts = False
sh2.Delete
Application.DisplayAlerts = True
End If

If Not sh3 Is Nothing Then
Application.DisplayAlerts = False
sh3.Delete
Application.DisplayAlerts = True
End If
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Deleting Sheets

When you delete the sheets, the object variable loses its reference, so it
is effectively cleared. You cannot refer to that object variable later and
expect it to still be pointing at the object . Just because an object has
the same name as a previous object, it is not the same object.

You need to recreate your object variables

Set sh2 = ActiveWorkbook.Sheets(TempSh2Name)
Set sh3 = ActiveWorkbook.Sheets(TempSh3Name)


--

HTH

RP
(remove nothere from the email address if mailing direct)


"ExcelMonkey" wrote in message
...
I have a routine which adds sheet to a workbook. It
first detects if they are already there. If so, it
deletes them, then it creates names for them and adds
them back in. At the end of the routine I want to delete
them again. I am using the exact code for deleting the
sheets at the end of the routine as I am at the start.
However the sheets do not get deleted at the end of the
routine. When I typein ?sh2, I get a run time error.
Whey is this?

TempSh2Name = "AuditUFTempH"
TempSh3Name = "AuditUFTempV"

On Error Resume Next
Set sh2 = ActiveWorkbook.Sheets(TempSh2Name)
Set sh3 = ActiveWorkbook.Sheets(TempSh3Name)

If Not sh2 Is Nothing Then
Application.DisplayAlerts = False
sh2.Delete
Application.DisplayAlerts = True
End If

If Not sh3 Is Nothing Then
Application.DisplayAlerts = False
sh3.Delete
Application.DisplayAlerts = True
End If

With ActiveWorkbook
.Worksheets.Add(After:=.Worksheets
(.Worksheets.Count)).Name _
= TempSh2Name

.Worksheets.Add(After:=.Worksheets
(.Worksheets.Count)).Name _
= TempSh3Name
End With


If Not sh2 Is Nothing Then
Application.DisplayAlerts = False
sh2.Delete
Application.DisplayAlerts = True
End If

If Not sh3 Is Nothing Then
Application.DisplayAlerts = False
sh3.Delete
Application.DisplayAlerts = True
End If



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 sheets 2&3 doug53098 Excel Discussion (Misc queries) 2 July 7th 05 08:22 PM
deleting sheets KLP Excel Discussion (Misc queries) 3 January 14th 05 12:24 AM
Deleting Sheets Jordan[_3_] Excel Programming 4 February 13th 04 03:19 PM
Deleting sheets Gary Adamson[_2_] Excel Programming 2 October 30th 03 07:24 PM
deleting sheets david Excel Programming 2 July 31st 03 01:02 AM


All times are GMT +1. The time now is 09:07 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"