添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams
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
            End If
    RETURN_Equipment = myCollection
End Function

I keep getting

Compile error:
Argument not optional

I get the error on the last line

RETURN_Equipment = myCollection

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

What gives?

The default 'value' of collection is .Item which requires an index. To assign the reference of the collection object to a variable you need to use the Set keyword at the start of the line. – Cor_Blimey May 9, 2014 at 21:31

I was getting this error because I was using the wrong function name when trying to return a result from a function. I was doing this:

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

This happened because I copied a function from somewhere else and changed the name, but not the return statement. This is not the OP's problem, but I was getting the same error message.

Because you've specified the Optional Parameter as a string it will default to an empty string if you've not specified a value.

This means it can't be missing

If you'd specified it as

Public Function RETURN_Equipment(Optional category) As Collection

It would be a variant and that could be missing, although you'd also be able to mess things up by passing non string variants as the category parameter

The best course of action is probably to replace

If IsMissing(category) Then 
If category = "" Then 

And as Brad has pointed out you'll need to use Set

Set RETURN_Equipment = myCollection

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

I did notice that the IsMissing() wasn't firing, but hadn't looked into it with my assignment error mucking everything up. Thanks for the tip you saved me some grief today. Regards, – Robomato May 12, 2014 at 19:46

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.