Search code examples
arraysvbalistclassuserform

What is the best strategy in VBA to transfer a full array from one userform lists to another?


I am passing the content of a two dimensional array (first column header and than all column numbers) to a userform named Listbox1. Here the user selects items that are passed to Listbox2 and at last the user activates a process that sends data to a webservice.

So far I have managed to populate Listbox1 with data headers only but have the problem to retain all values in listbox2, not only the header. I thought of creating a private variable at the userform level to store the full array but it seems to generate a type mismatch issue with the let/get properties.

What is the best strategy to handle a full set of data with two lists?


Private pArr As Variant
Public Property Get arr() As Variant
    Set arr = pArr
End Property
Public Property Let arr(Value As Variant)
    Set pArr = Value
End Property

Private Sub LoadModelData()

    Dim i As Integer
    Dim myArray As Variant
    Dim v As Variant
    Dim mystring As String

    myArray = ReadModelData(this is the function returning the array data from a range)

    Set pArr = myArray

    For i = LBound(myArray) To UBound(pArr)
        If pArr(i)(1, 1) <> vbNullString Then
            frmListModelItms.List1.AddItem (pArr(i)(1, 1))
        End If
    Next i

End Sub

Solution

  • my understading is arr property of your class is an array, not an object

    hence don't use Set keyword:

    Private pArr As Variant
    
    Public Property Get arr() As Variant
        arr = pArr
    End Property
    
    Public Property Let arr(Value As Variant)
        pArr = Value
    End Property