PC Review


Reply
Thread Tools Rate Thread

Another 'Sheet Exists' Question...

 
 
=?Utf-8?B?VHJldm9yIFdpbGxpYW1z?=
Guest
Posts: n/a
 
      1st Aug 2007
Dear All

I'm struggling with working out if a sheet exists in my workbook. I've used
code posted from this group but need to tweek it to create a sheet name based
on a couple of cell values. When I use the code below, Run Time Error 9 is
returned.

myShtName creates a string 'prd1 Product Name' - this is the sheet name I'm
looking for - if it exists then no action is needed, otherwise copy a
template sheet at the end of the workbook, and name the sheet 'prd1 Product
Name'.

As usual, any help gratefully appreciated

Trevor.
--------------------------------------
Dim rng As Range, cell As Range
Dim ws As Workksheet
Dim myShtName As String

Set rng = Range("productlist")

For Each cell In rng
If cell <> "" Then
myShtName = "prd" & cell.Offset(0, -1).Value & " " & cell.Value
Set ws = Worksheets(myShtName)
If ws Is Nothing Then
Sheets("Product Template").Copy After:=Sheets(Sheets.Count)
ActiveSheet.Name = myShtName
Range("B3") = cell.Value
End If
End If
Next cell

 
Reply With Quote
 
 
 
 
Peter T
Guest
Posts: n/a
 
      1st Aug 2007
Hi Trevor,

The Run Time Error 9 is returned when you try and set a reference to a
sheet that doesn't exist. That's fine but do it like this -

On Error Resume Next
Set ws = Nothing
Set ws = Worksheets(myShtName)
On Error goto 0
' or resume normal error handler
If ws is nothing then
'etc

Are you really wanting to potentially insert a sheet in each loop. If so, or
if 'ws' may already refer to a sheet, note the line set ws = Nothing before
testing.

Regards,
Peter T


"Trevor Williams" <(E-Mail Removed)> wrote in
message news:34F64848-66D9-4419-B87D-(E-Mail Removed)...
> Dear All
>
> I'm struggling with working out if a sheet exists in my workbook. I've

used
> code posted from this group but need to tweek it to create a sheet name

based
> on a couple of cell values. When I use the code below, Run Time Error 9

is
> returned.
>
> myShtName creates a string 'prd1 Product Name' - this is the sheet name

I'm
> looking for - if it exists then no action is needed, otherwise copy a
> template sheet at the end of the workbook, and name the sheet 'prd1

Product
> Name'.
>
> As usual, any help gratefully appreciated
>
> Trevor.
> --------------------------------------
> Dim rng As Range, cell As Range
> Dim ws As Workksheet
> Dim myShtName As String
>
> Set rng = Range("productlist")
>
> For Each cell In rng
> If cell <> "" Then
> myShtName = "prd" & cell.Offset(0, -1).Value & " " & cell.Value
> Set ws = Worksheets(myShtName)
> If ws Is Nothing Then
> Sheets("Product Template").Copy After:=Sheets(Sheets.Count)
> ActiveSheet.Name = myShtName
> Range("B3") = cell.Value
> End If
> End If
> Next cell
>



 
Reply With Quote
 
Arvi Laanemets
Guest
Posts: n/a
 
      1st Aug 2007
Hi

....
Dim ws As Object
Dim varSourceSheet As String
Dim varCont As Boolean
....
varSourceSheet = "prd1 Product Name"
For Each ws In Worksheets
varCont = IIf(varCont, True, ws.Name = varSourceSheet)
Next ws
If varCont Then
' your actions
Else
' your actions
End If
....


--
Arvi Laanemets
( My real mail address: arvi.laanemets<at>tarkon.ee )



