Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA Function - Argument Not Optional

Tags:

excel

vba

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

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?

like image 983
Robomato Avatar asked May 09 '14 21:05

Robomato


People also ask

How do you fix an argument not optional in VBA?

In this called function, no argument is defined with the “Optional” keyword, so when an argument is not passed in the calling function, we see the error message “Argument not Optional.” In order to resolve this, we need to either pass the parameter in the calling function or mark the rollno argument as optional.

What does argument not optional mean?

The number and types of arguments must match those expected. Either there is an incorrect number of arguments, or an omitted argument is not optional. An argument can only be omitted from a call to a user-defined procedure if it was declared Optional in the procedure definition.

How do you make an argument optional in VBA?

Rules governing the use of optional parameters: The Optional keyword must be present to make a parameter optional. The data type should be (but need not be, see below) a Variant data type. The optional parameter(s) must be at the end of the parameter list.

How do I fix type mismatch error in VBA?

Step 1: Write the subprocedure for VBA Type Mismatch. Step 2: Again assign a new variable, let's say “A” as Byte data type. Let's understand the Byte Data type here. Byte can only store the numerical value from 0 to 255.


1 Answers

Anytime you assign an object you need to use the set keyword.

set RETURN_Equipment = myCollection

like image 100
Brad Avatar answered Sep 28 '22 03:09

Brad