+ Reply to Thread
Results 1 to 13 of 13

Run-time error 9 Subscript out of range

  1. #1
    Registered User
    Join Date
    03-30-2014
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    21

    Run-time error 9 Subscript out of range

    Hi Folks,

    The code placed below is throwing an error ( "Run-time error 9 Subscript out of range") on the following lines in the code placed at the end of this post:

    Set WrkBk0 = Workbooks(Wk0)
    Set WrkBk1 = Workbooks(Wk1)

    Tried but failed to figure out the reason.

    Can somebody help please?

    Thanks in advance

    Regards,

    Amber
    -------------------------------------------------------------------------------------------------------------------------------------------
    Sub Topfive()

    Dim WrkBk0 As Workbook
    Dim WrkBk1 As Workbook
    Dim WrkSt1 As Worksheet
    Dim WrkSt2 As Worksheet
    Dim WrkSt3 As Worksheet
    Dim Wk0, Wk1 As String
    Dim M1 As String
    Dim M0 As String
    Dim Y0, Y1 As Integer
    Dim D As Variant
    M0 = Format(DateAdd("M", -2, Date), "mmm")
    M1 = Format(DateAdd("M", -1, Date), "mmm")
    Y0 = Format(DateAdd("m", -1, Date), "yy")
    Y1 = Format(DateAdd("m", -2, Date), "yy")
    Wk0 = "Performance Master_" & M0 & " " & Y0 & ".xlsm"
    Wk1 = "Performance Master_" & M1 & " " & Y1 & ".xlsm"
    Set WrkBk0 = Workbooks(Wk0)
    Set WrkBk1 = Workbooks(Wk1)
    Set WrkSt1 = Sheets("Performance Summay")
    Set WrkSt2 = Sheets("Sheet1")
    Set WrkSt3 = Sheets("Performance Summary")

    End Sub
    ------------------------------------------------------------------------------------------------------------------------------------------

  2. #2
    Registered User
    Join Date
    09-24-2013
    Location
    Twin Cities, MN
    MS-Off Ver
    Excel 2010
    Posts
    91

    Re: Run-time error 9 Subscript out of range

    If you copied that directly from your VBA, the 4th line from the bottom has "Summary" spelled incorrectly. Could that be it?

  3. #3
    Valued Forum Contributor
    Join Date
    07-04-2012
    Location
    Cape Town, RSA
    MS-Off Ver
    Office 365 ProPlus
    Posts
    1,050

    Re: Run-time error 9 Subscript out of range

    A few other things to check/adjust:

    Dim Y0, Y1 As Integer
    The line above is probably not the cause of your error, but realize that your Y0 variable is a variant and your Y1 variable is Integer. VBA does not assume Y0 to be Integer just because it is on the same line. Each variable myust be explicitly declared.
    Dim Y0 as Integer, Y1 As Integer

    Second: Just an assumption?!
    M0 = Format(DateAdd("M", -2, Date), "mmm")
    M1 = Format(DateAdd("M", -1, Date), "mmm")
    Y0 = Format(DateAdd("m", -1, Date), "yy") <<< Should this not be -2
    Y1 = Format(DateAdd("m", -2, Date), "yy") <<< And this be -1
    Regards,
    Rudi

  4. #4
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Run-time error 9 Subscript out of range

    Your workbook needs to be "Open" to set the object reference. e.g.
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    03-30-2014
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    21

    Re: Run-time error 9 Subscript out of range

    Thanks for the responses.

    I am facing problem while setting the value for the variables WrkBk0 and WrkBk1.

    everything else is working fine.

    the error pops up on the the following line of the code:
    Set WrkBk0 = Workbooks(Wk0)

    Looking forward for a solution...do let me know in case any futher clarity is required.

    ----------------------------------------------------------------------------------------------------------------------------------------------------------

    Hi minnesotaart...the spelling doesnt seem to be a problem as the sheet name is exactly the same as referred in the code.

    Hi Rudis...Thanks for pointing these out...hv corrected the same in my code, however as u rightly thought, the same is not the reason behind the error.

    Hi Kenneth...the error was thrown up despite all the files referred to in the code were open while executing the code.

  6. #6
    Valued Forum Contributor
    Join Date
    07-04-2012
    Location
    Cape Town, RSA
    MS-Off Ver
    Office 365 ProPlus
    Posts
    1,050

    Re: Run-time error 9 Subscript out of range

    Here are my findings:

    Change this line: Dim Wk0, Wk1 As String to be: Dim Wk0 as String, Wk1 As String
    Change this line: Dim Y0, Y1 As Integer to be: Dim Y0 as Integer, Y1 As Integer - (Although both can be string also)

    Ensure that the two files you point to are both open, and their names are spelled exactly like these:
    - Performance Master_Feb 14.xlsm
    - Performance Master_Mar 14.xlsm
    (Check spaces, spelling and if both files are *xlsm)

    Ensure that the spelling of "Summary" in this line is valid...else correct it.
    Set WrkSt1 = Sheets("Performance Summay")

    It is advisable to modify your Sheet declarations from this:
    Set WrkSt1 = Sheets("Performance Summary")
    Set WrkSt2 = Sheets("Sheet1")
    Set WrkSt3 = Sheets("Performance Summary")

    To this:
    Set WrkSt1 = WrkBk0.Sheets("Performance Summary")
    Set WrkSt2 = WrkBk0.Sheets("Sheet1")
    Set WrkSt3 = WrkBk1.Sheets("Performance Summary")

    Without the Workbook prefixed, (and if you fixed the spelling) your sheet variables will will point to the same sheet (or become ambiguous).
    Obviously, make sure you assign the correct workbook to the correct worksheet

    This the changes and corrections listed above I have successfully run the code...

  7. #7
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Run-time error 9 Subscript out of range

    I suspect that your workbook names are not resolving to the workbook names that are open workbooks. Use debug.print to see the string result in VBE's Immediate window or use F8 to step through code one line at a time. Depending on your version, you may not need the file extension but some do.
    Please Login or Register  to view this content.
    Maybe you should check if it is open before assigning it.
    Please Login or Register  to view this content.
    Last edited by Kenneth Hobson; 04-04-2014 at 09:17 AM.

  8. #8
    Registered User
    Join Date
    03-30-2014
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    21

    Re: Run-time error 9 Subscript out of range

    Hi Rudis,

    Hv made the suggested changes still there is a problem with the code.

    I am attaching the workbook with the code for your reference...plz advice.

    Regards,

    Amber
    Attached Files Attached Files

  9. #9
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Run-time error 9 Subscript out of range

    You have several issues that were not fixed.
    e.g.
    Please Login or Register  to view this content.
    You are setting the left side as a worksheet? That is not how you dimmed that variable.

    You did not use the function that I suggested. We do not have the workbooks to open to test.

  10. #10
    Registered User
    Join Date
    03-30-2014
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    21

    Re: Run-time error 9 Subscript out of range

    Thanks Kenneth...got ur point...will keep that in mind.

  11. #11
    Registered User
    Join Date
    03-30-2014
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    21

    Re: Run-time error 9 Subscript out of range

    Hi Kenneth,

    Can you help me with identifying the issue in code named "top five".

    Is there anything wrong with the way I am assingning value to the variable "WrkSt1"

    Thanks in advance.

    Amber

  12. #12
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Run-time error 9 Subscript out of range

    I suggest using a problem solving method that I used for years. Break the problem down to the most simple level possible. It not only helps you but would help others help you more readily.

    Syntax errors aside, it really boils down to, did you define the workbook name that is open properly, or not. IsWorkBookOpen() will give you a quick way to find out. It is a good thing to check before setting the workbook object as a standard way to catch errors during run-time.

  13. #13
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Run-time error 9 Subscript out of range

    Reusing workbooks or even using ThisWorkbook and setting it to another workbook object can be fussy sometimes.

    Here is how I tested your code and did a bit of tidying up.
    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Run-time error 9 Subscript out of range
    By katieshields in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-02-2014, 02:24 AM
  2. [SOLVED] Run-time error '9': subscript out of range - error occurs on multiple computers except one
    By BrettE in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 02-06-2014, 11:19 PM
  3. [SOLVED] Run-time Error '9': Subscript out of Range
    By Veena Anoohya in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 01-19-2014, 05:06 AM
  4. Run-time error '9' - Subscript out of range
    By mgphill in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-25-2012, 10:08 AM
  5. VB Run-time error '9'; Subscript out of range
    By lisabethvw in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 12-29-2009, 03:49 AM

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