How can I tell if sheet a exists in a workbook?
I'd make a separate function for it:
Function SheetExists(SheetName As String)
On Error GoTo no:
WorksheetName = Worksheets(SheetName).Name
SheetExists = True
Exit Function
no:
SheetExists = False
End Function
Then you can easily call it where needed, even in a formula if you wanted:
Sub ABC()
If SheetExists("Test") Then
MsgBox "Yay!"
Else
MsgBox "Boo!"
End If
End Sub
or
=If(SheetExists("Test"),"Yay!","Boo")
Something like:
Sub DoesSheetExist()
Dim s As Worksheet
For Each s In Sheets
If s.Name = "a" Then
MsgBox "Sheet a exists"
Exit Sub
End If
Next s
MsgBox "Sheet a does not exist"
End Sub
You can check for the error. eg:
Dim A As String
On Error Resume Next
A = Worksheets("a").Name
Select Case Err.Number
Case 9
MsgBox "Sheet ""a"" does not exist"
Case 0
MsgBox "Sheet ""a"" exists"
Case Else
Stop
End Select
On Error GoTo 0