VBA to Check if Worksheet Exists in A Workbook

juan4412

Board Regular
Joined
Oct 17, 2011
Messages
94
I have an array that opens a workbook containing close to 100 worksheets, and copies specific sheet names into there own individual workbooks. The problem I have now, is that I assume the worksheet exists in the workbook, but often times it does not :( Is there a way to add some sort of "catch" that will 1st verify the worksheet exists instead of my code crashing?
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Try like this

Code:
Function WorksheetExists(WSName As String) As Boolean
On Error Resume Next
WorksheetExists = Worksheets(WSName).Name = WSName
On Error GoTo 0
End Function

Sub test()
If Not WorksheetExists("Sheet1") Then
        MsgBox "Unable to proceed", vbExclamation, "Error"
        Exit Sub
End If
End Sub
 
Upvote 0
How would I incorporate that into my current code:
Code:
Dim varBooks
Dim varbook
Dim ws As Excel.Worksheet
Dim wb As Excel.Workbook
 
varSheets = Array("Rain", "Sunshine", "Snow")
 
For Each varSheet In varSheets
    Set wb = Workbooks.Open(Filename:="R:\Weather.xls", ReadOnly:=True)
    With wb.Sheets(varSheet)
        .Copy
        ActiveWorkbook.SaveAs Filename:="O:\WeatherReport" & .Name & VBA.Format(Date, "mmddyyyy") & ".xls"
    End With
Next varSheet

And also if the sheet does not exist, is there a way to "do nothing" and just continue processing like normal?
 
Upvote 0
Try

Code:
For Each varsheet In varSheets
    If worksheetexists(varsheet) Then
        Set wb = Workbooks.Open(Filename:="R:\Weather.xls", ReadOnly:=True)
        With wb.Sheets(varsheet)
            .Copy
            ActiveWorkbook.SaveAs Filename:="O:\WeatherReport" & .Name & VBA.Format(Date, "mmddyyyy") & ".xls"
        End With
    End If
Next varsheet
 
Upvote 0
Here is an alternate method using Select Case...
Without a need to test if the sheet exists.

Rich (BB code):
Dim ws As WorkSheet
For Each ws In Worksheets
    Select Case ws.Name
        Case "Rain", "Sunshine", "Snow"
            'Code here to run on sheets listed above
        Case Else
            'Do Nothing
    End Select
Next ws
 
Upvote 0
VOG trying to use your code, and on the line that says -----
If worksheetexists(varsheet) Then

It throws a compile error of Sub or function is not defined?
 
Upvote 0
You need to add this (which I posted in my first reply)

Code:
Function WorksheetExists(WSName As String) As Boolean
On Error Resume Next
WorksheetExists = Worksheets(WSName).Name = WSName
On Error GoTo 0
End Function
 
Upvote 0
Oh My apologies! Is there a way to put the Function in a module of it's own and set that entire module to "Public" so that every module I want to use the code in can see the Function?

Or does this Function have to be copied into each module that the code is going to be executed in?
 
Upvote 0
If you use the following it can be accessed from any module

Rich (BB code):
Public Function WorksheetExists(WSName As String) As Boolean
On Error Resume Next
WorksheetExists = Worksheets(WSName).Name = WSName
On Error GoTo 0
End Function
 
Upvote 0
I receive a compile error of this
ByRef argument type mismatch
highlighted code is
If WorksheetExists(varsheet) Then
 
Upvote 0

Forum statistics

Threads
1,215,692
Messages
6,126,226
Members
449,303
Latest member
grantrob

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top