Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 143
Default 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


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default 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




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 143
Default 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






Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro to copy active worksheet to new workbook Macca Excel Discussion (Misc queries) 1 May 25th 08 02:07 PM
Testing for existence of a formula Bob Excel Worksheet Functions 5 November 20th 06 09:31 PM
copy worksheet from closed workbook to active workbook using vba mango Excel Worksheet Functions 6 December 9th 04 07:55 AM
Copy worksheet from Active workbook into all other open workbooks TroyB[_2_] Excel Programming 0 February 10th 04 10:29 AM
Testing for existence of range David Excel Programming 4 November 19th 03 07:39 AM


All times are GMT +1. The time now is 11:18 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"