Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 172
Default Subscript Out of Range

I am getting an Subscript Out of Range Error on this line
of code:

Set sh1 = ActiveWorkbook.Sheets("Audit Results")

Why is this happening?

Thanks
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Subscript Out of Range

You don't have a worksheet in the activeworkbook named Audit Results. If
you did, you wouldn't get the error. If you didn't think you did you
wouldn't have written the code - so you need to look closer. Is there an
extra space somewhere - on either end or perhaps an extra spaced separating
the two words.

--
Regards,
Tom Ogilvy

"ExcelMonkey" wrote in message
...
I am getting an Subscript Out of Range Error on this line
of code:

Set sh1 = ActiveWorkbook.Sheets("Audit Results")

Why is this happening?

Thanks



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 172
Default Subscript Out of Range

Here is the problem. I am in an xla file. The routine
inserts a sheet called "Audit Results". If its already
there from a prevous run it deletes it. I was using
dilogue sheets for temporay userforms to summarize sheets
in the model. Just changed this to a list box
userform. I was replacing "Thisworkbook"
with "ActiveWorkbook". Now I am confused. This was
working fine. It only seems to work when sheet already
exists called "Audit Results". Previously it worked even
if one did not exist, because it would creat it.

Here is what I have for code:


'Set up name of new summary sheet
Set sh1 = ActiveWorkbook.Sheets("Audit Results")
On Error GoTo 0

'If Sheet called "Audit Results" already exists
'then delete it and prepare to create a new one

If Not sh1 Is Nothing Then
Application.DisplayAlerts = False
sh1.Delete
Application.DisplayAlerts = True
End If

With ActiveWorkbook

'Add a worksheet for results to be pasted to
.Worksheets.Add(After:=.Worksheets
(.Worksheets.Count)).Name = "Audit Results"

End With

-----Original Message-----
You don't have a worksheet in the activeworkbook named

Audit Results. If
you did, you wouldn't get the error. If you didn't

think you did you
wouldn't have written the code - so you need to look

closer. Is there an
extra space somewhere - on either end or perhaps an

extra spaced separating
the two words.

--
Regards,
Tom Ogilvy

"ExcelMonkey"

wrote in message
...
I am getting an Subscript Out of Range Error on this

line
of code:

Set sh1 = ActiveWorkbook.Sheets("Audit Results")

Why is this happening?

Thanks



.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,758
Default Subscript Out of Range

When you're testing whether a worksheet (or any object) exists, you'll want to
avoid the error you get when it doesn't.

'Set up name of new summary sheet
Set sh1 = ActiveWorkbook.Sheets("Audit Results")
On Error GoTo 0

Should have an "on error resume next" line near it:

'Set up name of new summary sheet
on error resume next
Set sh1 = ActiveWorkbook.Sheets("Audit Results")
On Error GoTo 0

'then this line works nicely
if not sh1 is nothing then
.....

ExcelMonkey wrote:

Here is the problem. I am in an xla file. The routine
inserts a sheet called "Audit Results". If its already
there from a prevous run it deletes it. I was using
dilogue sheets for temporay userforms to summarize sheets
in the model. Just changed this to a list box
userform. I was replacing "Thisworkbook"
with "ActiveWorkbook". Now I am confused. This was
working fine. It only seems to work when sheet already
exists called "Audit Results". Previously it worked even
if one did not exist, because it would creat it.

Here is what I have for code:

'Set up name of new summary sheet
Set sh1 = ActiveWorkbook.Sheets("Audit Results")
On Error GoTo 0

'If Sheet called "Audit Results" already exists
'then delete it and prepare to create a new one

If Not sh1 Is Nothing Then
Application.DisplayAlerts = False
sh1.Delete
Application.DisplayAlerts = True
End If

With ActiveWorkbook

'Add a worksheet for results to be pasted to
.Worksheets.Add(After:=.Worksheets
(.Worksheets.Count)).Name = "Audit Results"

End With

-----Original Message-----
You don't have a worksheet in the activeworkbook named

Audit Results. If
you did, you wouldn't get the error. If you didn't

think you did you
wouldn't have written the code - so you need to look

closer. Is there an
extra space somewhere - on either end or perhaps an

extra spaced separating
the two words.

--
Regards,
Tom Ogilvy

"ExcelMonkey"

wrote in message
...
I am getting an Subscript Out of Range Error on this

line
of code:

Set sh1 = ActiveWorkbook.Sheets("Audit Results")

Why is this happening?

Thanks



.


--

Dave Peterson
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,758
Default Subscript Out of Range

I'd guess that that workbook that's active doesn't contain a worksheet named
"Audit results".

Common problems are typos and extra spaces (embedded or leading/trailing).



ExcelMonkey wrote:

I am getting an Subscript Out of Range Error on this line
of code:

Set sh1 = ActiveWorkbook.Sheets("Audit Results")

Why is this happening?

Thanks


--

Dave Peterson


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,588
Default Subscript Out of Range

Maybe there is no sheet with that name? That's the usual cause of
this error

Tim.

"ExcelMonkey" wrote in message
...
I am getting an Subscript Out of Range Error on this line
of code:

Set sh1 = ActiveWorkbook.Sheets("Audit Results")

Why is this happening?

Thanks



  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 72
Default Subscript Out of Range

The only way I could replicate your error is by removing the Audit
Results worksheet from my workbook. Are you sure it's spelled
correctly?



*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
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
Subscript out of Range Q John Excel Programming 5 December 14th 04 07:54 AM
Subscript Out of Range Al Excel Programming 5 September 22nd 04 07:07 PM
Subscript out of range Bill Murphy Excel Programming 1 August 5th 04 08:52 AM
Subscript out of range Stacy Haskins[_2_] Excel Programming 4 April 10th 04 05:41 AM
Subscript Out of Range John Wilson Excel Programming 2 September 7th 03 04:07 AM


All times are GMT +1. The time now is 02:11 AM.

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"