2010-07-30 47 views
4

我还在学习VBA和问心无愧我有一个集合对象这么多问题我想不通。错误添加项目到VBA/VB6收藏

我有一个功能,增加了自定义对象(我创建了一个非常简单的类来存储一些数据),做典型的“读取数据,创建对象表示,它粘成收藏”之类的东西。

如果我尝试添加一个“钥匙” bag.add呼叫我得到一个“编译错误的预期:=”消息。

如果我不把它似乎已经工作,然后当我运行它说,这个项目“编译错误。参数不可选”,并强调了“getRevColumns =包”线。

我不能为我的生活出问心无愧是怎么回事!我怀疑我的包初始化有什么问题? 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 
+0

是否有可能您发布类“columnMap”也? – hol 2010-07-30 08:07:16

回答

10

尝试在添加删除ITM周围的括号:

bag.Add itm 

bag.Add itm, key 

这已经有一段时间,因为我已经工作与VBA/VB6,但我相信包括parens导致itm通过价值而不是通过参考。我可能是错的。

+2

一个字:Bizzare! 感谢百万队友。我仍然有错误,但现在是另一回事了。 – holografix 2010-07-30 08:27:49

+0

在StackOverflow的问题'VBA隐藏功能'中也讨论了这种行为。 http://stackoverflow.com/questions/1070863/hidden-features-of-vba#1070942 – marg 2010-07-30 09:51:25

3

包是物体。规则#1的对象使用SET

Set getRevColumns = bag 
+0

是的,你肯定是在那里!奇怪的奇怪的VBA ...哪里的返回关键字?! – holografix 2010-07-30 08:28:24

+1

没有明确的return语句。返回值(或您的情况下的对象)必须与函数的名称相匹配。 – renick 2010-07-30 09:03:51

+0

是的,我抓了20分钟后抓到我的头和额外的10个谷歌搜索! – holografix 2010-08-09 06:58:28

0

你需要说

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 

希望它有帮助

+0

它绝对做伴侣!无法理解为什么我不应该使用()围绕该添加方法......但无论如何只要它工作。 – holografix 2010-08-09 06:59:17

+0

是的是疯了,但正如文森特之前提到的,似乎有一些逻辑背后它是如何通过参数(通过价值或参考)。我搜索了一段时间但找不到任何东西。 VBA VB6是旧的,并不完美。 – hol 2010-08-09 08:44:30

-1

我有一个集合类似的问题。

我Dim'd,但没有新的或初始化其设置。

基本上我有

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

我添加了添加

Set collection1 = New Collection 
Call collection1.init 

在此之前,它的工作就像一个魅力下面......我也搬到Dim语句从子顶端的模块,使其成为一个类变量