Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming,microsoft.public.scripting.vbscript
external usenet poster
 
Posts: 26
Default How can I tell if a worksheet exists?

Hi,

I want to determine if a worksheet named Sheets("Chart") exists. How can I
do this using VBA?

Thank you,

Robert Stober


  #2   Report Post  
Posted to microsoft.public.excel.programming,microsoft.public.scripting.vbscript
external usenet poster
 
Posts: 45
Default How can I tell if a worksheet exists?

Hi Robert:

Function WorksheetExists(wsName As String, _
Optional wbName As String) As Boolean
If wbName = "" Then wbName = _
ActiveWorkbook.Name
On Error Resume Next
WorksheetExists = CBool(Len(Workbooks(wbName) _
.Worksheets(wsName).Name))
End Function

?WorksheetExists("Charts")
False 'True

Regards,

Vasant.

"Robert Stober" wrote in message
...
Hi,

I want to determine if a worksheet named Sheets("Chart") exists. How can I
do this using VBA?

Thank you,

Robert Stober




  #3   Report Post  
Posted to microsoft.public.excel.programming,microsoft.public.scripting.vbscript
external usenet poster
 
Posts: 620
Default How can I tell if a worksheet exists?

Robert,

Here's some code to show the principle

Dim oWS As Worksheet

On Error Resume Next
Set oWS = Activeworkbook.Worksheets("Chart")
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)

"Robert Stober" wrote in message
...
Hi,

I want to determine if a worksheet named Sheets("Chart") exists. How can I
do this using VBA?

Thank you,

Robert Stober




  #4   Report Post  
Posted to microsoft.public.excel.programming,microsoft.public.scripting.vbscript
external usenet poster
 
Posts: 1
Default How can I tell if a worksheet exists?

"Robert Stober" wrote in
:

Hi,

I want to determine if a worksheet named Sheets("Chart") exists. How
can I do this using VBA?

Thank you,

Robert Stober



An alternate way which does not "abuse" the error mechanism:
(function header shamelessly stolen from V. Nanavati)

Function WorksheetExists(wsName As String, _
Optional wbName As String) As Boolean
If wbName = "" Then wbName = _
ActiveWorkbook.Name
Dim x as Variant
WorksheetExists = false
For Each x In Workbooks(wbName).Worksheets
If x.Name = wsName Then
WorksheetExists = True
Exit Function
Next
End Function

(I know it's silly to call it abuse, but I always get caught by
forgetting to do On Error Goto 0, so I tend to avoid using it as an
exception mechanism)
  #5   Report Post  
Posted to microsoft.public.excel.programming,microsoft.public.scripting.vbscript
external usenet poster
 
Posts: 27
Default How can I tell if a worksheet exists?

Sub Test()
MsgBox SheetExist("A")
End Sub

Function SheetExist(shName) As Boolean
Dim sh As Worksheet

On Error GoTo Err_SheetExist

Set sh = Worksheets(shName) 'if worksheet exist, object sh is set
SheetExist = True
Set sh = Nothing 'we don't need this object

Exit Function

Err_SheetExist:
SheetExist = False 'worksheet not existing
Set sh = Nothing 'free memory
End Function


--
Sorry for my language, I'm still learning
losmac


Użytkownik "Robert Stober" napisał w wiadomości
...
Hi,

I want to determine if a worksheet named Sheets("Chart") exists. How can I
do this using VBA?

Thank you,

Robert Stober






  #6   Report Post  
Posted to microsoft.public.excel.programming,microsoft.public.scripting.vbscript
external usenet poster
 
Posts: 26
Default How can I tell if a worksheet exists?

Thank you all for your generous response. I thought of this one before I saw
your collective responses:

' Add the chart sheet if needed
On Error Resume Next
Sheets("Chart").Select
If Err.Number < 0 Then
Sheets.Add
ActiveSheet.Name = "Chart"
End If

I've never used functions before - looks like I need to expand my
vocabulary!

Thank you,

Robert

"losmac" wrote in message
...
Sub Test()
MsgBox SheetExist("A")
End Sub

Function SheetExist(shName) As Boolean
Dim sh As Worksheet

On Error GoTo Err_SheetExist

Set sh = Worksheets(shName) 'if worksheet exist, object sh is set
SheetExist = True
Set sh = Nothing 'we don't need this object

Exit Function

Err_SheetExist:
SheetExist = False 'worksheet not existing
Set sh = Nothing 'free memory
End Function


--
Sorry for my language, I'm still learning
losmac


Użytkownik "Robert Stober" napisał w wiadomości
...
Hi,

I want to determine if a worksheet named Sheets("Chart") exists. How can

I
do this using VBA?

Thank you,

Robert Stober






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
Test to see if a worksheet exists thanks, Chip CT[_2_] Excel Programming 0 August 22nd 03 03:17 AM
Determining if a worksheet exists within a workbook Cory Schneider Excel Programming 1 July 17th 03 12:36 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 12:39 PM.

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"