Some example methods here ...
Public Sub DefineArray()
Dim i As Long
' ---------------------------------------------------------------
' Using the Array function
' ---------------------------------------------------------------
Dim arrTest1 As Variant
arrTest1 = Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
For i = 0 To UBound(arrTest1)
Debug.Print arrTest1(i)
Next
' ---------------------------------------------------------------
' ReDim Preserve
' ---------------------------------------------------------------
Dim arrTest2() As String
For i = 0 To 10
ReDim Preserve arrTest2(i)
arrTest2(i) = i
Next
For i = 0 To UBound(arrTest2)
Debug.Print arrTest2(i)
Next
' ---------------------------------------------------------------
' Fixed at declaration
' ---------------------------------------------------------------
Dim arrTest3(10) As String
For i = 0 To UBound(arrTest3)
arrTest3(i) = i
Next
For i = 0 To UBound(arrTest3)
Debug.Print arrTest3(i)
Next
' ---------------------------------------------------------------
' Using a function that returns an array, e.g. Split
' ---------------------------------------------------------------
Dim strTest As String, arrTest4 As Variant
strTest = "This is a test"
arrTest4 = Split(strTest, " ")
For i = 0 To UBound(arrTest4)
Debug.Print arrTest4(i)
Next
' ---------------------------------------------------------------
' From a range
' ---------------------------------------------------------------
Dim arrTest5 As Variant, lngRow As Long, lngCol As Long
arrTest5 = Sheet1.Range("A1:K10").Value
For lngRow = LBound(arrTest5, 1) To UBound(arrTest5, 1)
For lngCol = LBound(arrTest5, 2) To UBound(arrTest5, 2)
Debug.Print arrTest5(lngRow, lngCol)
Next
Next
End Sub
Hopefully that gives you a good cross section and as per the comment, make sure it's contained within a function or procedure (sub). See my example.
Sub,Function,Property [Get|Let|Set]).Dimisn't an executable statement, it's legal at module-level, only in the module's(declarations)section (at the top). You need to put thePixels = Array(1, 2, 3)assignment in a procedure in the same module as the declaration (could be another module if it wasPublic Pixels(1 To 3) As Integer).Dim Pixels() As VariantorDim Pixels As Variantinstead ofDim Pixels(1 To 3) As Integerto make possible the result ofArray()function be assigned to the variable.Variant."