2010-07-30 52 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語句從子頂端的模塊,使其成爲一個類變量