PC Review


Reply
Thread Tools Rate Thread

Checking if the sheet already exists

 
 
=?Utf-8?B?R3JlZw==?=
Guest
Posts: n/a
 
      8th Nov 2006
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 With Quote
 
 
 
 
=?Utf-8?B?VG9tIE9naWx2eQ==?=
Guest
Posts: n/a
 
      8th Nov 2006
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 With Quote
 
=?Utf-8?B?R3JlZw==?=
Guest
Posts: n/a
 
      8th Nov 2006
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 With Quote
 
Tom Ogilvy
Guest
Posts: n/a
 
      9th Nov 2006
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" <(E-Mail Removed)> wrote in message
news:663E9E06-BE29-49D9-8ADB-(E-Mail Removed)...
> 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 With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
checking if a sheet exists greg Microsoft Excel Programming 6 9th Jul 2008 09:58 PM
Checking if a Chart Exists on a Sheet Keith Wilby Microsoft Excel Programming 6 30th Mar 2007 03:06 PM
Checking if Sheet Exists? brett.kaplan@gmail.com Microsoft Excel Misc 5 1st Sep 2006 03:27 PM
checking if an url exists Aldo Microsoft Excel Programming 2 9th Oct 2005 10:20 PM
checking to see if DDL value exists Darrel Microsoft ASP .NET 4 10th Dec 2004 03:23 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:50 AM.