views:

113

answers:

3

Hi guys,

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
+3  A: 

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

Set getRevColumns = bag
renick
Yep you were definitely right there! Weird weird VBA... Where's the return keyword?!
holografix
There is no explicit return statement. The return value(or object as in your case) has to match the name of the function.
renick
Yep I got that after 20 minutes of scratching my head and an additional 10 of googling!
holografix
+3  A: 

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.

Vincent
One word: Bizzare!Thanks a million mate. I'm still getting errors but now it's something else.
holografix
This behavior was also discussed in the Question `Hidden features of VBA` on StackOverflow. http://stackoverflow.com/questions/1070863/hidden-features-of-vba#1070942
marg
A: 

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

Jürgen Hollfelder
It definitely does mate! Can't understand why I'm not supposed to use () around that add method... but anyway as long as it works.
holografix
Yes is crazy but as Vincent mentioned before there seems to be some logic behind it how the parameter is passed (by value or by reference). I googled it for a while but couldn't find anything. VBA is somehow old VB6 and wasn't perfect.
Jürgen Hollfelder