If sheet exists, then...

J

Jim Thomlinson

if sheetexits("xyz") then
application.displayalerts = false
sheets("xyz").delete
application.displayalerts = true
else
call Macro1
end if

Public 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
 
B

Bob Phillips

typo

if sheetexists("xyz") then
application.displayalerts = false
sheets("xyz").delete
application.displayalerts = true
else
call Macro1
end if


--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)
 
J

Jim Thomlinson

Sorry... There was a typo sheetexits to sheetexists

Sub Test()
if sheetexists("xyz") then
application.displayalerts = false
sheets("xyz").delete
application.displayalerts = true
else
call Macro1
end if
end sub

Public 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
 
D

Darin Kramer

Okay typo fixed. But now for some reason Excel is thinking that the
sheet doesnt exist and is going to the "Else", but I can see the sheet
exists... any ideas...?



*** Sent via Developersdex http://www.developersdex.com ***
 
B

Bob Phillips

It takes the correct path in both situations for me.

--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)
 
A

Andrew Taylor

There's no need to select the sheet before deleting it
(and doing so "forgets" your current selection).

On Error Resume Next
Application.DisplayAlerts = False
ActiveWorkbook.Sheets("xyz").Delete
Application.DisplayAlerts = True
 
D

Dave Peterson

I'd guess a slight change in the spelling of the name.

I'd look for leading/trailing/embedded spaces.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top