Some help needed on arrays

mahmed1

Well-known Member
Joined
Mar 28, 2009
Messages
2,302
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi people

Can anyone of you lovely people help me understand why and when to use transpose with arrays vbvba and what people mean by you can only redim 2nd dimention


just a small data set with some examples would really be appreciated

thank you
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Does this help?

Code:
    Dim av As Variant
    
    ReDim av(1 To 10, 1 To 5)
    

    ' this won't work with the Preserve keyword:
    ReDim Preserve av(1 To 20, 1 To 5)
    

    ' this will, but it's awkward:
    av = WorksheetFunction.Transpose(av)
    ReDim Preserve av(1 To 5, 1 To 20)
    av = WorksheetFunction.Transpose(av)
 
Upvote 0
It shows that you can't change the first dimension of a 2D array, and then how you can work around that by transposing, changing the second dimension, and then transposing again.

Look at the array in the Locals window while stepping through the code.
 
Upvote 0
Hi


can you redim 1st dim without the preserve keyword or is it a case of you can't redim 1st dim full stop with or without preserve?


thanks


ps I know sometimes we use transpose with the join function


How would you transpose a 2d to 1d to use join ?


i just wanted an example to when we use transpose with join function


thanks
 
Upvote 0
It shows that you can't change the first dimension of a 2D array, ..
Sorry, that should read 'anything but the first'
can you redim 1st dim without the preserve keyword ...
Yes, sure, any and all dimensions.

ps I know sometimes we use transpose with the join function. How would you transpose a 2d to 1d to use join? I just wanted an example to when we use transpose with join function
I think you mean Split. Step through this:

Code:
    Dim v as variant
    

    ' v is (0 to 2) (Split always returns a 0-based array)
     v = Split("a b c")
    

    ' which writes as a row vector:
    Range("A1:C1").Value = v
    Cells.ClearContents
    

    ' this writes three copies:
    Range("A1:C3").Value = v
    Cells.ClearContents
   

    ' this converts v to (1 to 3, 1 to 1)
    v = WorksheetFunction.Transpose(v)
    

    ' which writes as a column vector:
    Range("A1:A3").Value = v
    Cells.ClearContents
    

    ' this writes three copies:
    Range("A1:C3").Value = v
    Cells.ClearContents
 
Upvote 0
Hi

If I had a 2d or 3d array and I had (dim arr (1 to 2, 1 to 3, 1 to 6) and I did ubound(arr) what result do I get because I did not specify dimension ie ubound(arr,2)
 
Upvote 0
You will get 2. If you don't specify which dimension UBound refers to, it defaults to the first one.
 
Upvote 0

Forum statistics

Threads
1,214,591
Messages
6,120,427
Members
448,961
Latest member
nzskater

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top