+ Reply to Thread
Results 1 to 8 of 8

Runtime error 9 "subscript out of range"

  1. #1
    Registered User
    Join Date
    04-07-2013
    Location
    Kuala Lumpur
    MS-Off Ver
    Excel 2007
    Posts
    3

    Runtime error 9 "subscript out of range"

    Hi Guys,

    I'm kindly new to excel vba, and just learn how to use it this weekend because I need to use it to one of my client.

    in my program, I need to export the data from third party software (scada software) to an excel.

    it was working for a few line, but then an runtime error 9 "subscript out of range" occur. And from my reading, this error caused by "some object you tried to access by name cannot be found".

    when i debug, the error was in this line:

    NextRow = Cells(Workbooks("Test.xlsx").Worksheets("Sheet1").Rows.Count, 1).End(xlUp).Row + 1

    and this is my full script:

    Please Login or Register  to view this content.
    Could someone please guide me?
    Last edited by arlu1201; 04-08-2013 at 01:47 AM. Reason: Use code tags in future.

  2. #2
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Runtime error 9 "subscript out of range"

    A subscript out of range error occurs when the sheet name or workbook name does not match with the sheet name or workbook name in your code.

    Check if you have a workbook named Test.xlsx with a worksheet named Sheet1. If your file is named differently or your sheet is named differently, change this line in the code to reflect the same.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  3. #3
    Forum Contributor
    Join Date
    01-02-2007
    Location
    Australia NSW
    MS-Off Ver
    2013
    Posts
    494

    Re: Runtime error 9 "subscript out of range"

    If you have Macros in your workbook, then chances are the file is Test.xlsm not Test.xlsx

  4. #4
    Registered User
    Join Date
    04-07-2013
    Location
    Kuala Lumpur
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: Runtime error 9 "subscript out of range"

    Hi Guys,

    Thank you for replied, I named it correctly for the workbook and worksheet and there is no macro in my workbook.

  5. #5
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Runtime error 9 "subscript out of range"

    And you are still getting the error?

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Runtime error 9 "subscript out of range"

    For some reason you are creating a new instance of Excel.

    It's this instance you are opening the workbook 'Test.xlsx' in.

    In the line of code you get the error on you don't reference this instance when trying to refer to the workbook.

    This means that VBA will be looking for the workbook in the instance of Excel the code is in.

    Either add a reference to the instance you've created or, probably a better idea, don't create a new instance in the first place.
    If posting code please use code tags, see here.

  7. #7
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643
    Just realised the code might not be in Excel VBA.

    If that's the case try this.
    Please Login or Register  to view this content.
    PS I think what I said in the previous post still stands.

  8. #8
    Registered User
    Join Date
    04-07-2013
    Location
    Kuala Lumpur
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: Runtime error 9 "subscript out of range"

    Hello Guys,

    Thanks to Norie, I solved the problem.
    Norie, I'm quite new to vba so could you please explain to me why your code work and mine is not?
    because i guess, it quite the same.

    Akmal

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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