Consulting

Results 1 to 12 of 12

Thread: Subscript out of range Multidimensional Array

  1. #1

    Subscript out of range Multidimensional Array

    Hello guys,

    I have a problem and I cannot solve it's super puzzling.

    I have a macro that's been working fine and today it started giving me a Subscript out of Range error right before looping through an array.

    My array is global and declared:

        Public Arr(1 To 1000, 0 To 8) As Variant    Dim sh(1 To 1000) As Variant
        
        i = 1
        x = 1
        Do Until Arr(i, 0) = Empty ' I am getting the error here
            sh(x) = Arr(i, 0)
            x = x + 1
            i = i + 1
        Loop
    Funny thing is that the array does hold a value at Arr(1,0). If I change it to Arr(1,1) move one line down and then change it back to what it was before the code runs with no problems.

    I have tried restarting Excel, deleting the temp files and the Appdata.

    No luck any suggestions?

    Thanks a lot for the help guys.
    Feedback is the best way for me to learn


    Follow the Armies

  2. #2
    Never mind guys. The same way it appeared its gone now. So strange.

    Still curious to know what this was about. Also in case it comes back again.

    Thanks
    Feedback is the best way for me to learn


    Follow the Armies

  3. #3
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    If no member of Arr(1 to 1000,0) is empty then i will reach 1001 (and so will x), which will give the error.
    What is the value of i when it errors? (hover over the variable in the code or look atthe Locals pane in the vbe)?
    Perehaps something along these lines to ensure the loop doesn't go around too many times:
    For i = LBound(Arr) To UBound(Arr)
    If IsEmpty(Arr(i, 0)) Then Exit For
        sh(i) = Arr(i, 0)
    Next i
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  4. #4
    HI,

    Thanks for the reply,

    The Value of i=1 and the value of arr(i,0)= "Valid text"

    it never reaches the boundaries of the array i will go up to 20 maybe.

    Thanks
    Feedback is the best way for me to learn


    Follow the Armies

  5. #5
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    Hmmm.. difficult one.
    When the error occurs, have a look at the specific member of Arr in the locals pane. Does that member exist? Does Arr have the dimensions you expect?
    I ask this because I expect that you've simpified the code/left bits out etc. for posting here. Are you working with multiple workbooks? What kind of module is the code in? Is the Public statement in the same module? If not, where is it?
    Could you duplicate the error in a workbook (difficult I know) and post it here?
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  6. #6
    Quote Originally Posted by p45cal View Post
    Hmmm.. difficult one.
    I know.

    I double check the array in the watch window and it looks the way its supposed to look.

    The public array is dimensioned in a Home Module and the loop is in a different module.

    There is only one workbook open or involved at the moment of the error.

    No I cannot replicate the error in a different workbook. Actually I cannot replicate the error again at all. So strange.

    Thanks for the replies p45cal

    Feedback is the best way for me to learn


    Follow the Armies

  7. #7
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    What is meant here by the Home module?
    And the loop code is in a standard module?.. a sheet code module?

    and re:"There is only one workbook open or involved at the moment of the error."
    Does this mean there are other workbooks opened/closed by the code?
    Are those workbooks properly closed (not just not visible).
    Do you see more workbooks in the project explorer part of the vbe than there should be?
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  8. #8
    Quote Originally Posted by fredlo2010 View Post
    The public array is dimensioned in a Home Module and the loop is in a different module
    lol way to go... my bad the home module is a regular module. All is happening is between regular modules. The opening and closing o other workbooks will not happen until later on in the code.
    There is no hidden workbooks.
    Besides the workbook in question, there is my personal workbook and come addin workbooks(unrelated to the code)

    Thanks sorry for the Home Module lol
    Feedback is the best way for me to learn


    Follow the Armies

  9. #9
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    VBA has a tendency to do strange things like this. I tend to declare a constant at the start of each procedure with the procedure name, and rarely, but often enough to bug me, it throws a compilation error.

    Get a copy of Rob Bovey's CodeCleaner addin (it's free), and run it regularly on important projects.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  10. #10
    Thanks for the replies guys.

    I have tried the Code Cleaner before and I am not sure what it actually does. I don't see anything being changed.

    Thanks a gain.
    Feedback is the best way for me to learn


    Follow the Armies

  11. #11
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    It doesn't change anything. All it does is to export all code modules, then re-import them. VBA has a tendency to bloat as you change it, and this often causes problems, exporting and re-importing the code modules removes the bloat. You could do it manually, Rob's addin just automates it for you, plus adding a couple of other features.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  12. #12
    I see.

    Good to know I will make sure I use it frequently then.

    Thanks a lot for the info xld
    Feedback is the best way for me to learn


    Follow the Armies

Posting Permissions

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