Does sheet exist?

S

Sean Evanovich

What's the proper way to check and see if a specific
sheet exits in the workbook?

I need to pull data from this sheet IF it exists...and if
it doesn't exist I need to pull data from another sheet I
know will be there...

If "Multi-Acct Summary" exists then
GetDataFromMultiAccount
Else
GetDataFromSingleAccount
End if

Thanks!!!
 
C

Chip Pearson

Sean,

Use a function like

Function SheetExists(SheetName As String) As Boolean
On Error Resume Next
SheetExists = CBool(Len(ThisWorkbook.Worksheets(SheetName).Name))
End Function


Then, you can call this with code like

If SheetExists("Sheet1") = True Then
' do something
Else
' do something else
End If


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
 
D

Don Guillett

Try this. If sheet 22 does not exist it gets info from sheet 2.

Sub getifthere()
On Error Resume Next
MsgBox Sheets("Sheet22").Range("c5")
MsgBox Sheets("Sheet2").Range("c5")
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top