+ Reply to Thread
Results 1 to 9 of 9

assigning multidimensional arrays and then using Ubound.........i am using it wrong.

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712

    assigning multidimensional arrays and then using Ubound.........i am using it wrong.

    I have the following code:

    ReDim stdy_file_array1(UBound(file_array1), 3)
    
    For x = 1 To UBound(file_array1)
    stdy_file_array1(x, 1) = Left(file_array1(x), InStr(1, file_array1(x), "_") - 1)
    stdy_file_array1(x, 2) = path1
    stdy_file_array1(x, 3) = file_array1(x)
    Next x


    But for some reason when i try to find the ubound using the following .....i get an error.


    MsgBox ("ubound of stdy_file_array1 is   " & UBound(stdy_file_array1, 1))
    MsgBox ("ubound of stdy_file_array1 is   " & UBound(stdy_file_array1, 2))
    MsgBox ("ubound of stdy_file_array1 is   " & UBound(stdy_file_array1, 3))

    So i think i am confused about something or not doing something correct. I view the array above as having 3 cols by x-number of rows.

  2. #2
    Registered User
    Join Date
    10-03-2011
    Location
    Quebec, Canada
    MS-Off Ver
    Excel 2010
    Posts
    29

    Re: assigning multidimensional arrays and then using Ubound.........i am using it wro

    Your array only has 2 dimensions.. and you try to get the UBound of the 3rd dimension...

    UBound(stdy_file_array1, 3) means "get the upper bound of the 3rd dimension", but in your redim, you only have 2 dimensions.

  3. #3
    Registered User
    Join Date
    10-03-2011
    Location
    Quebec, Canada
    MS-Off Ver
    Excel 2010
    Posts
    29

    Re: assigning multidimensional arrays and then using Ubound.........i am using it wro

    and also, be advised you can only redim the last dimension if you want to preserve data.

    So sometime, you need to add columns instead of rows (for a 2 dimensions array). making each column represent a record. Once you've done, you can transpose it to make it a row per record, as we are more used to use...

  4. #4
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712

    Re: assigning multidimensional arrays and then using Ubound.........i am using it wro

    for some reason i thought that by having the "stdy_array1(14,3)" that this ment i had 3 dimensions......but your right.......now that i think about it its only 2 dimensions.

    Makes sense

  5. #5
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712

    Re: assigning multidimensional arrays and then using Ubound.........i am using it wro

    you read my mind.............so if i used


    lets assume i had the following:

    redim stdy_array1(10,3)


    then i do somethings in my code


    now i want to add items to my array

    so i do the following

    redim preserve stdy_array(100,3)


    this wont work? is that what your saying? the first 10 items in my array wont be preserved?

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: assigning multidimensional arrays and then using Ubound.........i am using it wro

    When you dimension an array, you should explicitly define the lower boundary of each dimension:

    dim myArray(1 to 10, -3 to 6, 0 to 2) as whatever
    Otherwise, the lower boundary defaults to zero, unless you use Option Base 1, which is not great practice.
    Entia non sunt multiplicanda sine necessitate

  7. #7
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712

    Re: assigning multidimensional arrays and then using Ubound.........i am using it wro

    not to get off topic......but what is wrong with arrays starting at 1...........what is the advantage of starting them at "0"......i thought there was no advantage either way just a preference thing.

  8. #8
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: assigning multidimensional arrays and then using Ubound.........i am using it wro

    It doesn't matter what they start with -- it's a matter of convenience. But it does matter how many elements you have, especially if you're writing an array to a worksheet:

    Sub x()
        Dim i           As Long
        Dim ad1(1 To 3) As Double
        Dim ad2(3)      As Double
    
        For i = 1 To 3
            ad1(i) = i ^ 2
            ad2(i) = i ^ 2
        Next i
    
        Range("A1:C1").Value = ad1
        Range("A2:D2").Value = ad2
    End Sub

  9. #9
    Registered User
    Join Date
    10-03-2011
    Location
    Quebec, Canada
    MS-Off Ver
    Excel 2010
    Posts
    29

    Re: assigning multidimensional arrays and then using Ubound.........i am using it wro

    Exactly, you can't redim preserve an array if you change the first dimension.

    That's why it would be better to do this

    redim stdy_array1(3,10)
    
    and later do
    
    redim preserve stdy_array1(3, 100)
    that way, the 3 headers you had when you declared it (10,3) will now be the first column instead of the first row. and you can add as many column as you want.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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