VBA function - argument is optional

Public Function RETURN_Equipment(Optional category As String) As Collection
    Dim config As classConfiguration
    Set config = New classConfiguration

    Dim item As classItem
    Set item = New classItem

    Dim myCollection As Collection
    Set myCollection = New Collection

    For Each config In Configurations
        For Each item In config.colItems
            If IsMissing(category) Then   
                myCollection.add item
            ElseIf InStr(category, "mainframe") <> 0 And item.category = "mainframe" Then
                myCollection.add item
                MsgBox "Fired!"                
            ElseIf category = "accessory" And item.category = "accessory" Then
            Else
            End If
        Next
    Next

    RETURN_Equipment = myCollection
End Function

I keep getting

Compilation error:
Argument not optional

I get an error in the last line

RETURN_Equipment = myCollection

I understand the error message telling me that I did not fill in the parameter. But I have only one parameter, and I declared it optional. It looks like the code thinks I'm trying to call a function from a function?

What gives?

+4
source share
3 answers

Anytime you assign an object, you need to use a keyword set.

set RETURN_Equipment = myCollection

+14
source

I was getting this error because when I tried to return the result from a function, I used the wrong function name. I did it:

Function MyFuncA(arg as String)
    MyFuncB = arg 'The problem is I'm using MyFuncB instead of MyFuncA
End Function

, , return. OP, .

+4

, , .

,

Public Function RETURN_Equipment(Optional category) As Collection

, , ,

, ,

If IsMissing(category) Then 

If category = "" Then 

, Set

Set RETURN_Equipment = myCollection

http://msdn.microsoft.com/en-us/library/office/gg251721%28v=office.15%29.aspx

0

Source: https://habr.com/ru/post/1539977/


All Articles