Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 246
Default Does Sheet Name Exist in a Workbook

Hello All,

Pretty trivial puzzle for someone I'm sure.

Without looping though all the sheets in a workbook is it possible to
find out (TRUE or FALSE) if a sheet named "mySheet" exists within the
activeworkbook ?

I tried setting something up using On Error but still got a run-time
error 9!

Any help much appreciated

Jason
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,298
Default Does Sheet Name Exist in a Workbook

two useful functions

Option Explicit

Sub test1()
MsgBox SheetExists("Sheet1")
End Sub
Sub test2()
Dim text As String
Dim ws As Worksheet
text = "sheet1"
Set ws = GetSheet(text)
MsgBox text & " exists: " & Not (ws Is Nothing)

End Sub


Public Function SheetExists(sName As String) As Boolean
On Error Resume Next
Dim ws As Worksheet
Set ws = Worksheets(sName)
SheetExists = Not (ws Is Nothing)
On Error GoTo 0
End Function
Public Function GetSheet(sName As String) As Worksheet
On Error Resume Next
Dim ws As Worksheet
Set ws = Worksheets(sName)
Set GetSheet = ws
On Error GoTo 0
End Function



"WhytheQ" wrote:

Hello All,

Pretty trivial puzzle for someone I'm sure.

Without looping though all the sheets in a workbook is it possible to
find out (TRUE or FALSE) if a sheet named "mySheet" exists within the
activeworkbook ?

I tried setting something up using On Error but still got a run-time
error 9!

Any help much appreciated

Jason

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,069
Default Does Sheet Name Exist in a Workbook

If you refer to a sheet that does not exist you get an error so at simple
level, just test for that error.

Something like following:

Sub CheckSheet()
Dim WS As Worksheet

On Error Resume Next

Set WS = Worksheets("Sheet3")

If Err 0 Then

MsgBox " Sheet Does Not Exist"

Else

MsgBox "Sheet Exists"

End If

End Sub
--
jb


"WhytheQ" wrote:

Hello All,

Pretty trivial puzzle for someone I'm sure.

Without looping though all the sheets in a workbook is it possible to
find out (TRUE or FALSE) if a sheet named "mySheet" exists within the
activeworkbook ?

I tried setting something up using On Error but still got a run-time
error 9!

Any help much appreciated

Jason

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 246
Default Does Sheet Name Exist in a Workbook

On 16 Sep, 13:18, Patrick Molloy
wrote:
two useful functions

Option Explicit

Sub test1()
MsgBox SheetExists("Sheet1")
End Sub
Sub test2()
Dim text As String
Dim ws As Worksheet
text = "sheet1"
Set ws = GetSheet(text)
MsgBox text & " exists: " & Not (ws Is Nothing)

End Sub

Public Function SheetExists(sName As String) As Boolean
On Error Resume Next
Dim ws As Worksheet
Set ws = Worksheets(sName)
SheetExists = Not (ws Is Nothing)
On Error GoTo 0
End Function
Public Function GetSheet(sName As String) As Worksheet
On Error Resume Next
Dim ws As Worksheet
Set ws = Worksheets(sName)
Set GetSheet = ws
On Error GoTo 0
End Function



"WhytheQ" wrote:
Hello All,


Pretty trivial puzzle for someone I'm sure.


Without looping though all the sheets in a workbook is it possible to
find out (TRUE or FALSE) if a sheet named "mySheet" exists within the
activeworkbook ?


I tried setting something up using On Error but still got a run-time
error 9!


Any help much appreciated


Jason- Hide quoted text -


- Show quoted text -



Nice one Patrick - will add those little beauties to my small
collection of functions

Kind Regards
Jason.
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
does a value exist in another workbook chris 123456 Excel Worksheet Functions 4 February 19th 09 04:58 PM
Does a sheet exist Randall Excel Programming 1 December 12th 08 08:50 PM
If Sheet Exist Peter Excel Programming 4 August 23rd 06 02:00 PM
Copy Worksheet to a new Workbook creating if it doesn't exist and add more Worksheets if it does exist [email protected] Excel Programming 4 June 18th 06 06:08 PM
Does sheet exist? Sean Evanovich Excel Programming 2 November 19th 03 02:30 PM


All times are GMT +1. The time now is 07:17 AM.

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"