declare dynamic array with 3 columns

kinetic

New Member
Joined
Nov 23, 2009
Messages
9
hello

i am trying to declare a dynamic array with 3 columns. within a for loop i would like to add to the array so it is unknown how many record elements will be in the array.

here is what i have so far which is not working

Code:
'declare array
Dim final_array() As String

'add records to the array
ReDim final_array(1 To 3, 1 To UBound(final_array) + 1)
                            
final_array(0, UBound(final_array) - 1) = Current_Sheet
final_array(1, UBound(final_array) - 1) = Current_Ticker
final_array(2, UBound(final_array) - 1) = Current_PCT_Change

'test array
MsgBox (UBound(final_array))
MsgBox final_array(1, 1)

I never get a message box

Any help is GREATLY appreciated!
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Rich (BB code):
'declare array
Dim final_array() As String

'add records to the array
ReDim final_array(1 To 3, 1 To UBound(final_array) + 1)
                            
final_array(0, UBound(final_array) - 1) = Current_Sheet
final_array(1, UBound(final_array) - 1) = Current_Ticker
final_array(2, UBound(final_array) - 1) = Current_PCT_Change

'test array
MsgBox (UBound(final_array))
MsgBox final_array(1, 1)
One thing I noticed is that you're defining the first dimension as 1 to 3, then using 0 as an index. This will cause an error. So it will jump to the error handler, explaining why you don't get a message box.
 
Upvote 0
nice catch

i changed it but same results - not working

Code:
[COLOR=#555555][FONT=Roboto]‘declare array
Dim final_array() As String[/FONT][/COLOR]
[COLOR=#555555][FONT=Roboto]‘add records to the array
ReDim final_array(0 To 2, 0 To UBound(final_array) + 1)[/FONT][/COLOR]
[COLOR=#555555][FONT=Roboto]final_array(0, UBound(final_array) – 1) = Current_Sheet
final_array(1, UBound(final_array) – 1) = Current_Ticker
final_array(2, UBound(final_array) – 1) = Current_PCT_Change[/FONT][/COLOR]
[COLOR=#555555][FONT=Roboto]‘test array
MsgBox (UBound(final_array))
MsgBox final_array(1, 1)[/FONT][/COLOR]
 
Upvote 0
Two other thoughts:

Rich (BB code):
ReDim Preserve final_array(0 To 2, 1 To UBound(final_array, 2) + 1)

First, if you want to keep the values already in the array, you need the Preserve option. Second, you need the ,2 in the UBound function to get the size of the second dimension.

Also, what are Current_Sheet, Current_Ticker and Current_PCT_Change defined as? Your array is defined as String. VBA should be able to coerce most data types into String, but not all of them. If Current_Sheet is defined as Worksheet, that will cause an error too.

Finally, this may work for you for small size arrays, but for larger arrays it's pretty inefficient. For a Redim Preserve, VBA has to create another array, then copy the values over, then dispose of the first array. If you have enough memory on your PC (and modern PCs usually have plenty), it's best to just define the array initially as big as you think it'll ever get.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,889
Messages
6,175,224
Members
452,620
Latest member
dsubash

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