将项目添加到 VBA/VB6 集合时出错
我仍在学习 VBA,我不明白为什么 Collections 对象有这么多问题。
我有一个添加自定义对象的函数(我创建了一个非常简单的类来存储一些数据),它执行典型的“读取数据,创建对象表示,将其粘贴到集合中”之类的事情。
如果我尝试向 bag.add 调用添加“密钥”,我会收到“编译错误。预期:=”消息。
如果我不这样做,那么当我运行程序时,它会显示“编译错误。参数不可选”,并突出显示“getRevColumns = bag”行。
我一生都无法弄清楚发生了什么事!我怀疑我初始化包的方式有问题?! PS:columnMap是我的自定义类的名称。
Function getRevColumns() As Collection
Dim rng As Range
Dim i As Integer
Dim bag As Collection
Dim opManCol As Integer, siebelCol As Integer
Dim opManColName As String, siebelColName As String
Dim itm As columnMap
Set bag = New Collection
Set rng = shSiebelMap.UsedRange.Columns(5)
i = 1
For i = 1 To rng.Rows.count
If StrComp(UCase(rng.Cells(i).value), "Y") = 0 Then
opManCol = rng.Rows(i).OffSet(0, -2).value
opManColName = rng.Rows(i).OffSet(0, -4)
siebelCol = rng.Rows(i).OffSet(0, -1).value
siebelColName = rng.Rows(i).OffSet(0, -3)
Set itm = New columnMap
itm.opManColName = opManColName
itm.opManColNumber = opManCol
itm.siebelColName = siebelColName
itm.siebelColNumber = siebelCol
'WHY DOESN'T IT WORK!''
bag.Add (itm)
'MsgBox "opMan Col: " & opManColName & " : " & opManCol & ". Siebel Col: " & siebelColName & " : " & siebelCol'
End If
Next i
getRevColumns = bag
End Function
I'm still learning VBA and I can't figure out wth I'm having so many problems with a Collections object.
I have a function that adds custom objects (I created a very simple class to store some data) that does the typical "read data, create object representation, stick it into Collections" sort of stuff.
If I try to add a "key" to the bag.add call I get a "Compile error. Expected:=" message.
If I don't it appears to have worked then when I run the program it says "Compile Error. Argument not optional" and highlights the "getRevColumns = bag" line.
I can't for the life of me figure out wth is going on! I suspect something wrong with how I initialized my bag?! PS: columnMap is the name of my custom class.
Function getRevColumns() As Collection
Dim rng As Range
Dim i As Integer
Dim bag As Collection
Dim opManCol As Integer, siebelCol As Integer
Dim opManColName As String, siebelColName As String
Dim itm As columnMap
Set bag = New Collection
Set rng = shSiebelMap.UsedRange.Columns(5)
i = 1
For i = 1 To rng.Rows.count
If StrComp(UCase(rng.Cells(i).value), "Y") = 0 Then
opManCol = rng.Rows(i).OffSet(0, -2).value
opManColName = rng.Rows(i).OffSet(0, -4)
siebelCol = rng.Rows(i).OffSet(0, -1).value
siebelColName = rng.Rows(i).OffSet(0, -3)
Set itm = New columnMap
itm.opManColName = opManColName
itm.opManColNumber = opManCol
itm.siebelColName = siebelColName
itm.siebelColNumber = siebelCol
'WHY DOESN'T IT WORK!''
bag.Add (itm)
'MsgBox "opMan Col: " & opManColName & " : " & opManCol & ". Siebel Col: " & siebelColName & " : " & siebelCol'
End If
Next i
getRevColumns = bag
End Function
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试删除 add: 中 itm 周围的括号
,或者
自从我不得不使用 VBA/VB6 以来已经有一段时间了,但我相信包含括号会导致 itm 按值传递而不是按引用传递。我可能是错的。
Try removing the parens around itm in the add:
or
It's been a while since I've had to work with VBA/VB6, but I believe including the parens causes itm to be passed by value instead of by reference. I could be wrong.
包是一个物体。对象使用 Set 的规则 #1
the bag is an object. Rule #1 for objects use Set
你还需要说
我猜你在添加上有问题。我不知道为什么会这样,但它可以工作
我以简单的方式尝试了整个事情这是我的工作代码
该类非常简单:
希望它有帮助
You need to say
also I guess you have a problem on the add. I don't know why this is but it works on
I tried the whole thing in a simple manner here is my working code
The class is really simple:
Hope it helps
我在收藏时也遇到了类似的问题。
我把它调暗了,但没有用 New 设置它或初始化它。
基本上我
在添加之前添加了以下内容
,然后它就像一个魅力...我还将 Dim 语句从 Sub 移到了模块的顶部,使其成为一个类变量
I had a similar problem with a collection.
I Dim'd it but hadn't set it with New or initialized it.
Basically i had
I added the following before the add
then it worked like a charm...I had also moved the Dim statement from the Sub to the top of the Module to make it a class variable