Submitted By: Kent Finkle

Verifies that a worksheet exists in a Microsoft Excel spreadsheet.

Visual Basic
Edit|Remove
'*  Script name:    WorksheetExists.vbs
'*  Created on:	    10/24/2006
'*  Author:         Kent Finkle
'*  Purpose:        Verify that an Excel Worksheet exists.

If SheetExists("C:\scripts\test.xls","Sheet1") Then
    Wscript.Echo "Exists"
Else
    Wscript.Echo "Does not exist"
End If

If SheetExists("C:\scripts\test.xls","Sheet2") Then
    Wscript.Echo "Exists"
Else
    Wscript.Echo "Does not exist"
End If

Function SheetExists(strFileName,strSheetName)

Dim objExcel
Dim objWorkbook
Dim objWorksheet
Dim blnSheetExists

Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("C:\scripts\test.xls")

SheetExists = False

For Each objWorksheet In objWorkbook.Worksheets

    If objWorksheet.Name = strSheetName Then
        SheetExists = True
        Exit For
    End If

Next

objExcel.Quit

End Function