How to Check if a Worksheet Exists in Excel VBA

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

This is a sample for deleting a worksheet if exists

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