Consulting

Results 1 to 8 of 8

Thread: Solved: Bulk assignment to VBA arrays

  1. #1

    Solved: Bulk assignment to VBA arrays

    Having some trouble bulk assigning values to an array. I know that you can assign values to an array individually, but what I would really like is to assign all values in one statement like in other programming languages.

    Here is my code. Any assistance would be greatly appreciated.

    [vba]
    Dim aArray(3) As Integer
    Dim index As Integer
    aArray = {1, 2, 3}

    For index = LBound(aArray) To UBound(aArray)
    MsgBox aArray(index)
    Next
    [/vba]

    btw. this is my first post and i'm not a 100 percent sure if i'm in the right section. sorry if i'm not....

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    hi.
    try:

    [vba]
    Dim aArray
    Dim index As Integer

    aArray = Array(1, 2, 3)

    For index = LBound(aArray) To UBound(aArray)
    MsgBox aArray(index)
    Next
    [/vba]


    or
    [vba]
    Dim aArray(2) As Integer
    Dim index As Integer

    aArray(0) = 1
    aArray(1) = 2
    aArray(2) = 3

    For index = LBound(aArray) To UBound(aArray)
    MsgBox aArray(index)
    Next
    [/vba]


    or
    [vba]
    Dim aArray(2) As Integer
    Dim index As Integer

    For index = 0 To 2
    aArray(index) = index + 1
    Next

    For index = LBound(aArray) To UBound(aArray)
    MsgBox aArray(index)
    Next
    [/vba]


    or
    [VBA]
    Dim aArray(1 To 3) As Integer
    Dim index As Integer

    For index = 1 To 3
    aArray(index) = index
    Next

    For index = LBound(aArray) To UBound(aArray)
    MsgBox aArray(index)
    Next
    [/VBA]
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  3. #3
    Thank you for your quick reply. Unfortunately what I really needed, but failed to explain, was bulk assignment of an multidimensional array.

    I have code like this.

    [VBA]
    Dim stringArray(0 To 5, 0 To 1) As String

    stringArray(0, 0) = "C2"
    stringArray(0, 1) = "14"
    stringArray(1, 0) = "C19"
    stringArray(1, 1) = "3"
    stringArray(2, 0) = "F2"
    stringArray(2, 1) = "12"
    stringArray(3, 0) = "F17"
    stringArray(3, 1) = "2"
    stringArray(4, 0) = "C25"
    stringArray(4, 1) = "6"
    stringArray(5, 0) = "C34"
    stringArray(5, 1) = "4"
    [/VBA]

    Is there an easier way to assign values that have no pattern, quickly into an array. I think your use of variant type, along with your array(1, 2...) may do the trick in simplifying this assignment, but i would really like to have this type of the bracket bulk type of assignment. Is there a way?

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Dim stringArray() As Variant

    stringArray = [{"C2",14;"C19",3; "F2",12;"F17",2; "C25", 6;"C34", 4}]
    [/vba]
    ____________________________________________
    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

  5. #5
    Great! Thanks xld. This does exactly what I needed. Now I just have two follow up questions. Why wont it let me write...

    [VBA]Dim stringArray() As String
    stringArray = [{"C2","14";"C19","3"; "F2","12";"F17","2"; "C25", "6";"C34", "4"}][/VBA]

    and why is this array base 1 even when choose option base 0?

  6. #6
    the variant array you wrote.

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    You need to declare it as type variant, VBA will sub-type it.

    It is always base 1 because you are effectively doing a Range dump into an array, and this is always base 1, regardless of any option.
    ____________________________________________
    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

  8. #8
    Oh ok xld. Thanks for the help!

Posting Permissions

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