Hey guys, I'm having a bit of trouble understanding the LBound and Ubound functions for VBA.
I attached below what my book explains what LBound and UBound does and I
kinda get it for one dimensional arrays (but have no idea what it does for 2 dimensional arrays).
So firstly, my understanding of LBound and UBound for 1-d arrays is basically it treats the array like a matrix, then LBound just pumps out the number that the first column is named with and UBound pumps out the number that the last column is named with. For example, in the attached picture, MyArray(5), since by default the first "column" of the matrix is named 0, LBound would be 0 and UBound is 5. But say we had a different code, something like this that specifies which index the array starts: [Emphasized in the following code with >>>>>>>>>>>]. Then LBound would be 6 and UBound would be 10, right?
Sub ArrayDemo4()
>>>>>>>>>>>>>Dim MyArray(6 To 10)
Dim i As Integer
MsgBox “Index of MyArray Starts at:” & _
LBound(MyArray)
MsgBox “Index of MyArray Stops at:” & _
UBound(MyArray)
For i = LBound(MyArray) To UBound(MyArray)
MyArray(i) = i * i
Next i
MsgBox “The Value in MyArray(7) is: “ & _
MyArray(7)
End Sub
Ok, now I don't seem to get what LBound and UBound does for multidimensional arrays, using the MyMat1 (ie, 3 by 2 array).
This part of the code confuses me:
For J = LBound(MyMat1, 2) To
UBound(MyMat1, 2)
It should really be:
For J = 1 To 2
But the book is just highlighting the fact that LBound/UBound can also be used, however I don't get how LBound(MyMat1, 2) =1 and UBound(MyMat1, 2) =2?
Namely, what is the 2 in LBound(MyMat1, 2) and UBound(MyMat1, 2) mean? The explanation says this array is a 2 dimensional array, how?? It's a 3 by 2 array, how do you determine it's dimension? And how does LBound, UBound work in general for multi dimensional arrays? Maybe my fundamental understanding of UBound/LBound is wrong so that's why I don't seem to get it, please correct me!
Thank you!