将项添加到VBA/VB6集合时出错

hol*_*fix 4 vb6 collections vba ms-office excel-vba

我还在学习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
Run Code Online (Sandbox Code Playgroud)

Vin*_*ent 11

尝试在添加中删除它周围的parens:

bag.Add itm
Run Code Online (Sandbox Code Playgroud)

要么

bag.Add itm, key
Run Code Online (Sandbox Code Playgroud)

已经有一段时间了,因为我不得不使用VBA/VB6,但我相信包括parens导致它通过值而不是通过引用传递.我错了.

  • 一句话:Bizzare!感谢一百万的伴侣.我仍然会遇到错误,但现在还有其他问题. (2认同)