Wednesday, June 6, 2012

Check if Worksheet Exists

This is a VBA function that checks if a Worksheet exists. It accepts a worksheet name as a parameter. It loops through worksheet names and stops if it finds the one you are looking for.
Otherwise, it goes through all of them and returns false. Also, it is case-insensitive.

Private Function WorksheetExists(wsName As String) As Boolean
        Dim ws As Worksheet
        Dim ret As Boolean
        ret = False
        wsName = UCase(wsName)
        For Each ws In ThisWorkbook.Sheets
            If UCase(ws.Name) = wsName Then
                ret = True
                Exit For
            End If
        Next
        WorksheetExists = ret
End Function

No comments:

Post a Comment