PDA

View Full Version : Array issues - one vs two dimensional



nikki333
12-06-2019, 12:36 PM
Hi Folks

I've always had trouble with arrays; should they start from 0 or 1? mulitdimesion or not?....
Currently, I'm trying to declare two one dimesional arrays and then consolidate them in another array.

Simple example (with Option base 1):

arrCardsCategories = Array("Hearts", "Tiles", "Pikes", "Clovers")arrCardsValues = Array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13")
ReDim arrCardsTotal(1 To UBound(arrCardsCategories) * UBound(arrCardsValues), 1 To 1)

for i = 1 to ubound(arrCardsCategories)
for j = 1 to ubound(arrCardsValues)
arrCardsTotal(i, j) = arrCardsCategories(i) & arrCardsValues(j)
next j
next i

The problem is that the first two arrays are "one-dimensional", while the other array is 2-dimensional.

In essence, I just want to put those ****ing 52 cards in one array (1 to 52). Is there a way to define simple arrays like above to two-dimensional?

Paul_Hossler
12-06-2019, 12:55 PM
With Option Base 1, arrCardsCategories goes from 1 - 4

Without Option Base 1 (the default), arrCardsCategories goes from 0 - 3

1. I always like to use LBound and UBound

2. You can start the array bounds anywhere -- e.g. I've used Dim Ary(23 to 48) as String when I needed to process rows 23 through 48, just kept things neater (IMVHO)


Not sure why you need a 2-dim array anywhere




Option Explicit


Sub phh()
Dim arrCardsCategories As Variant, arrCardsValues As Variant, arrCardsTotal() As String
Dim i As Long, j As Long, k As Long


arrCardsCategories = Array("Hearts", "Tiles", "Pikes", "Clovers") ' 0 - 3
arrCardsValues = Array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13") ' 0 - 12


ReDim arrCardsTotal(0 To (UBound(arrCardsCategories) + 1) * (UBound(arrCardsValues) + 1) - 1) ' 0 - 51


k = 0


For i = LBound(arrCardsCategories) To UBound(arrCardsCategories)
For j = LBound(arrCardsValues) To UBound(arrCardsValues)
arrCardsTotal(k) = arrCardsCategories(i) & arrCardsValues(j)
k = k + 1
Next j
Next i
End Sub



Edit -- added a link that you might find useful

https://bytecomb.com/arrays-in-vba-part-2-array-assignment-rules/