ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   delete sheet if it exist (https://www.excelbanter.com/excel-programming/352653-delete-sheet-if-exist.html)

Art Parra

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,


Ron de Bruin

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,




Dave Peterson

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


All times are GMT +1. The time now is 12:33 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com