使用 Collection.Add 用自定义对象填充 VBA 集合
我正在尝试通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要创建 objTrans 的新实例。您要做的是将 DESC 设置为“描述 1”,添加到集合中,然后将 DESC 更改为“描述 2”(不创建新的 objTrans 实例),然后再次将同一实例添加到集合中。我就是这样做的。
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.
稍微修改过的表单(没有 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?
更简单的版本是将 n=0 设置为 SampleCollection.Count,因为数组的索引以 0 开头,而 n 以 1 开头...
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...
用基本的简单术语来说,这样做总是更好:
而不是:
因为后者会导致非常微妙的问题。不那么明显的问题,&正如我最近发现的那样,可能不会产生任何错误。
In basic simple terms, it is always better to do:
And not:
As the latter will cause very subtle & not so apparent issues, & may not generate any errors, as I recently discovered.