VBA: questions about working with arrays

BIGTONE559

Active Member
Joined
Apr 20, 2011
Messages
336
Greetings,

I'm trying to learn how to work with arrays. in this sample that i wrote i keep receiving an error at the "Msgbox MyArray(i)" line. the goal is to have the msgbox cycle through all of the values in the array. the error is "Subscript out of Range"

Code:
Sub Sample()
Dim i As Integer


Dim MyArray() As Variant
MyArray = Range("MyDates")
For i = 1 To UBound(MyArray)
MsgBox MyArray(i)
Next i

End Sub
All help is appreciated.
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Greetings,

I'm trying to learn how to work with arrays. in this sample that i wrote i keep receiving an error at the "Msgbox MyArray(i)" line. the goal is to have the msgbox cycle through all of the values in the array. the error is "Subscript out of Range"

Code:
Sub Sample()
Dim i As Integer
 
 
Dim MyArray() As Variant
MyArray = Range("MyDates")
For i = 1 To UBound(MyArray)
MsgBox MyArray(i)
Next i
 
End Sub
All help is appreciated.


I got this modified version to work:

Code:
Sub Sample2()
Dim i As Integer
Dim MyArray() As Variant
MyArray = Application.Transpose(Range("myDates"))
For i = 1 To UBound(MyArray)
MsgBox CDate(MyArray(i))
Next i
End Sub
Code:
 
Upvote 0
What you have defined is NOT a 1-D array, but a 2-D array, so you need to tell Excel which dimension of the array you want returned in the msgbox. Like this (assumes MyDates is an N row x 1 Column array):
Code:
Sub Sample()
Dim i As Integer
Dim MyArray() As Variant
MyArray = Range("MyDates") 'this is a 2-D array NOT 1-D
For i = 1 To UBound(MyArray, 1)
    MsgBox MyArray(i, 1)
Next i

End Sub
Suppose your range "MyDates" is A1:A4
then MyArray(1,1)= the value in A1; MyArray(2,1) = the value in A2 and so on. MyArray is a 4 rows X 1 column array.
 
Upvote 0
Also, if you want to make it a string value:

Code:
Sub Sample3()
Dim i As Integer
Dim MyArray() As Variant
MyArray = Application.Transpose(Range("myDates"))
For i = 1 To UBound(MyArray)
MsgBox Format(MyArray(i), "mm/dd/yyyy")
Next i
End Sub
Code:
 
Upvote 0
I got this modified version to work:

Code:
Sub Sample2()
Dim i As Integer
Dim MyArray() As Variant
MyArray = Application.Transpose(Range("myDates"))
For i = 1 To UBound(MyArray)
MsgBox CDate(MyArray(i))
Next i
End Sub
Code:

Worked Perfectly... I noticed you used application.transpose to change a range to array values. will i have to use this method to change range values in the future?
 
Upvote 0
What you have defined is NOT a 1-D array, but a 2-D array, so you need to tell Excel which dimension of the array you want returned in the msgbox. Like this (assumes MyDates is an N row x 1 Column array):
Code:
Sub Sample()
Dim i As Integer
Dim MyArray() As Variant
MyArray = Range("MyDates") 'this is a 2-D array NOT 1-D
For i = 1 To UBound(MyArray, 1)
    MsgBox MyArray(i, 1)
Next i

End Sub
Suppose your range "MyDates" is A1:A4
then MyArray(1,1)= the value in A1; MyArray(2,1) = the value in A2 and so on. MyArray is a 4 rows X 1 column array.


