There is a couple of ways to do this.
Solutions 1, use a for – next loop to check worksheet one by one.
Function WorksheetExists(ByVal WorksheetName As String) As Boolean
Dim Sht As Worksheet
For Each Sht In ThisWorkbook.Worksheets
If Application.Proper(Sht.Name) = Application.Proper(WorksheetName) Then
WorksheetExists = True
Exit Function
End If
Next Sht
WorksheetExists = False
End Function
Solution 2,
Setting the non-existent sheet as an object and then blowing through the errors is clever indeed. Also learned from the use of wildcard * in the second suggestion.
Public Function WorksheetExists(ByVal WorksheetName As String) As Boolean
On Error Resume Next
WorksheetExists = (Sheets(WorksheetName).Name <> “”)
On Error GoTo 0
End Function