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

how do I find out if a sheet exist to delete it? I've tried:

if not isnull(sheets("myname")) then
sheets("myname").delete
end if

thanks,

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,123
Default delete sheet if it exist

Hi Art

You can test it like this with a function

Sub Sheet_Test()
Dim SName As String
If SheetExists("test") = True Then
'code to delete the sheet
Else
'do nothing
End If
End Sub

Function SheetExists(SName As String, _
Optional ByVal wb As Workbook) As Boolean
'Chip Pearson
On Error Resume Next
If wb Is Nothing Then Set wb = ThisWorkbook
SheetExists = CBool(Len(wb.Sheets(SName).Name))
End Function


--
Regards Ron de Bruin
http://www.rondebruin.nl


"Art Parra" wrote in message ...
how do I find out if a sheet exist to delete it? I've tried:

if not isnull(sheets("myname")) then
sheets("myname").delete
end if

thanks,



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default delete sheet if it exist

application.displayalerts = false
on error resume next
worksheets("Myname").delete
on error goto 0
application.displayalerts = true

If the sheet doesn't exist, then the error is ignored.

(The .displayalerts stuff stops the confirmation dialog from showing up.)

=====
Chip Pearson posted this function:


Function WorksheetExists(SheetName As Variant, _
Optional WhichBook As Workbook) As Boolean
'from Chip Pearson
Dim WB As Workbook
Set WB = IIf(WhichBook Is Nothing, ThisWorkbook, WhichBook)
On Error Resume Next
WorksheetExists = CBool(Len(WB.Worksheets(SheetName).Name) 0)
End Function

'and you can use it like:
....
if worksheetexists("myname",activeworkbook) then
application.displayalerts = false
worksheets("Myname").delete
application.displayalerts = true
end if


Art Parra wrote:

how do I find out if a sheet exist to delete it? I've tried:

if not isnull(sheets("myname")) then
sheets("myname").delete
end if

thanks,


--

Dave Peterson
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
VBA: Make a new sheet if it doesn't exist PaulW Excel Discussion (Misc queries) 2 May 8th 06 03:45 PM
VBA, Make a new sheet if it doesn't exist PaulW Excel Discussion (Misc queries) 1 May 5th 06 05:25 PM
Does the sheet exist? Dr.Schwartz Excel Programming 1 August 25th 04 02:16 PM
test if a sheet exist (with the name) ? LoloSoft Excel Programming 4 February 17th 04 02:39 PM
Does sheet exist? Sean Evanovich Excel Programming 2 November 19th 03 02:30 PM


All times are GMT +1. The time now is 09:01 AM.

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"