"Trevor Williams" <(E-Mail Removed)> wrote in
message news:34F64848-66D9-4419-B87D-(E-Mail Removed)...
> Dear All
>
> I'm struggling with working out if a sheet exists in my workbook. I've
> used
> code posted from this group but need to tweek it to create a sheet name
> based
> on a couple of cell values. When I use the code below, Run Time Error 9
> is
> returned.
>
> myShtName creates a string 'prd1 Product Name' - this is the sheet name
> I'm
> looking for - if it exists then no action is needed, otherwise copy a
> template sheet at the end of the workbook, and name the sheet 'prd1
> Product
> Name'.
>
> As usual, any help gratefully appreciated
>
> Trevor.
> --------------------------------------
> Dim rng As Range, cell As Range
> Dim ws As Workksheet
> Dim myShtName As String
>
> Set rng = Range("productlist")
>
> For Each cell In rng
> If cell <> "" Then
> myShtName = "prd" & cell.Offset(0, -1).Value & " " & cell.Value
> Set ws = Worksheets(myShtName)
> If ws Is Nothing Then
> Sheets("Product Template").Copy After:=Sheets(Sheets.Count)
> ActiveSheet.Name = myShtName
> Range("B3") = cell.Value
> End If
> End If
> Next cell
>



 
Reply With Quote
 
=?Utf-8?B?VHJldm9yIFdpbGxpYW1z?=
Guest
Posts: n/a
 
      1st Aug 2007
Brilliant, thanks Peter - works a treat!

"Peter T" wrote:

> Hi Trevor,
>
> The Run Time Error 9 is returned when you try and set a reference to a
> sheet that doesn't exist. That's fine but do it like this -
>
> On Error Resume Next
> Set ws = Nothing
> Set ws = Worksheets(myShtName)
> On Error goto 0
> ' or resume normal error handler
> If ws is nothing then
> 'etc
>
> Are you really wanting to potentially insert a sheet in each loop. If so, or
> if 'ws' may already refer to a sheet, note the line set ws = Nothing before
> testing.
>
> Regards,
> Peter T
>
>
> "Trevor Williams" <(E-Mail Removed)> wrote in
> message news:34F64848-66D9-4419-B87D-(E-Mail Removed)...
> > Dear All
> >
> > I'm struggling with working out if a sheet exists in my workbook. I've

> used
> > code posted from this group but need to tweek it to create a sheet name

> based
> > on a couple of cell values. When I use the code below, Run Time Error 9

> is
> > returned.
> >
> > myShtName creates a string 'prd1 Product Name' - this is the sheet name

> I'm
> > looking for - if it exists then no action is needed, otherwise copy a
> > template sheet at the end of the workbook, and name the sheet 'prd1

> Product
> > Name'.
> >
> > As usual, any help gratefully appreciated
> >
> > Trevor.
> > --------------------------------------
> > Dim rng As Range, cell As Range
> > Dim ws As Workksheet
> > Dim myShtName As String
> >
> > Set rng = Range("productlist")
> >
> > For Each cell In rng
> > If cell <> "" Then
> > myShtName = "prd" & cell.Offset(0, -1).Value & " " & cell.Value
> > Set ws = Worksheets(myShtName)
> > If ws Is Nothing Then
> > Sheets("Product Template").Copy After:=Sheets(Sheets.Count)
> > ActiveSheet.Name = myShtName
> > Range("B3") = cell.Value
> > End If
> > End If
> > Next cell
> >

>
>
>

 
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
Rename sheet if current sheet name exists Ron5440 Microsoft Excel Programming 7 7th Jan 2010 08:55 PM
If the sheet exists...., then Darin Kramer Microsoft Excel Programming 5 13th Aug 2007 04:05 PM
RE: If sheet exists, then... Darin Kramer Microsoft Excel Programming 2 12th Sep 2006 01:22 PM
Re: If sheet exists, then... Andrew Taylor Microsoft Excel Programming 0 12th Sep 2006 11:58 AM
How can I know if a sheet exists ? =?Utf-8?B?QmVuLkM=?= Microsoft Excel Programming 3 29th Dec 2003 09:36 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:29 AM.