Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 15
Default I get Subscript out of Range second time, not the first time

Hello:


I am using a marco to copy two sheet into a workbook from an existing
sheet in same workbook.

I want the user to have the ability to run the marco more than once
which updates the sheet by going to the database, there fore I don't
want the sheets copied more than once..

I check to see if sheet exist, if not I copy it. It works with the
fisrst sheet, but
when I do the test on the second sheet I get subscript out of range.
When I run the macro a second time it works. Its not consistant

What am I doing wrong the logic is the same?




SheetExists = False
On Error GoTo NoSuchSheet
If Len(Sheets("CCB_Host_Analysis").Name) 0 Then
SheetExists = True
MsgBox ("Sheet already Exist")

End If

NoSuchSheet:
If SheetExists = False Then
Sheets("Sheet1").Copy Befo=Sheets(1)
Sheets("Sheet1 (2)").Select
Sheets("Sheet1 (2)").Name = "LogicalSheet"
Range("C13").Select
MsgBox ("Adding Sheet")
End If



SheetExists = False
On Error GoTo Sheet
If Len(Sheets("3G_Triage_Analysis").Name) 0 Then
SheetExists = True
MsgBox ("Sheet2 already Exist")
End If


Sheet:
If Sheet2Exists = False Then
Sheets("Sheet1").Copy Befo=Sheets(2)
'Sheets("Sheet1 (2)").Select
Sheets("Sheet1 (2)").Name = "Testsheet"
Range("C13").Select
MsgBox ("No sheet2 exist Adding sheet")
End If

Thank You
Pam

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 15
Default I get Subscript out of Range second time, not the first time

On Feb 20, 1:04 am, "Pam" wrote:
Hello:

I am using a marco to copy two sheet into a workbook from an existing
sheet in same workbook.

I want the user to have the ability to run the marco more than once
which updates the sheet by going to the database, there fore I don't
want the sheets copied more than once..

I check to see if sheet exist, if not I copy it. It works with the
fisrst sheet, but
when I do the test on the second sheet I get subscript out of range.
When I run the macro a second time it works. Its not consistant

What am I doing wrong the logic is the same?

SheetExists = False
On Error GoTo NoSuchSheet
If Len(Sheets("CCB_Host_Analysis").Name) 0 Then
SheetExists = True
MsgBox ("Sheet already Exist")

End If

NoSuchSheet:
If SheetExists = False Then
Sheets("Sheet1").Copy Befo=Sheets(1)
Sheets("Sheet1 (2)").Select
Sheets("Sheet1 (2)").Name = "LogicalSheet"
Range("C13").Select
MsgBox ("Adding Sheet")
End If

SheetExists = False
On Error GoTo Sheet
If Len(Sheets("3G_Triage_Analysis").Name) 0 Then
SheetExists = True
MsgBox ("Sheet2 already Exist")
End If

Sheet:
If Sheet2Exists = False Then
Sheets("Sheet1").Copy Befo=Sheets(2)
'Sheets("Sheet1 (2)").Select
Sheets("Sheet1 (2)").Name = "Testsheet"
Range("C13").Select
MsgBox ("No sheet2 exist Adding sheet")
End If

Thank You
Pam


Correction, the first logic works for the first sheet but dosen't work
for the second sheet,
I get subscript out of range. Then when I run the macro a second time
it works. I can't delivery macro this way.


HELP PLEASE!!!!!

Thank You
Pam

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default I get Subscript out of Range second time, not the first time

Your problem is probably that your code address whatever workbook is active.
You need to qualify your code so it checks sheets in a particular workbook if
you want consistent behavior. If no other way, you could loop through the
workbooks collection and identify the workbook by some unique feature
perhaps.

--
Regards,
Tom Ogilvy

"Pam" wrote:

On Feb 20, 1:04 am, "Pam" wrote:
Hello:

I am using a marco to copy two sheet into a workbook from an existing
sheet in same workbook.