JoeMO I just seen your response and i have some questions. What do you mean by a 2-D array (i'm assumming dimensions????). does it change if the list was in rows as oppose to columns e.g. data in A1:D1. If i wanted value C1 MyArray(3,1)?

How would you loop through multiple arrays?

e.g. two sets of different arrays MyDates and My Colors (somewhat different from the original question)
 
Upvote 0
What you have defined is NOT a 1-D array, but a 2-D array, so you need to tell Excel which dimension of the array you want returned in the msgbox. Like this (assumes MyDates is an N row x 1 Column array):
Code:
Sub Sample()
Dim i As Integer
Dim MyArray() As Variant
MyArray = Range("MyDates") 'this is a 2-D array NOT 1-D
For i = 1 To UBound(MyArray, 1)
    MsgBox MyArray(i, 1)
Next i

End Sub
Suppose your range "MyDates" is A1:A4
then MyArray(1,1)= the value in A1; MyArray(2,1) = the value in A2 and so on. MyArray is a 4 rows X 1 column array.


JoeMO I just seen your response and i have some questions. What do you mean by a 2-D array (i'm assumming dimensions????). does it change if the list was in rows as oppose to columns e.g. data in A1:D1. If i wanted value C1 MyArray(3,1)?

How would you loop through multiple arrays?

e.g. two sets of different arrays MyDates and My Colors (somewhat different from the original question)

"For Each value in MyArray

For Each Element in MyArray1 ' pertains to MyColors "
 
Upvote 0
A1:D1 would be a 1 row X 4 column array so value in C1 would be
MyArray(1,3) and to read out all values via your msgbox you would use:

For i = 1 to Ubound(MyArray,2)

where Ubound(MyArray, 2) is 4 in this example and Ubound(MyArray,1) is 1.

To loop through array of this type (where the array is dim'd as a variant and then set = the values in a contiguous range):

for i = Lbound(MyArray,1) to Ubound(MyArray,1)
msgbox MyArray(i,1)
next i
will produce the values in the first column and ith row.

To go across a row
For i = Lbound(MyArray,2) to Ubound(MyArray,2)
msgbox MyArray(1,i)next i
next i

To loop through all values (read out row by row):
For i = Lbound(MyArray,1) to Ubound(MyArray,1)
For j = Lbound(MyArray,2) to Ubound(MyArray,2)
msgbox MyArray(i,j)
Next j
Next i


BTW for this type of array, the Lbound is always 1.
 
Upvote 0
A1:D1 would be a 1 row X 4 column array so value in C1 would be
MyArray(1,3) and to read out all values via your msgbox you would use:

For i = 1 to Ubound(MyArray,2)

where Ubound(MyArray, 2) is 4 in this example and Ubound(MyArray,1) is 1.

To loop through array of this type (where the array is dim'd as a variant and then set = the values in a contiguous range):

for i = Lbound(MyArray,1) to Ubound(MyArray,1)
msgbox MyArray(i,1)
next i
will produce the values in the first column and ith row.

To go across a row
For i = Lbound(MyArray,2) to Ubound(MyArray,2)
msgbox MyArray(1,i)next i
next i

To loop through all values (read out row by row):
For i = Lbound(MyArray,1) to Ubound(MyArray,1)
For j = Lbound(MyArray,2) to Ubound(MyArray,2)
msgbox MyArray(i,j)
Next j
Next i


BTW for this type of array, the Lbound is always 1.

JoeMO there are times when things happen in life and that little light bulb above your head goes off. You sir. . . have just help that take place for me. I appreciate your responses and i will take this info and do some great things.

Thanks Again!!!!!!!!
 
Upvote 0
Hi Big,

Not sure if this will add anything, but I think a good way to see what is happening is to use the Locals window and step through the code. For instance, with some values like:

Excel 2000
AB
12.31.15
24.62.3
36.93.45
49.24.6
511.55.75
613.86.9
716.18.05
818.49.2
920.710.35
102311.5
Sheet1


Display the Locals window, ditch the parenthesis for the moment and step thru:
Rich (BB code):
Sub example()
Dim MyArray
    
    MyArray = Sheet1.Range("A1:A10").Value
    MyArray = Sheet1.Range("A1:B1").Value
    MyArray = Sheet1.Range("A1:B10").Value
    MyArray = Sheet1.Range("A1").Value
    
    MyArray = Application.Transpose(Sheet1.Range("A1:A10").Value)
    
    MyArray = Application.Transpose(Sheet1.Range("A1:B10").Value)
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,267
Messages
6,123,964
Members
449,137
Latest member
yeti1016

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