ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Testing for existence of a worksheet in the active workbook (https://www.excelbanter.com/excel-programming/312600-testing-existence-worksheet-active-workbook.html)

Ken Loomis

Testing for existence of a worksheet in the active workbook
 
I need to add a new workseet to the active workbook, but I need test for its
existence before I do.

I used this:

If WorksheetExists("BuildQueries.xls", "Queries") Then
MsgBox ("sheet does exist")
Else
MsgBox ("sheet does NOT exist")
End If

with this function:

Function WorksheetExists(WBName As String, WSName As String) As Boolean

On Error Resume Next
WorksheetExists = _
(Workbooks(WBName).Worksheets(WSName).Name = WSName)
End Function


It works fine, but the name of the file may not always be
"BuildQueries.xls".

What do I need to use in place of "BuildQueries.xls" in the function call to
WorksheetExists to make it reference the active workbook?

Thanks,
Ken Loomis



Norman Jones

Testing for existence of a worksheet in the active workbook
 
Hi Ken,

Replace the hard coded workbook name:

If WorksheetExists(ActiveWorkbook.Name, "Queries") Then


---
Regards,
Norman



"Ken Loomis" wrote in message
...
I need to add a new workseet to the active workbook, but I need test for
its existence before I do.

I used this:

If WorksheetExists("BuildQueries.xls", "Queries") Then
MsgBox ("sheet does exist")
Else
MsgBox ("sheet does NOT exist")
End If

with this function:

Function WorksheetExists(WBName As String, WSName As String) As Boolean

On Error Resume Next
WorksheetExists = _
(Workbooks(WBName).Worksheets(WSName).Name = WSName)
End Function


It works fine, but the name of the file may not always be
"BuildQueries.xls".

What do I need to use in place of "BuildQueries.xls" in the function call
to WorksheetExists to make it reference the active workbook?

Thanks,
Ken Loomis





Ken Loomis

Testing for existence of a worksheet in the active workbook
 
Thnaks, Norman. That did the trick.

Ken Loomis


"Norman Jones" wrote in message
...
Hi Ken,

Replace the hard coded workbook name:

If WorksheetExists(ActiveWorkbook.Name, "Queries") Then


---
Regards,
Norman



"Ken Loomis" wrote in message
...
I need to add a new workseet to the active workbook, but I need test for
its existence before I do.

I used this:

If WorksheetExists("BuildQueries.xls", "Queries") Then
MsgBox ("sheet does exist")
Else
MsgBox ("sheet does NOT exist")
End If

with this function:

Function WorksheetExists(WBName As String, WSName As String) As Boolean

On Error Resume Next
WorksheetExists = _
(Workbooks(WBName).Worksheets(WSName).Name = WSName)
End Function


It works fine, but the name of the file may not always be
"BuildQueries.xls".

What do I need to use in place of "BuildQueries.xls" in the function call
to WorksheetExists to make it reference the active workbook?

Thanks,
Ken Loomis








All times are GMT +1. The time now is 10:00 PM.

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