如何在VBA中编写一个返回对象列表的函数

3

我有一个表格,想要将其中每一行转换成一个对象。该对象将会拥有每一列的属性。

我写了这个函数:

Public Function GetProducts() As Object
   Set GetProducts = New Collection

    Dim p As New ProductInfo
    Dim rng As Range
    Dim xRow As Range

    Set rng = Sheet2.Range("A2:I" & Sheet2.Range("A2").End(xlDown).Row)

        For Each xRow In rng.Rows

            p.Id = xRow.Cells(1, 1).Value
            p.Cod = xRow.Cells(1, 2).Value
            p.Name = xRow.Cells(1, 3).Value
            p.productType = xRow.Cells(1, 4).Value
            p.norma = xRow.Cells(1, 5).Value
            p.masina = xRow.Cells(1, 6).Value
            p.masinaType = xRow.Cells(1, 7).Value
            p.operatori = xRow.Cells(1, 8).Value
            p.sectie = xRow.Cells(1, 9).Value

            GetProducts.Add Item:=p, Key:=CStr(p.Id)

        Next xRow

End Function

我尝试使用以下子程序检查该功能:

Public Sub CheckProducts()
    Dim products As Collection
    Dim p As ProductInfo
    Set products = GetProducts()

        For Each p In products
            MsgBox p.Id
        Next p

End Sub

消息框总是返回20(我有20个项目在我的表中,最后一个ID是20)。

当我检查集合中的项目数量时,我得到了20,正如我所期望的那样。

有人能帮我理解为什么我无法迭代集合并获取每个项目的ID吗?


能否把你的ProductInfo代码分享一下?还有个愚蠢的明显问题——A列的值都是唯一的,对吧? - Absinthe
1个回答

6
GetProducts() 函数中,你需要编写:
Dim p As ProductInfo

而不是:

Dim p As New ProductInfo

然后在循环代码中:

Set p = New ProductInfo

以下是一个示例:

数据

enter image description here

类 - TestInfo

Private m_Id As String
Private m_Code As String
Private m_Name As String

Property Get Id() As String
    Id = m_Id
End Property
Property Let Id(str As String)
    m_Id = str
End Property

Property Get Code() As String
    Code = m_Code
End Property
Property Let Code(str As String)
    m_Code = str
End Property

Property Get Name() As String
    Name = m_Name
End Property
Property Let Name(str As String)
    m_Name = str
End Property

模块

Option Explicit

Sub Test()
    Dim coll As Collection
    Dim obj As TestInfo

    Set coll = GetProducts

    For Each obj In coll
        MsgBox obj.Name
    Next
End Sub

Public Function GetProducts() As Collection

    Set GetProducts = New Collection

    Dim rngData As Range
    Dim lngCounter As Long
    Dim obj As TestInfo '<--- do not use New here

    Set rngData = ThisWorkbook.Worksheets("Sheet1").Range("A1:C7")

    For lngCounter = 2 To rngData.Rows.Count
        Set obj = New TestInfo '<--- use New here

        obj.Id = rngData.Cells(lngCounter, 1).Value
        obj.Code = rngData.Cells(lngCounter, 2).Value
        obj.Name = rngData.Cells(lngCounter, 3).Value

        GetProducts.Add obj

    Next lngCounter

End Function

注意

我个人不会使用以下语句:

Set GetProducts = New Collection

相反,我会这样做:

Public Function GetProducts() As Collection

    Dim coll As Collection
    Dim rngData As Range
    Dim lngCounter As Long
    Dim obj As TestInfo

    Set rngData = ThisWorkbook.Worksheets("Sheet1").Range("A1:C7")
    Set coll = New Collection

    For lngCounter = 2 To rngData.Rows.Count
        Set obj = New TestInfo
        obj.Id = rngData.Cells(lngCounter, 1).Value
        obj.Code = rngData.Cells(lngCounter, 2).Value
        obj.Name = rngData.Cells(lngCounter, 3).Value
        coll.Add obj
    Next lngCounter

    Set GetProducts = coll

End Function

为什么?

在stackoverflow上有很多关于此问题的问答可以阅读和考虑:


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接