VBA函数参数列表选择 [英] VBA Function argument list select
问题描述
我正在寻找类似以下内容:
I am looking to do something like the following:
Public Function myFunc(vArg1 as string, vArg2 as string, vArg3 as ["A","B","C"])
End Function
当用户在调用vArg3时,用户获取一个下拉列表。这将类似于以下内容:
Where the user gets a drop down list for vArg3 when they call it. This would be similar to the following:
Public Sub Main()
Call StrComp("A", "B", vbTextCompare)
End Sub
其中vbTextCompare可以从预定义的列表中选择或参数的功能。
Where vbTextCompare can be chosen from a pre-defined list or arguments for the function.
谢谢
推荐答案
这是什么被称为枚举。以下是一个简单的例子:
This is what is known as an enumeration. Here is a quick example:
Public Enum DayOfWeek
Monday = 1
Tuesday = 2
Wednesday = 3
Thursday = 4
Friday = 5
Saturday = 6
Sunday = 7
End Enum
Public Function GetDrinkSpecial(day As DayOfWeek) As String
Select Case day
Case DayOfWeek.Monday
GetDrinkSpecial = "$1 Tap Domestics"
Case DayOfWeek.Tuesday
GetDrinkSpecial = "2 for 1 Rail Mixers"
Case DayOfWeek.Wednesday
GetDrinkSpecial = "$2 You-Call-Its"
Case DayOfWeek.Thursday
GetDrinkSpecial = "$1 Bush Bottles"
Case DayOfWeek.Friday
GetDrinkSpecial = "$3 Greenies"
Case DayOfWeek.Saturday
GetDrinkSpecial = "No Specials, Doh!"
Case DayOfWeek.Sunday
GetDrinkSpecial = "No Specials, Doh!"
Case Else
GetDrinkSpecial = "No Specials, Doh!"
End Select
End Function
Public Sub TestIt()
MsgBox GetDrinkSpecial(Monday)
MsgBox GetDrinkSpecial(Tuesday)
MsgBox GetDrinkSpecial(Wednesday)
MsgBox GetDrinkSpecial(Thursday)
MsgBox GetDrinkSpecial(Friday)
MsgBox GetDrinkSpecial(Saturday)
MsgBox GetDrinkSpecial(Sunday)
End Sub
这将在VBA编辑器中调用函数时获得所需的下拉效果。但是,如果要从excel单元格公式中调用GetDrinkSpecial,那么您将无法访问枚举,并且需要具体通过枚举的长期值。
This will get the desired 'Drop Down' Effect you are looking for when calling the function within the VBA editor. However, if you were to call 'GetDrinkSpecial' from within an excel cell formula, you will not have access to the enumeration, and would need to specifically pass it the long value of the enumeration.
这篇关于VBA函数参数列表选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!