Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default Worksheet Exists...

How do you find out if a worksheet exists or not within a
workbook? I have some code which should only work if a
worksheet (with a known name) exists in the user's
workbook.

Thanks.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Worksheet Exists...

James,

Try the following function:

Function WorksheetExists(WSName As String, _
Optional WB As Workbook) As Boolean
On Error Resume Next
WorksheetExists = CBool(Len(IIf(WB Is Nothing, _
ActiveWorkbook, WB).Worksheets(WSName).Name))
End Function


You can then call this in code with something like

If WorksheetExists("Sheet1",ThisWorkbook) = True Then
' whatever
Else
' whatever else
End If


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
www.cpearson.com


"James Weaver" wrote in message
...
How do you find out if a worksheet exists or not within a
workbook? I have some code which should only work if a
worksheet (with a known name) exists in the user's
workbook.

Thanks.



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 620
Default Worksheet Exists...

James,

Here's one way

Dim oWS As Worksheet

On Error Resume Next
Set oWS = Activeworkbook.Worksheets("Sheet1")
On Error GoTo 0
If oWS Is Nothing Then
MsgBox "Worksheet does not exist"
End If


--

HTH

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

"James Weaver" wrote in message
...
How do you find out if a worksheet exists or not within a
workbook? I have some code which should only work if a
worksheet (with a known name) exists in the user's
workbook.

Thanks.



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default Worksheet Exists...

Thanks - very effective!
-----Original Message-----
James,

Here's one way

Dim oWS As Worksheet

On Error Resume Next
Set oWS = Activeworkbook.Worksheets("Sheet1")
On Error GoTo 0
If oWS Is Nothing Then
MsgBox "Worksheet does not exist"
End If


--

HTH

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

"James Weaver" wrote in message
...
How do you find out if a worksheet exists or not within

a
workbook? I have some code which should only work if a
worksheet (with a known name) exists in the user's
workbook.

Thanks.



.

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
Create Worksheet BUT If It Already Exists... Dave Excel Discussion (Misc queries) 2 October 30th 07 11:13 PM
check if worksheet exists joeeng Excel Worksheet Functions 3 September 7th 05 06:49 PM
How can I tell if a worksheet exists? Robert Stober Excel Programming 5 October 3rd 03 01:00 AM
Test to see if a worksheet exists thanks, Chip CT[_2_] Excel Programming 0 August 22nd 03 03:17 AM
check if worksheet exists Craig Wilks Excel Programming 2 July 10th 03 04:07 AM


All times are GMT +1. The time now is 02:38 PM.

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

About Us

"It's about Microsoft Excel"