Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 28
Default Test for the existence of a worksheet?

Hi,

I need to test for the existence of a worksheet with a given name.
Presently I'm using some code I found on the net like:

On Error Resume Next
Set xlobj = Workbooks(WorkBookName).Worksheets(SheetName)
If Err = 0 Then
do something
else
do another thing

End If

I find that this is an awkward way to perform the test because my
If..Then..Else is False no matter which error occurs.

Is there a more definitive way to perform the test?

TIA
--
Damien
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,510
Default Test for the existence of a worksheet?

Hi Damien,

Try this.

Sub Test_For_Sheet_Name()

Dim Sht
Dim GivenShtName

GivenShtName = "Sheet1"

For Each Sht In Sheets
If Sht.Name = GivenShtName Then
MsgBox "The sheet name " & GivenShtName & " found"
End If
Next Sht

End Sub

Regards,

OssieMac

"Damien McBain" wrote:

Hi,

I need to test for the existence of a worksheet with a given name.
Presently I'm using some code I found on the net like:

On Error Resume Next
Set xlobj = Workbooks(WorkBookName).Worksheets(SheetName)
If Err = 0 Then
do something
else
do another thing

End If

I find that this is an awkward way to perform the test because my
If..Then..Else is False no matter which error occurs.

Is there a more definitive way to perform the test?

TIA
--
Damien

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,101
Default Test for the existence of a worksheet?

One way

Sub isitthere()
Dim wSheet As Worksheet
On Error Resume Next
Set wSheet = Sheets("Sheet1") 'change as required
If wSheet Is Nothing Then
MsgBox "Worksheet does not exist", vbCritical
Set wSheet = Nothing
On Error GoTo 0
Else
MsgBox "Sheet 1 does exist", vbInformation
Set wSheet = Nothing
On Error GoTo 0
End If
End Sub

Mike

"Damien McBain" wrote:

Hi,

I need to test for the existence of a worksheet with a given name.
Presently I'm using some code I found on the net like:

On Error Resume Next
Set xlobj = Workbooks(WorkBookName).Worksheets(SheetName)
If Err = 0 Then
do something
else
do another thing

End If

I find that this is an awkward way to perform the test because my
If..Then..Else is False no matter which error occurs.

Is there a more definitive way to perform the test?

TIA
--
Damien

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,646
Default Test for the existence of a worksheet?

Not my invention, I found this function in this forum, but I forgot the
author's name, sorry!

Function SheetExists(Sh As String, _
Optional wb As Workbook) As Boolean
'-----------------------------------------------------------------
Dim oWs As Worksheet
If wb Is Nothing Then Set wb = ActiveWorkbook
On Error Resume Next
SheetExists = CBool(Not wb.Worksheets(Sh) Is Nothing)
On Error GoTo 0
End Function

Regards,
Stefi

€žDamien McBain€ť ezt Ă*rta:

Hi,

I need to test for the existence of a worksheet with a given name.
Presently I'm using some code I found on the net like:

On Error Resume Next
Set xlobj = Workbooks(WorkBookName).Worksheets(SheetName)
If Err = 0 Then
do something
else
do another thing

End If

I find that this is an awkward way to perform the test because my
If..Then..Else is False no matter which error occurs.

Is there a more definitive way to perform the test?

TIA
--
Damien

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Test for the existence of a worksheet?

It looks like Chip Pearson's.

Stefi wrote:

Not my invention, I found this function in this forum, but I forgot the
author's name, sorry!

Function SheetExists(Sh As String, _
Optional wb As Workbook) As Boolean
'-----------------------------------------------------------------
Dim oWs As Worksheet
If wb Is Nothing Then Set wb = ActiveWorkbook
On Error Resume Next
SheetExists = CBool(Not wb.Worksheets(Sh) Is Nothing)
On Error GoTo 0
End Function

Regards,
Stefi

€žDamien McBain€ť ezt Ă*rta:

Hi,

I need to test for the existence of a worksheet with a given name.
Presently I'm using some code I found on the net like:

On Error Resume Next
Set xlobj = Workbooks(WorkBookName).Worksheets(SheetName)
If Err = 0 Then
do something
else
do another thing

End If

I find that this is an awkward way to perform the test because my
If..Then..Else is False no matter which error occurs.

Is there a more definitive way to perform the test?

TIA
--
Damien


--

Dave Peterson


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 28
Default Test for the existence of a worksheet?

Dave Peterson wrote:

It looks like Chip Pearson's.

Stefi wrote:

Not my invention, I found this function in this forum, but I forgot the
author's name, sorry!

Function SheetExists(Sh As String, _
Optional wb As Workbook) As Boolean
'-----------------------------------------------------------------
Dim oWs As Worksheet
If wb Is Nothing Then Set wb = ActiveWorkbook
On Error Resume Next
SheetExists = CBool(Not wb.Worksheets(Sh) Is Nothing)
On Error GoTo 0
End Function


Thanks y'all :)
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
Test for file existence Joseph[_46_] Excel Programming 1 November 12th 04 08:56 PM
Test for file existence Joseph[_45_] Excel Programming 1 November 12th 04 04:04 PM
Existence, Search, Test JohnNews Excel Programming 0 October 11th 04 05:48 PM
Test for existence of Name Jack Schitt Excel Programming 2 April 6th 04 06:51 PM
how do i test for the existence of a range name Jake Marx[_3_] Excel Programming 2 March 3rd 04 09:57 PM


All times are GMT +1. The time now is 02:41 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"