I want the user to have the ability to run the marco more than once
which updates the sheet by going to the database, there fore I don't
want the sheets copied more than once..

I check to see if sheet exist, if not I copy it. It works with the
fisrst sheet, but
when I do the test on the second sheet I get subscript out of range.
When I run the macro a second time it works. Its not consistant

What am I doing wrong the logic is the same?

SheetExists = False
On Error GoTo NoSuchSheet
If Len(Sheets("CCB_Host_Analysis").Name) 0 Then
SheetExists = True
MsgBox ("Sheet already Exist")

End If

NoSuchSheet:
If SheetExists = False Then
Sheets("Sheet1").Copy Befo=Sheets(1)
Sheets("Sheet1 (2)").Select
Sheets("Sheet1 (2)").Name = "LogicalSheet"
Range("C13").Select
MsgBox ("Adding Sheet")
End If

SheetExists = False
On Error GoTo Sheet
If Len(Sheets("3G_Triage_Analysis").Name) 0 Then
SheetExists = True
MsgBox ("Sheet2 already Exist")
End If

Sheet:
If Sheet2Exists = False Then
Sheets("Sheet1").Copy Befo=Sheets(2)
'Sheets("Sheet1 (2)").Select
Sheets("Sheet1 (2)").Name = "Testsheet"
Range("C13").Select
MsgBox ("No sheet2 exist Adding sheet")
End If

Thank You
Pam


Correction, the first logic works for the first sheet but dosen't work
for the second sheet,
I get subscript out of range. Then when I run the macro a second time
it works. I can't delivery macro this way.


HELP PLEASE!!!!!

Thank You
Pam


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 15
Default I get Subscript out of Range second time, not the first time

On Feb 20, 7:32 am, Tom Ogilvy
wrote:
Your problem is probably that your code address whatever workbook is active.
You need to qualify your code so it checks sheets in a particular workbook if
you want consistent behavior. If no other way, you could loop through the
workbooks collection and identify the workbook by some unique feature
perhaps.

--
Regards,
Tom Ogilvy



"Pam" wrote:
On Feb 20, 1:04 am, "Pam" wrote:
Hello:


I am using a marco to copy two sheet into a workbook from an existing
sheet in same workbook.


I want the user to have the ability to run the marco more than once
which updates the sheet by going to the database, there fore I don't
want the sheets copied more than once..


I check to see if sheet exist, if not I copy it. It works with the
fisrst sheet, but
when I do the test on the second sheet I get subscript out of range.
When I run the macro a second time it works. Its not consistant


What am I doing wrong the logic is the same?


SheetExists = False
On Error GoTo NoSuchSheet
If Len(Sheets("CCB_Host_Analysis").Name) 0 Then
SheetExists = True
MsgBox ("Sheet already Exist")


End If


NoSuchSheet:
If SheetExists = False Then
Sheets("Sheet1").Copy Befo=Sheets(1)
Sheets("Sheet1 (2)").Select
Sheets("Sheet1 (2)").Name = "LogicalSheet"
Range("C13").Select
MsgBox ("Adding Sheet")
End If


SheetExists = False
On Error GoTo Sheet
If Len(Sheets("3G_Triage_Analysis").Name) 0 Then
SheetExists = True
MsgBox ("Sheet2 already Exist")
End If


Sheet:
If Sheet2Exists = False Then
Sheets("Sheet1").Copy Befo=Sheets(2)
'Sheets("Sheet1 (2)").Select
Sheets("Sheet1 (2)").Name = "Testsheet"
Range("C13").Select
MsgBox ("No sheet2 exist Adding sheet")
End If


Thank You
Pam


Correction, the first logic works for the first sheet but dosen't work
for the second sheet,
I get subscript out of range. Then when I run the macro a second time
it works. I can't delivery macro this way.


HELP PLEASE!!!!!


Thank You
Pam- Hide quoted text -


