I'm struggling with this program I'm trying to write. In it, I need to use a two dimensional array that looks like array(# rows in table, 2). For some reason, even though I can get the number of rows correctly, I keep getting the Compile error: Constant expression required.
If anybody could give me any help it'd be much appreciated. I can't just assign a static number to it because the table is always going to be increasing as time goes on, and I know you cannot resize an array once it's built.
This is my code if it'll provide any help.
Sub arrayToTest()
Dim rng As Range, row As Range, cell As Range, RowCount As Long, rowsinTable As Integer
Set rng = Sheets("Sheet1").Range("TestTable")
rowsinTable = rng.Rows.Count
Dim arrayTest(rowsinTable, 2) As String
RowCount = 1
For Each row In rng.Rows
arrayTest(RowCount - 1, 0) = rng.Cells(RowCount, 2)
RowCount = RowCount + 1
Next row
End Sub
The test table I'm using has 3 rows.
Dim arrayTest(rowsinTable, 2) As String(or the correct version ofReDim arrayTest(rowsinTable, 2) As String) is dimensioning your array to be arowsinTable+1x3array, because you appear to be using base 0 - so the array is sized0 To rowsInTable, 0 To 2.ReDim Preservestatement will allow modifications to the last dimension of an array without affecting the existing contents of the array. So you could, after building the array, decide to increase your 2 to 3, or reduce it to 1. And, if you usedTransposeon the array, you would then be able to change what was the first dimension (but following the transpose is now the second dimension), resize, and then transpose it back again. (But definitely better to do it as you are doing if you know the number of rows!)