且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

检索集合中的项目(Excel,VBA)

更新时间:2023-02-14 09:59:47

我设法解决了这个问题.将我的集合转换为数组,并将集合设置为inputparameter.遍历我的整个集合,并将其分配到数组中.该问题似乎与.List-function有关,仅允许将数组作为variant-datatype.解决了;受到( http://www. iwebthereforeiam.com/iwebthereforeiam.com/2004/06/excel-vba-code-to-convert-coll.html ).

I manage to solve it. Converting my collection to an array, and setting my collection as inputparameter. Looping through my entire collection, and allocating it in an array. The issue seems to be related to .List-function, only allowing arrays as variant-datatype. It was solved; inspired by (http://www.iwebthereforeiam.com/iwebthereforeiam/2004/06/excel-vba-code-to-convert-coll.html).

Sub FillListBox(sListName As String)

    With frm_T1_Kundeoplysninger.Controls.Item(sListName)
        .List = ConvertCollectionToArray(CustomerCollection)
    End With

Clearing:
    Set CustomerCollection = Nothing

End Sub

Function ConvertCollectionToArray(cCustomers As Collection) As Variant()

    Dim arrCustomers() As Variant: ReDim arrCustomers(0 To cCustomers.Count - 1, 16)
    Dim i As Integer

    With cCustomers
        For i = 1 To .Count
            arrCustomers(i - 1, 0) = .Item(i).customerID
            arrCustomers(i - 1, 1) = .Item(i).customerName
            arrCustomers(i - 1, 2) = .Item(i).customerCompanyName
            arrCustomers(i - 1, 3) = .Item(i).customerFullName
            arrCustomers(i - 1, 4) = .Item(i).customerCVR
            arrCustomers(i - 1, 5) = .Item(i).customerType
            arrCustomers(i - 1, 6) = .Item(i).customerGroup
            arrCustomers(i - 1, 7) = .Item(i).customerCountry
            arrCustomers(i - 1, 8) = .Item(i).customerStreet
            arrCustomers(i - 1, 9) = .Item(i).customerZipcode
            arrCustomers(i - 1, 10) = .Item(i).customerCity
            arrCustomers(i - 1, 11) = .Item(i).customerPhoneNum
            arrCustomers(i - 1, 12) = .Item(i).customerMobileNum
            arrCustomers(i - 1, 13) = .Item(i).customerEmail
            arrCustomers(i - 1, 14) = .Item(i).customerInvoiceEmail
            arrCustomers(i - 1, 15) = .Item(i).customerCreationDate
            arrCustomers(i - 1, 16) = .Item(i).customerLastChange
        Next
    End With

    ConvertCollectionToArray = arrCustomers

End Function