VBA code for tab exists

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Looking for code or a statement that comfirms if a tab in excel exists,
similar to FileExists method. Any suggestions?

Thanks

Marc
 
Hi Marc

There are many ways but I like to use a function for it

Function SheetExists(SName As String, _
Optional ByVal WB As Workbook) As Boolean
'Chip Pearson
On Error Resume Next
If WB Is Nothing Then Set WB = ThisWorkbook
SheetExists = CBool(Len(WB.Sheets(SName).Name))
End Function


You can call the function in Your macro like this

If SheetExists("Sheet1") = False Then
'not exist
Else
'exist
End If
 
Thanks Ron. Worked great!

Marc

Ron de Bruin said:
Hi Marc

There are many ways but I like to use a function for it

Function SheetExists(SName As String, _
Optional ByVal WB As Workbook) As Boolean
'Chip Pearson
On Error Resume Next
If WB Is Nothing Then Set WB = ThisWorkbook
SheetExists = CBool(Len(WB.Sheets(SName).Name))
End Function


You can call the function in Your macro like this

If SheetExists("Sheet1") = False Then
'not exist
Else
'exist
End If
 

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

Back
Top