Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Checking if the sheet already exists

Dim dataSh as Worksheet


shName = Application.InputBox("Enter the name of the data sheet")
On Error Resume Next
Set dataSh = Worksheets(shName)
On Error goto 0

If dataSh Is Nothing Then
MsgBox ("The name entered does not exist. Exiting...")
Exit Sub
End If

--
Regards,
Tom Ogilvy


"Greg" wrote in message
...
How about this modification? Can I get rid of one of the if statements?
Thanks!


If dataSh Is Nothing Then
shName = Application.InputBox("Enter the name of the data sheet")
Set dataSh = Worksheets(shName)
End If

If dataSh Is Nothing Then
MsgBox ("The name entered does not exist. Exiting...")
Exit Sub
End If



--
______
Regards,
Greg


"Tom Ogilvy" wrote:

Dim sh as Worksheet
On Error Resume Next
set sh = worksheets("Singles")
ON Error goto 0
if sh is nothing then
set sh = worksheets.Add(After:=Worksheets(worksheets.count) )
sh.Name = "Singles"
End if
' now the variable sh refers to Singles in either case

--
Regards,
Tom Ogilvy

"Greg" wrote:

Hi,

I have the following code now, Is there a more efficient way to do this
?

For Each sh In ActiveWorkbook.Sheets
counter = 0
If sh.Name = "Singles" Then
MsgBox ("Sheet 'Singles' already exists")
counter = 1
End If
Next

If counter = 0 Then
Set sh = Sheets.Add
sh.Name = "Singles"
End If


Thank you,
--
______
Regards,
Greg



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 to see if a file exists. JHB Excel Discussion (Misc queries) 3 August 5th 09 03:11 PM
Checking if Sheet Exists? [email protected] Excel Discussion (Misc queries) 5 September 1st 06 03:27 PM
checking if an url exists Aldo[_2_] Excel Programming 2 October 9th 05 10:20 PM
Checking to see if a worksheet exists Raman325[_27_] Excel Programming 7 August 19th 05 06:36 PM
Checking if a worksheet already exists... JR[_5_] Excel Programming 1 July 14th 04 09:31 PM


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