Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 16
Default Checking for existence of worksheet before creating


Hi everyone,

You guys have been so helpful, I can't help but ask you again to help me
with a problem I can't seem to get around.

I wanna check if a worksheet exists before creating a new worksheet. I
stole bits from other questions on this forum, and came up with :


Workbooks(tata).Activate

Set sh = Worksheets(toto)
On Error Resume Next
If sh Is Nothing Then
Worksheets.Add.Name = Worksheets(toto)
Else
sh.Activate
End If



my strings "tata" and "toto" work fine elsewhere, but I gather that I get a
"Subscript out of range" at the line : "Set sh = Worksheets(toto)"
because worksheet "toto" doesn't exist.....but that's the purpose of this
thing, to check if it exists.

Yeah, I know I should also get help to find better variable names :-)

Thanks in advance for your help
Marc


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 644
Default Checking for existence of worksheet before creating

Move your on error resume next before the set command. Oh, and don't
forget to reset your error handling (On error goto 0)

Die_Another_Day
Marc Gendron wrote:
Hi everyone,

You guys have been so helpful, I can't help but ask you again to help me
with a problem I can't seem to get around.

I wanna check if a worksheet exists before creating a new worksheet. I
stole bits from other questions on this forum, and came up with :


Workbooks(tata).Activate

Set sh = Worksheets(toto)
On Error Resume Next
If sh Is Nothing Then
Worksheets.Add.Name = Worksheets(toto)
Else
sh.Activate
End If



my strings "tata" and "toto" work fine elsewhere, but I gather that I get a
"Subscript out of range" at the line : "Set sh = Worksheets(toto)"
because worksheet "toto" doesn't exist.....but that's the purpose of this
thing, to check if it exists.

Yeah, I know I should also get help to find better variable names :-)

Thanks in advance for your help
Marc


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 703
Default Checking for existence of worksheet before creating

You didn't try the one I sent you the other day?

CreateSheet "toto"

here is the sub again:

Public Sub CreateSheet(SheetName As String)
'
' if the sheet already exists select it
'
On Error GoTo CreateNewSheet
Sheets(SheetName).Select
Exit Sub
'
' if not create it
'
CreateNewSheet:
Sheets.Add After:=Sheets(Sheets.Count)
Sheets(Sheets.Count).Name = SheetName
'
End Sub


"Marc Gendron" wrote:


Hi everyone,

You guys have been so helpful, I can't help but ask you again to help me
with a problem I can't seem to get around.

I wanna check if a worksheet exists before creating a new worksheet. I
stole bits from other questions on this forum, and came up with :


Workbooks(tata).Activate

Set sh = Worksheets(toto)
On Error Resume Next
If sh Is Nothing Then
Worksheets.Add.Name = Worksheets(toto)
Else
sh.Activate
End If



my strings "tata" and "toto" work fine elsewhere, but I gather that I get a
"Subscript out of range" at the line : "Set sh = Worksheets(toto)"
because worksheet "toto" doesn't exist.....but that's the purpose of this
thing, to check if it exists.

Yeah, I know I should also get help to find better variable names :-)

Thanks in advance for your help
Marc


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 16
Default Checking for existence of worksheet before creating

I musta got lost.......

Well, I tried it this time, and it worked on the first try.

YĆ© !!!!

Thanks,
Marc

"Charlie" wrote:

You didn't try the one I sent you the other day?

CreateSheet "toto"

here is the sub again:

Public Sub CreateSheet(SheetName As String)
'
' if the sheet already exists select it
'
On Error GoTo CreateNewSheet
Sheets(SheetName).Select
Exit Sub
'
' if not create it
'
CreateNewSheet:
Sheets.Add After:=Sheets(Sheets.Count)
Sheets(Sheets.Count).Name = SheetName
'
End Sub


"Marc Gendron" wrote:


Hi everyone,

You guys have been so helpful, I can't help but ask you again to help me
with a problem I can't seem to get around.

I wanna check if a worksheet exists before creating a new worksheet. I
stole bits from other questions on this forum, and came up with :


Workbooks(tata).Activate

Set sh = Worksheets(toto)
On Error Resume Next
If sh Is Nothing Then
Worksheets.Add.Name = Worksheets(toto)
Else
sh.Activate
End If



my strings "tata" and "toto" work fine elsewhere, but I gather that I get a
"Subscript out of range" at the line : "Set sh = Worksheets(toto)"
because worksheet "toto" doesn't exist.....but that's the purpose of this
thing, to check if it exists.

Yeah, I know I should also get help to find better variable names :-)

Thanks in advance for your help
Marc


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
Checking for the existence of a worksheet Allen Excel Discussion (Misc queries) 1 January 20th 10 06:57 PM
Checking for existence of value in another sheet [email protected] Excel Programming 2 October 20th 04 02:51 PM
Checking for existence of excel Dave Peterson[_3_] Excel Programming 3 June 26th 04 05:11 AM
Checking for existence of excel keepITcool Excel Programming 0 June 24th 04 06:07 PM
Checking for the Existence of a Folder Viswanath Tumu Excel Programming 2 April 19th 04 08:31 AM


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