ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Does worksheet exist (https://www.excelbanter.com/excel-programming/298718-does-worksheet-exist.html)

Robert S

Does worksheet exist
 
I'm looking for a way to get a boolean result if a given
worksheet name already exists - for example if workbook
text.xls contains sheet1, sheet2, and sheet3, then if I
pass a variable containing the worksheet 'sheet4', I would
get a false value returned. Any help would be appreciated.

Thanks

Harald Staff

Does worksheet exist
 
Function SheetXist(ShName As String) As Boolean
On Error Resume Next
SheetXist = Len(Sheets(ShName).Name)
End Function

Sub Test()
MsgBox SheetXist("Sheet1")
MsgBox SheetXist("Sheet4501")
End Sub

HTH. Best wishes Harald

"Robert S" skrev i melding
...
I'm looking for a way to get a boolean result if a given
worksheet name already exists - for example if workbook
text.xls contains sheet1, sheet2, and sheet3, then if I
pass a variable containing the worksheet 'sheet4', I would
get a false value returned. Any help would be appreciated.

Thanks




[email protected]

Does worksheet exist
 
From a prior posting by Chip Pearson :

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

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

HTH
Paul
--------------------------------------------------------------------------------------------------------------
Be advised to back up your WorkBook before attempting to make changes.
--------------------------------------------------------------------------------------------------------------

I'm looking for a way to get a boolean result if a given
worksheet name already exists - for example if workbook
text.xls contains sheet1, sheet2, and sheet3, then if I
pass a variable containing the worksheet 'sheet4', I would
get a false value returned. Any help would be appreciated.

Thanks



Bob Phillips[_6_]

Does worksheet exist
 
Here is a function


'-----------------------------------------------------------------
Function IsWsSheet(Sh As String, Optional WB As Workbook) As Boolean
'-----------------------------------------------------------------
Dim oWs As Worksheet
If WB Is Nothing Then Set WB = ActiveWorkbook
On Error Resume Next
Set oWs = WB.Worksheets(Sh)
On Error GoTo 0
IsWsSheet = Not oWs Is Nothing
End Functi

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Robert S" wrote in message
...
I'm looking for a way to get a boolean result if a given
worksheet name already exists - for example if workbook
text.xls contains sheet1, sheet2, and sheet3, then if I
pass a variable containing the worksheet 'sheet4', I would
get a false value returned. Any help would be appreciated.

Thanks





All times are GMT +1. The time now is 10:56 AM.

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