ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   VBA code for tab exists (https://www.excelbanter.com/excel-programming/392430-vba-code-tab-exists.html)

MFINE

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

Thanks

Marc

Ron de Bruin

VBA code for tab exists
 
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




--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


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

Thanks

Marc


MFINE

VBA code for tab exists
 
Thanks Ron. Worked great!

Marc

"Ron de Bruin" wrote:

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




--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


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

Thanks

Marc




All times are GMT +1. The time now is 05:18 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com