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

发布于 2024-09-12 06:41:44 字数 1370 浏览 2 评论 0原文

我仍在学习 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 技术交流群。

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

发布评论

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

评论(4

电影里的梦 2024-09-19 06:41:44

尝试删除 add: 中 itm 周围的括号

bag.Add itm

,或者

bag.Add itm, key

自从我不得不使用 VBA/VB6 以来已经有一段时间了,但我相信包含括号会导致 itm 按值传递而不是按引用传递。我可能是错的。

Try removing the parens around itm in the add:

bag.Add itm

or

bag.Add itm, key

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.

阳光的暖冬 2024-09-19 06:41:44

包是一个物体。对象使用 Set 的规则 #1

Set getRevColumns = bag

the bag is an object. Rule #1 for objects use Set

Set getRevColumns = bag
哆兒滾 2024-09-19 06:41:44

你还需要说

set getRevColumns = bag

我猜你在添加上有问题。我不知道为什么会这样,但它可以工作

bag.add itm

我以简单的方式尝试了整个事情这是我的工作代码

Sub myroutine()

     Dim bag As Collection
     Dim itm As clsSimple

     Set bag = getTheCollection()

     Set itm = bag.Item(1)
     MsgBox (itm.someObjectValue)

     Set itm = bag.Item(2)
     MsgBox (itm.someObjectValue)


End Sub

Function getTheCollection() As Collection

        Dim bag As Collection
        Dim itm As clsSimple

        Set bag = New Collection

        Set itm = New clsSimple
        itm.someObjectValue = "value 1"
        bag.Add itm

        Set itm = New clsSimple
        itm.someObjectValue = "value 2"
        bag.Add itm

        Set getTheCollection = bag

End Function

该类非常简单:

Public someObjectValue As String

希望它有帮助

You need to say

set getRevColumns = bag

also I guess you have a problem on the add. I don't know why this is but it works on

bag.add itm

I tried the whole thing in a simple manner here is my working code

Sub myroutine()

     Dim bag As Collection
     Dim itm As clsSimple

     Set bag = getTheCollection()

     Set itm = bag.Item(1)
     MsgBox (itm.someObjectValue)

     Set itm = bag.Item(2)
     MsgBox (itm.someObjectValue)


End Sub

Function getTheCollection() As Collection

        Dim bag As Collection
        Dim itm As clsSimple

        Set bag = New Collection

        Set itm = New clsSimple
        itm.someObjectValue = "value 1"
        bag.Add itm

        Set itm = New clsSimple
        itm.someObjectValue = "value 2"
        bag.Add itm

        Set getTheCollection = bag

End Function

The class is really simple:

Public someObjectValue As String

Hope it helps

许仙没带伞 2024-09-19 06:41:44

我在收藏时也遇到了类似的问题。

我把它调暗了,但没有用 New 设置它或初始化它。

基本上我

Dim collection1 As Collection
...
collection1.Add item     'no compile error just empty

在添加之前添加了以下内容

Set collection1 = New Collection
Call collection1.init

,然后它就像一个魅力...我还将 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

Dim collection1 As Collection
...
collection1.Add item     'no compile error just empty

I added the following before the add

Set collection1 = New Collection
Call collection1.init

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

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