- Show quoted text -


Hi:

What I did and it worked, I just split the logic up into to different
fucntions
and it's working fine now. I check if the first sheet exist in the
original funcition
and I moved the checking for second sheet into a different function.

Maybe it didn't like the On Error Go to in the logic twice.



Thank You for your suggestion.
Pam.

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default I get Subscript out of Range second time, not the first time

Upon closer examination of your code, Indirectly that is correct. It
doesn't like it because you never end the error handling and when you get an
error in the error handler, it quits. Separating into separate functions
does cause the error handling to end when the function is exited. but don't
think you couldn't have kept them together with better organization and
correct error handling.

--
Regards,
Tom Ogilvy



"Pam" wrote in message
oups.com...
On Feb 20, 7:32 am, Tom Ogilvy
wrote:
Your problem is probably that your code address whatever workbook is
active.
You need to qualify your code so it checks sheets in a particular
workbook if
you want consistent behavior. If no other way, you could loop through
the
workbooks collection and identify the workbook by some unique feature
perhaps.

--
Regards,
Tom Ogilvy



"Pam" wrote:
On Feb 20, 1:04 am, "Pam" wrote:
Hello:


I am using a marco to copy two sheet into a workbook from an existing
sheet in same workbook.


I want the user to have the ability to run the marco more than once
which updates the sheet by going to the database, there fore I don't
want the sheets copied more than once..


I check to see if sheet exist, if not I copy it. It works with the
fisrst sheet, but
when I do the test on the second sheet I get subscript out of range.
When I run the macro a second time it works. Its not consistant


What am I doing wrong the logic is the same?


SheetExists = False
On Error GoTo NoSuchSheet
If Len(Sheets("CCB_Host_Analysis").Name) 0 Then
SheetExists = True
MsgBox ("Sheet already Exist")


End If


NoSuchSheet:
If SheetExists = False Then
Sheets("Sheet1").Copy Befo=Sheets(1)
Sheets("Sheet1 (2)").Select
Sheets("Sheet1 (2)").Name = "LogicalSheet"
Range("C13").Select
MsgBox ("Adding Sheet")
End If


SheetExists = False
On Error GoTo Sheet
If Len(Sheets("3G_Triage_Analysis").Name) 0 Then
SheetExists = True
MsgBox ("Sheet2 already Exist")
End If


Sheet:
If Sheet2Exists = False Then
Sheets("Sheet1").Copy Befo=Sheets(2)
'Sheets("Sheet1 (2)").Select
Sheets("Sheet1 (2)").Name = "Testsheet"
Range("C13").Select
MsgBox ("No sheet2 exist Adding sheet")
End If


Thank You
Pam


Correction, the first logic works for the first sheet but dosen't work
for the second sheet,
I get subscript out of range. Then when I run the macro a second time
it works. I can't delivery macro this way.


HELP PLEASE!!!!!


Thank You
Pam- Hide quoted text -


- Show quoted text -


Hi:

What I did and it worked, I just split the logic up into to different
fucntions
and it's working fine now. I check if the first sheet exist in the
original funcition
and I moved the checking for second sheet into a different function.

Maybe it didn't like the On Error Go to in the logic twice.



Thank You for your suggestion.
Pam.



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
run-time error '9': subscript out of range [email protected] uk Excel Discussion (Misc queries) 4 December 8th 09 10:27 PM
Run-time Error 9: Subscript out of range Patrick[_15_] Excel Programming 4 August 23rd 06 02:57 PM
run-time error '9': Subscript out of range - WHY?? pastotnikr Excel Programming 3 April 18th 06 06:51 PM
Run-time error 9 (Subscript out of range) Lizz45ie[_3_] Excel Programming 3 October 26th 05 11:38 PM
run-time error '9': Subscript out of range AccessHelp Excel Programming 1 September 30th 05 05:10 PM


All times are GMT +1. The time now is 09:07 AM.

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"