I am trying to assign values from column A to an array.
I want the size of the array to be dynamic. I want to loop through each cell and assign every cell value to my array.
Sub exercise3()
Dim asnwer
Dim output
Dim lastrow
Dim test
Dim i
Dim Data() As Variant 'Creating an dynamic array
Dim endnumber
lastrow = cells(Rows.Count, 1).End(xlUp).Row 'Find last row in column A
MsgBox (lastrow)
For i = 1 To lastrow
Data(i) = cells(i, "A").Value 'I want to assign each cell value to my array
Next i
MsgBox (Data(6))
Set output = cells(4, 4)
answer = WorksheetFunction.Average(Data)
output.Value = answer
End Sub
I get an error in my loop
"subscript out of range".
I have 14 rows in column A. If I declare the array with 14 positions/variables like this: Dim data (14), it is working. However I want it to be dynamic, so I can add/remove rows.