简体   繁体   中英

writing an excel formula with multiple options for a single parameter

I would like to access information from a HTTP based API and manipulate it with excel.

The API returns about 20 pieces of information, and you can get that information by looking up any number of about ten lookup fields: name, serial number etc.

I want to write a function similar to the Match Function in excel where one of the parameters (in this case MATCH TYPE) has multiple possible values.

I have a list of values (the 20 pieces of information the API can return) and I want to make these pieces of information the possible return values for one of the Functions parameters.

在此处输入图片说明

How do I do I create a function where one parameter has a list of possible values? And how do I add tooltip help statements to those parameter options so people know what they are?

You want to use an Enum .

In the declarations part of your module (before any subs or functions) you can place code like this.

Enum MyFunctionsArgValue
    LessThan
    Equal
    GreaterThan
End Enum

This will assign each of these keywords an integer value, starting at zero and counting up. So LessThan = 0 , Equal = 1 , and GreaterThan = 2 . (You can actually start at any number you want, but the default is usually fine.)

Now you can use it in your function something like this.

Function MySuperCoolFunction(matchType as MyFunctionsArgValue)
    Select Case matchType
        Case LessThan
            ' do something
        Case Equal
            ' do it different 
        Case GreaterThan
            ' do the opposite of LessThan 
    End Select
End Function

To get the tool tip, you need to use something called an Attribute . In order to add it to your code, you'll need to export the *.bas (or *.cls) file and open it in a regular text editor. Once you've added it, you'll need to import it back in. These properties are invisible from inside of the VBA IDE. Documentation is sketchy (read "nonexistent"), so I'm not sure this works for an Enum , but I know it works for functions and module scoped variables.

Function/Sub

Function MySuperCoolFunction(matchType as MyFunctionsArgValue)
Attribute MySuperCoolFunction.VB_Description = "tool tip text"

Module Scoped Var

Public someVar As String
Attribute someVar.VB_VarDescription = "tooltip text"

So, you could try this to see if it works.

Enum MyFunctionsArgValue
Attribute MyFunctionsArgValue.VB_VarDescription = "tool tip text"
    LessThan
    Equal
    GreaterThan
End Enum

Resources

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM