使用 Collection.Add 用自定义对象填充 VBA 集合

发布于 2024-09-08 23:16:48 字数 1311 浏览 3 评论 0原文

我正在尝试通过 For 循环中的 sampleCollection.Add 添加自定义对象(事务)。

如果我将字符串而不是对象添加到集合中,该代码将起作用。

Public Function PopCollection()
    Dim sampleCollection As New Collection
    Dim objTrans As New Transaction
    Dim objTrans2 As New Transaction


    '********** SETUP ARRAY FOR LOOP *************
    Dim arrA(0 To 1) As String
    arrA(0) = "Description 1"
    arrA(1) = "Description 2"


    '********** POPULATE COLLECTION *************
    For n = 0 To 1
        objTrans.DESC = arrA(n)
        Call sampleCollection.Add(objTrans)
    Next n


    '********** ITERATE THROUGH COLLECTION *************
    For n = 1 To sampleCollection.Count
        Set objTrans2 = sampleCollection.Item(n)
        Debug.Print n & " - " & objTrans2.DESC
    Next n

End Function

Debug.Print n & “-”&此代码底部的 objTrans2.DESC 行输出“Description 2”两次。我希望它输出“描述 1”和“描述 2”。

以下是 Transaction 类中的信息:

Public PTXN As Integer
Public ACCTID As Integer
Public CHECKNUM As String
Public DESC As String
Public STATUS As String
Public TRANSACTIONDATE As String
Public SPLIT_DESC As String
Public SPLIT_AMT As Single
Public SPLIT_CATEGORY As Integer

我仅将属性声明添加到 Excel 中的 VB 编辑器中。我复制/粘贴了那里列出的内容。

I am trying to add a custom object (Transaction) through the sampleCollection.Add from within a For loop.

The code works if I add strings to the collection instead of objects.

Public Function PopCollection()
    Dim sampleCollection As New Collection
    Dim objTrans As New Transaction
    Dim objTrans2 As New Transaction


    '********** SETUP ARRAY FOR LOOP *************
    Dim arrA(0 To 1) As String
    arrA(0) = "Description 1"
    arrA(1) = "Description 2"


    '********** POPULATE COLLECTION *************
    For n = 0 To 1
        objTrans.DESC = arrA(n)
        Call sampleCollection.Add(objTrans)
    Next n


    '********** ITERATE THROUGH COLLECTION *************
    For n = 1 To sampleCollection.Count
        Set objTrans2 = sampleCollection.Item(n)
        Debug.Print n & " - " & objTrans2.DESC
    Next n

End Function

The Debug.Print n & " - " & objTrans2.DESC line at the bottom of this code is outputting "Description 2" twice. I want it to output "Description 1" and "Description 2".

Here is the information in the Transaction class:

Public PTXN As Integer
Public ACCTID As Integer
Public CHECKNUM As String
Public DESC As String
Public STATUS As String
Public TRANSACTIONDATE As String
Public SPLIT_DESC As String
Public SPLIT_AMT As Single
Public SPLIT_CATEGORY As Integer

I only added the property declarations to the VB editor in Excel. I copy/pasted what was listed there.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

友欢 2024-09-15 23:16:48

您需要创建 objTrans 的新实例。您要做的是将 DESC 设置为“描述 1”,添加到集合中,然后将 DESC 更改为“描述 2”(不创建新的 objTrans 实例),然后再次将同一实例添加到集合中。我就是这样做的。

Public Function PopCollection()

    Dim sampleCollection As Collection
    Dim objTrans As Transaction
    Dim arrA As Variant
    Dim n As Long

    arrA = Array("Description 1", "Description 2")
    Set sampleCollection = New Collection

    For n = LBound(arrA) To UBound(arrA)
        Set objTrans = New Transaction
        objTrans.DESC = arrA(n)
        sampleCollection.Add objTrans
    Next n

    For n = 1 To sampleCollection.Count
        Set objTrans = sampleCollection.Item(n)
        Debug.Print n & " - " & objTrans.DESC
    Next n

End Function

You need to make a new instance of objTrans. What you're doing is setting DESC to Description 1, adding to the collection, then changing DESC to Description 2 (not making a new objTrans instance), and adding that same instance to the collection a second time. Here's how I would do it.

Public Function PopCollection()

    Dim sampleCollection As Collection
    Dim objTrans As Transaction
    Dim arrA As Variant
    Dim n As Long

    arrA = Array("Description 1", "Description 2")
    Set sampleCollection = New Collection

    For n = LBound(arrA) To UBound(arrA)
        Set objTrans = New Transaction
        objTrans.DESC = arrA(n)
        sampleCollection.Add objTrans
    Next n

    For n = 1 To sampleCollection.Count
        Set objTrans = sampleCollection.Item(n)
        Debug.Print n & " - " & objTrans.DESC
    Next n

End Function
老娘不死你永远是小三 2024-09-15 23:16:48

稍微修改过的表单(没有 Transaction 类)可以按我的预期工作。我相信您的 Transaction 类有一个错误。你能发布它的代码吗?

A slightly modified form (without the Transaction class) works as intended for me. I believe there is an error is your Transaction class. Can you post the code for it?

子栖 2024-09-15 23:16:48

更简单的版本是将 n=0 设置为 SampleCollection.Count,因为数组的索引以 0 开头,而 n 以 1 开头...

For n = 0 To sampleCollection.Count
    Set objTrans = sampleCollection.Item(n)
    Debug.Print n & " - " & objTrans.DESC
Next n

The simpler version would've been to set you n=0 to SampleCollection.Count as the indexes of an array starts with 0 and your n is starting with 1...

For n = 0 To sampleCollection.Count
    Set objTrans = sampleCollection.Item(n)
    Debug.Print n & " - " & objTrans.DESC
Next n
狼性发作 2024-09-15 23:16:48

无法使用 Collection.add 用自定义对象填充 VBA 集合

代码输出“Description 2”两次

用基本的简单术语来说,这样做总是更好:

    Dim FooCollection As Collection
    Set FooCollection = New Collection
    
    Dim FooClass As classFoo
    Set FooClass = New classFoo
    FooCollection.Add FooClass

而不是:

    Dim FooCollection As New Collection
    etc

因为后者会导致非常微妙的问题。不那么明显的问题,&正如我最近发现的那样,可能不会产生任何错误。

Can't populate VBA Collection with Custom Objects using Collection.add

code is outputting "Description 2" twice

In basic simple terms, it is always better to do:

    Dim FooCollection As Collection
    Set FooCollection = New Collection
    
    Dim FooClass As classFoo
    Set FooClass = New classFoo
    FooCollection.Add FooClass

And not:

    Dim FooCollection As New Collection
    etc

As the latter will cause very subtle & not so apparent issues, & may not generate any errors, as I recently discovered.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文