+ Reply to Thread
Results 1 to 7 of 7

Subscript Out of Range

  1. #1
    ExcelMonkey
    Guest

    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. #2
    Dave Peterson
    Guest

    Re: 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

  3. #3
    Tom Ogilvy
    Guest

    Re: 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" <[email protected]> wrote in message
    news:[email protected]...
    > 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. #4
    Tim Williams
    Guest

    Re: Subscript Out of Range

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

    Tim.

    "ExcelMonkey" <[email protected]> wrote in message
    news:[email protected]...
    >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




  5. #5
    ExcelMonkey
    Guest

    Re: 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" <[email protected]>

    wrote in message
    >news:[email protected]...
    >> 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

    >
    >
    >.
    >


  6. #6
    Dave Peterson
    Guest

    Re: 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" <[email protected]>

    > wrote in message
    > >news:[email protected]...
    > >> 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

  7. #7
    Claud Balls
    Guest

    Re: 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 to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1