Basic help with Arrays in VBA

TheNoocH

Well-known Member
Joined
Oct 6, 2004
Messages
3,482
I'm trying to learn arrays in VBA and am testing some basic concepts...
i have the following code in which the TestArrayCol sub is working but the TestArrayRow is not....getting a subscipt out of range error on the msgbox myarrayrow(j) line...i'm sure i'm overlooking something simple...any help would be appreciated...

Code:
Sub TestArrayCol()
Dim myArrayCol As Variant, i As Integer
myArrayCol = WorksheetFunction.Transpose(Range("A1:A5"))
For i = LBound(myArrayCol) To UBound(myArrayCol)
MsgBox myArrayCol(i)
Next i
End Sub

Sub TestArrayRow()
Dim myArrayRow As Variant, j As Integer
myArrayRow = Range("B1:G1")
For j = LBound(myArrayRow) To UBound(myArrayRow)
MsgBox myArrayRow(j)
Next j
End Sub
 
in the lbound/ubound section if i did lbound(myarraycol1,2) to ubound(myarraycol1, 2)
it only returned the first element...
Of course, because your counter is always 1,
LBound(myarraycol1,2)=1 and UBound(myarraycol1,2)=1

the size of your array is
myarraycola(1 to 5, 1 to 1)

does it explain?
 
Upvote 0

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
So the code should look like
Code:
Sub TestArrayCol1() 
Dim myArrayCol1 As Variant, i1 As Integer 
myArrayCol1 = Range("A1:A5") 
For i1 = LBound(myArrayCol1,1) To UBound(myArrayCol1,1) 
MsgBox myArrayCol1(i1, 1) 
Next i1 
End Sub
if you want to dispplay a,b,c,d,e
 
Upvote 0
Jindon,
that's the code i had...see my last post with code...

but the question i didn't understand was

in the lbound/ubound section if i did lbound(myarraycol1,2) to ubound(myarraycol1, 2)
it only returned the first element...
especially weird since in the msgbox line i needed it to look like a 2 dimensional array...ie MsgBox myArrayCol1(i1, 1) ..
 
Upvote 0
Nooch, run this with your view locals window open (or a watch on myArrayRow). Tile apps so you can see what's going on in the worksheet. Step through the code. You'll see how you can use the TRANPOSE to force into a single-dimensioned array. And you can see what happens when you need to feed a single dimensioned array back to cells.

<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> TestArrayRow()
    <SPAN style="color:#00007F">Dim</SPAN> myArrayRow <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Variant</SPAN>, j <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN>
    myArrayRow = WorksheetFunction.Transpose(Range("B1:G1"))
    myArrayRow = WorksheetFunction.Transpose(WorksheetFunction.Transpose(Range("B1:G1")))
    
    <SPAN style="color:#00007F">For</SPAN> j = <SPAN style="color:#00007F">LBound</SPAN>(myArrayRow) <SPAN style="color:#00007F">To</SPAN> <SPAN style="color:#00007F">UBound</SPAN>(myArrayRow)
        myArrayRow(j) = j * 5
    <SPAN style="color:#00007F">Next</SPAN> j
    Range("b1:G1") = myArrayRow
    Range("B2:B7") = myArrayRow
    Range("B2:B7") = WorksheetFunction.Transpose(myArrayRow)
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>
HTH
 
Upvote 0
OK

What I was trying to say is
2nd dimention of your array is what you have in 1st dimention.

You have 1 to 5 in the 1st dimention and
1 to 1 in the 2nd dimention

so, you only have 1 element for each index of 1 st demention

say if you substitute "a1:c5" then you have
1 to 5 in 1st dimention and 1 to 3 in 2nd dimention

You know what I mean?
 
Upvote 0

Forum statistics

Threads
1,214,621
Messages
6,120,563
Members
448,972
Latest member
Shantanu2024

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