Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Help with code that checks if a sheet exists

I use the following code to check if a worksheet exists. It works OK if the
worksheet does exist, However, if the worksheet does not exist, then
the "Set" statement gives a "Subscript out of range" error.

Can someone please help? Thanks.

Sub checkit ()
Dim NewTabName As String, WkSht As Worksheet
NewTabName = "1-23-04"

Set WkSht = Workbooks(CurBookName).Worksheets(NewTabName)
If Not WkSht Is Nothing Then
MsgBox "The worksheet exists"
Else
Workbooks(CurBookName).Activate
Workbooks(CurBookName).Sheets.Add.Name = NewTabName
End If
End Sub


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 100
Default Help with code that checks if a sheet exists

Wache

One way is to add the lin
On Error Resume Nex
before the Set command

Also, you should initialise the CurBookName variable

Ton

----- wachen wrote: ----

I use the following code to check if a worksheet exists. It works OK if th
worksheet does exist, However, if the worksheet does not exist, the
the "Set" statement gives a "Subscript out of range" error

Can someone please help? Thanks

Sub checkit (
Dim NewTabName As String, WkSht As Workshee
NewTabName = "1-23-04

Set WkSht = Workbooks(CurBookName).Worksheets(NewTabName
If Not WkSht Is Nothing The
MsgBox "The worksheet exists
Els
Workbooks(CurBookName).Activat
Workbooks(CurBookName).Sheets.Add.Name = NewTabNam
End I
End Su



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 340
Default Help with code that checks if a sheet exists

Wachen, make the following changes:

On error resume next
Set WkSht = Workbooks(CurBookName).Worksheets(NewTabName)
On error goto 0

The first on error statement allows the following line to run with an error.
The second turns off this feature.

Bob Flanagan
Macro Systems
http://www.add-ins.com
Productivity add-ins and downloadable books on VB macros for Excel


"wachen" wrote in message
news:L0XVb.13830$QA2.24788@attbi_s52...
I use the following code to check if a worksheet exists. It works OK if

the
worksheet does exist, However, if the worksheet does not exist, then
the "Set" statement gives a "Subscript out of range" error.

Can someone please help? Thanks.

Sub checkit ()
Dim NewTabName As String, WkSht As Worksheet
NewTabName = "1-23-04"

Set WkSht = Workbooks(CurBookName).Worksheets(NewTabName)
If Not WkSht Is Nothing Then
MsgBox "The worksheet exists"
Else
Workbooks(CurBookName).Activate
Workbooks(CurBookName).Sheets.Add.Name = NewTabName
End If
End Sub




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
check if sheet exists mohavv Excel Discussion (Misc queries) 1 November 21st 07 01:58 AM
Micro that checks if file exists pete Excel Worksheet Functions 8 March 24th 05 04:47 PM
Sheet name already exists eddie_zoom Excel Discussion (Misc queries) 1 March 11th 05 02:53 PM
How can I know if a sheet exists ? Ben.C Excel Programming 3 December 29th 03 09:36 AM
check if sheet exists Ross[_6_] Excel Programming 3 July 25th 03 06:46 PM


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