Testing for existence of a worksheet in the active workbook

  • Thread starter Thread starter Ken Loomis
  • Start date Start date
K

Ken Loomis

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
 
Hi Ken,

Replace the hard coded workbook name:

If WorksheetExists(ActiveWorkbook.Name, "Queries") Then
 
Thnaks, Norman. That did the trick.

Ken Loomis


Norman Jones said:
Hi Ken,

Replace the hard coded workbook name:

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