Testing for existence of a worksheet in the active workbook

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
 
N

Norman Jones

Hi Ken,

Replace the hard coded workbook name:

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

Ken Loomis

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
 

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

Top