Mega Code Archive

 
Categories / VisualBasic Script / Excel
 

Check Whether a Sheet in an Open Workbook Exists

Function SheetExists(SName As String, Optional WB As workBook) As Boolean     Dim WS As Worksheet     If WB Is Nothing Then         Set WB = ActiveWorkbook     End If     On Error Resume Next         SheetExists = CBool(Not WB.Sheets(SName) Is Nothing)     On Error GoTo 0 End Function Sub CheckForSheet()     Dim ShtExists As Boolean     ShtExists = SheetExists("Sheet1")     If ShtExists Then         MsgBox "The worksheet exists!"     Else         MsgBox "The worksheet does NOT exist!"     End If End Sub