Consulting

Results 1 to 15 of 15

Thread: VBA Arrays with References to Multiple Sheets

  1. #1
    VBAX Regular
    Joined
    Dec 2023
    Posts
    12
    Location

    VBA Arrays with References to Multiple Sheets

    I would like to collect data from several cells on multiple sheets into an array but this is presenting a problem. I can fill an array with data from cells on the active sheet using the code below:

    Sub store_data()
    Dim d(4) As String
    d(0) = Range("E7").Value
        d(1) = Range("E8").Value
        d(3) = Range("E9").Value
        d(4) = Range("E10").Value
    End Sub
    However, when I attempt to collect data from the current sheet and then another sheet, the d(0) and d(1) expressions run but I get an error at the d(2) expression (Runtime error '9': subscript out of range).

    Sub store_data()
    Dim d(4) As String
    d(0) = Range("E7").Value
        d(1) = Range("E8").Value
        d(2) = Sheets("sheet6").Range("E9").Value
        d(3) = Sheets("sheet6").Range("E10").Value
    End Sub
    Any help would be appreciated.
    Last edited by Aussiebear; 12-22-2023 at 02:48 PM. Reason: Added code tags to supplied code

  2. #2
    VBAX Regular
    Joined
    Dec 2023
    Posts
    12
    Location
    Solved my own problem...

    Sub store_data()
    Dim d(4) As String
    d(0) = Range("E7").Value
        d(1) = Range("E8").Value
        d(2) = Sheet6.Range("E9").Value
        d(3) = Sheet6.Range("E10").Value
    End Sub
    Last edited by Aussiebear; 12-22-2023 at 02:49 PM. Reason: Added code tags to supplied code

  3. #3
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,064
    Location
    Been looking at this thread for a couple of days now and wondering how the logic of it works. To begin with the OP only dims 1 variable, namely "d(4)" as a String, yet uses four other variables as the solution. Also the reference, Sheets ("Sheet 6").Range is said to not work, but Sheet6.Range does. Why is this so?

    As a further input. If I saw something like d(4), I'd be expecting an Array with 4 components , namely d(0), d(1), d(2), & d(3), however the OP dimmed it as a String rather than an Array, so how did the array get formed as suggested per the first post.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  4. #4
    Hello
    You could have a look at Paul Site
    See you

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,729
    Location
    Quote Originally Posted by Aussiebear View Post
    Been looking at this thread for a couple of days now and wondering how the logic of it works. To begin with the OP only dims 1 variable, namely "d(4)" as a String, yet uses four other variables as the solution. Also the reference, Sheets ("Sheet 6").Range is said to not work, but Sheet6.Range does. Why is this so?

    As a further input. If I saw something like d(4), I'd be expecting an Array with 4 components , namely d(0), d(1), d(2), & d(3), however the OP dimmed it as a String rather than an Array, so how did the array get formed as suggested per the first post.

    Dim d(4) As String
    dim's a 5 element string array with indices of 0, 1, 2, 3 and 4

    (BTW I like to Dim d(1 to 4) as String)

    My guess is that Sheet6 is the Code Name, but the WS Name is something else, not = "Sheet6"
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  6. #6
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,064
    Location
    So effectively "Dim d(4) as a String" is shorthand coding.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  7. #7
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,729
    Location
    Quote Originally Posted by Aussiebear View Post
    So effectively "Dim d(4) as a String" is shorthand coding.
    I wouldn't call it shorthand, I've always seem it that way and I don't know any other way to define an array of Strings, without the 'As String" they'd all be Variants

    Capture.JPG


    https://learn.microsoft.com/en-us/of...6)%26rd%3Dtrue

    The examples are good
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  8. #8
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,064
    Location
    Thank you Paul. There's a hundred ways to skin a cat when using coding. Some don't seem natural to me.

    For example "dim d(4) as String". If you hadn't read the preamble about wanting to create an array of data, then the line "dim d(4) as string", would lead to anyone to believe that d(4) was a string. Yet we know from the preamble that d(4) is meant as an array by the OP. Subsequent use of the indices d(0), d(1), d(2), & d(3) are to me deceptive. As in having a very similar format as the name of the array.

    I looked at your example in the post above, and it shows exactly why I'm a little confused here. D(4) equals a value "E" therefore could not be regarded as an Array.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  9. #9
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,729
    Location
    A variable name cannot have a paren in it

    A variable D4 is not the same as an array element D(4)
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  10. #10
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,064
    Location
    I must be confusing you as well as myself..


    In this image this line, "Dim D(4) as String" is predetermined to be an Array of Strings, yet later on, 7 lines later, D(4) is used as a name of a indice. I'm inclined to believe that the original line should have read Dim d(0 to 4) As String
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  11. #11
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    Should is pretty subjective though.

    Although I also prefer to specify all fixed bounds in my real code - and sometimes in posted code, since I've lost track of the number of times I've seen people try and declare say a 5 element array using d(5), for example - you don't have to. The presence of the Dim statement makes the meaning clear. At least it wasn't written as:

    Dim D$(4)
    Be as you wish to seem

  12. #12
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,729
    Location
    Quote Originally Posted by Aussiebear View Post
    I must be confusing you as well as myself..
    In this image this line, "Dim D(4) as String" is predetermined to be an Array of Strings, yet later on, 7 lines later, D(4) is used as a name of a indice. I'm inclined to believe that the original line should have read Dim d(0 to 4) As String
    Dim D(4) As String defines a 5 element array named D

    To put data in or get it out of a specific "slot" you need something like D(4) where 4 is the index into D's 4th "slot" (actually 5th or UBound(D)
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  13. #13
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,064
    Location
    Got it. Thank you.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  14. #14
    VBAX Regular
    Joined
    Dec 2023
    Posts
    12
    Location
    Here is my best explanation, if I understand your question correctly. d(4) is an array with 4 addresses (actually its 5 addresses 0,1,2,3,4). Each array address is used to store a text string. The text strings stored in each array address come from specific cells in the workbook located on different worksheets. If you use a button to initiate the macro on a specific sheet, lets say sheet 1, then you can reference a cell on the same sheet as the button (the active sheet) like this:
    d(0) = Range("E7").Value
    d(1) = Range("E8").Value


    However if you want to populate the array using cells on worksheets other than the active worksheet (sheet 1 where the macro was called) you must do this:

    d(0) = Range("E7").Value
    d(1) = Sheet6.Range("E8").Value

    or this:

    d(0) = Sheet1.Range("E7").Value
    d(1) = Sheet6.Range("E8").Value

  15. #15
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,064
    Location
    Thank you.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

Posting Permissions

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