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

You could use code like this to delete your worksheet and any Names
referencing it...

Sub DeleteSheetAndItsNames()
Dim N As Name
Dim WS As String
WS = "Sheet3"
For Each N In ThisWorkbook.Names
If Mid(Split(N.RefersTo, "!")(0), 2) = WS Then N.Delete
Next
Worksheets(WS).Delete
End Sub

Just change the worksheet name reference or, better still maybe, make it a
Variant argument to the subroutine (then you can specify the worksheet name
or number) so that your code can call it and specify the worksheet name or
number when doing so. You can then, of course, bolster it up with some error
checking.

Rick


"ranswrt" wrote in message
...
I have a procedure to delete a sheet in a workbook. All the named cells in
that sheet gets deleted. When I use the 'F3' to paste the list of named
cells, the cells in the sheet that was deleted appear in it. Do I need to
delete the named cells before I delete the sheet?

Thanks


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default delete sheet

Well, actually, if you changed it to a Variant argument, you would have to
adapt the If-Then statement to account for that. Something like this
maybe...

Sub DeleteSheetAndItsNames(WS As Variant)
Dim N As Name
Dim S As Worksheet
If WS Like String(Len(WS), "#") Then
If WS <= Worksheets.Count Then
WS = Worksheets(WS).Name
End If
End If
For Each S In Worksheets
If S.Name = WS Then
For Each N In ThisWorkbook.Names
If Mid(Split(N.RefersTo, "!")(0), 2) = WS Then N.Delete
Next
Worksheets(WS).Delete
Exit Sub
End If
Next
MsgBox "No such worksheet!", vbCritical, "No Such Worksheet"
Err.Raise 1111, , "No such worksheet!"
End Sub

I provided a trappable error that can be caught by your calling code. For
example...

Sub Test()
On Error GoTo Whoops
DeleteSheetAndItsNames "SheetX"
Exit Sub
Whoops:
Debug.Print Err.Number, Err.Description
End Sub

Rick


"Rick Rothstein (MVP - VB)" wrote in
message ...
You could use code like this to delete your worksheet and any Names
referencing it...

Sub DeleteSheetAndItsNames()
Dim N As Name
Dim WS As String
WS = "Sheet3"
For Each N In ThisWorkbook.Names
If Mid(Split(N.RefersTo, "!")(0), 2) = WS Then N.Delete
Next
Worksheets(WS).Delete
End Sub

Just change the worksheet name reference or, better still maybe, make it a
Variant argument to the subroutine (then you can specify the worksheet
name or number) so that your code can call it and specify the worksheet
name or number when doing so. You can then, of course, bolster it up with
some error checking.

Rick


"ranswrt" wrote in message
...
I have a procedure to delete a sheet in a workbook. All the named cells
in
that sheet gets deleted. When I use the 'F3' to paste the list of named
cells, the cells in the sheet that was deleted appear in it. Do I need
to
delete the named cells before I delete the sheet?

Thanks



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
Delete ROW on Sheet#1 corupts data on Sheet#2 Duane Excel Worksheet Functions 4 February 11th 10 07:53 PM
Delete values in sheet 2 that arre found in sheet 1 np Excel Discussion (Misc queries) 1 December 10th 09 07:21 PM
Why won't this sheet delete? ragtopcaddy via OfficeKB.com Excel Programming 3 July 2nd 08 11:25 PM
Delete rows from one sheet containing deatils on the second sheet [email protected] New Users to Excel 4 September 6th 07 11:10 AM
delete content of one sheet off another sheet eureka Excel Programming 1 April 4th 06 06:47 AM


All times are GMT +1. The time now is 11:34 AM.

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"