VBA: store an array in class

March 1, 2011

In a module put this code

Sub test()

Set ArrayClass = New Class1

For i = ArrayClass.lower To ArrayClass.upper
ArrayClass.WriteArray i, i
Next i

For i = ArrayClass.lower To ArrayClass.upper
msgbox(ArrayClass.ReadArray(i))
Next i

End Sub

Insert a Class module. the first one will be Class1 which is in the above
sub in the New statement. Must be the same

Insert this code into class1module

Dim MyArray As Variant
Public lower As Long
Public upper As Long

Private Sub Class_Initialize()
ReDim MyArray(100)

lower = LBound(MyArray)
upper = UBound(MyArray)
End Sub
Public Sub WriteArray(i, a)

MyArray(i) = a

End Sub
Public Function ReadArray(i)

ReadArray = MyArray(i)

End Function

One Response to “VBA: store an array in class”

  1. AB Says:

    Really helpful code


Leave a comment