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?
 
Sorry, try like this

Code:
Dim varBooks
Dim varbook
Dim varsheet
Dim varsheets(0 To 2) As String
Dim ws As Excel.Worksheet
Dim wb As Excel.Workbook
Dim i
 varsheets(0) = "Rain"
 varsheets(1) = "Sunshine"
 varsheets(2) = "Snow"
 
For i = LBound(varsheets) To UBound(varsheets)
    MsgBox WorksheetExists(varsheets(i))
Next i
 
Upvote 0

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
I added your code and then added in the copy code that I had previously..I may have added it in the wrong place, since I am getting a debug error or this message Obect Variable or With Block Variable Not Set
Code:
Dim varBooks
Dim varbook
Dim varsheet
Dim varsheets(0 To 2) As String
Dim ws As Excel.Worksheet
Dim wb As Excel.Workbook
Dim i
 varsheets(0) = "Rain"
 varsheets(1) = "Sunshine"
 varsheets(2) = "Snow"
 
For i = LBound(varsheets) To UBound(varsheets)
        With wb.Sheets(varsheet)
            .Copy
            ActiveWorkbook.SaveAs Filename:="O:\WeatherReport" & .Name & VBA.Format(Date, "mmddyyyy") & ".xls"
        End With
Next i
 
Upvote 0
I thought at the top of the code where I set
Code:
Dim wb As Excel.Workbook

That gave wb a value?

Anywho...I tried to change it to ActiveWorkbook.Name, and Workbook.Name and both of those throw an error. What value would I need to set this as so that it copies the specific sheet in the array it is on?
 
Upvote 0
Try

Rich (BB code):
Dim varBooks
Dim varbook
Dim varsheet
Dim varsheets(0 To 2) As String
Dim ws As Excel.Worksheet
Dim wb As Excel.Workbook
Dim i
 varsheets(0) = "Rain"
 varsheets(1) = "Sunshine"
 varsheets(2) = "Snow"
Set wb = ActiveWorkbook
For i = LBound(varsheets) To UBound(varsheets)
        With wb.Sheets(varsheet)
            .Copy
            ActiveWorkbook.SaveAs Filename:="O:\WeatherReport" & .Name & VBA.Format(Date, "mmddyyyy") & ".xls"
        End With
Next i
 
Upvote 0
Try

Code:
Dim varBooks
Dim varbook
Dim varsheet
Dim varsheets(0 To 2) As String
Dim ws As Excel.Worksheet
Dim wb As Excel.Workbook
Dim i
 varsheets(0) = "Rain"
 varsheets(1) = "Sunshine"
 varsheets(2) = "Snow"
Set wb = ActiveWorkbook
For i = LBound(varsheets) To UBound(varsheets)
    If WorksheetExists(varsheets(i)) Then
        With wb.Sheets(varsheet)
            .Copy
            ActiveWorkbook.SaveAs Filename:="O:\WeatherReport" & .Name & VBA.Format(Date, "mmddyyyy") & ".xls"
        End With
    End If
Next i
 
Upvote 0
That debugs okay, but I just realized It no longer has my workbook open command in there !!!!

Thank you for your patience with me
 
Upvote 0
Maybe

Code:
Dim varBooks
Dim varbook
Dim varsheet
Dim varsheets(0 To 2) As String
Dim ws As Excel.Worksheet
Dim wb As Excel.Workbook
Dim i
 varsheets(0) = "Rain"
 varsheets(1) = "Sunshine"
 varsheets(2) = "Snow"
Set wb = Workbooks.Open(Filename:="R:\Weather.xls", ReadOnly:=True)
For i = LBound(varsheets) To UBound(varsheets)
    If WorksheetExists(varsheets(i)) Then
        With wb.Sheets(varsheet)
            .Copy
            ActiveWorkbook.SaveAs Filename:="O:\WeatherReport" & .Name & VBA.Format(Date, "mmddyyyy") & ".xls"
        End With
    End If
Next i
 
Upvote 0

Forum statistics

Threads
1,216,144
Messages
6,129,120
Members
449,488
Latest member
qh017

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