在Excel VBA中使用静态地图时出错

时间:2016-10-03 18:02:26

标签: excel vba excel-vba static

放入GetCallOrPut = map(legOption)行的错误处理程序。传入的值是=" C"。当我单步执行时,地图正在初始化,我似乎无法弄清楚我错过了什么。我有两个其他功能,使用类似的地图,没有麻烦。有什么想法吗?

Public Function GetCallOrPut(ByVal legOption As String) As String
    'Translates C to Call and P to Put in option Structure

    Static map As Collection

    If map Is Nothing Then
        Set map = New Collection
        map.Add "C", "Call"
        map.Add "P", "Put"
    End If

    GetCallOrPut = map(legOption)

End Function  

1 个答案:

答案 0 :(得分:3)

集合中的密钥是第二个参数。

也使用Dim而不是Static:

Public Function GetCallOrPut(ByVal legOption As String) As String
    'Translates C to Call and P to Put in option Structure

    Static map As Collection

    If map Is Nothing Then
        Set map = New Collection
        map.Add "Call", "C"
        map.Add "Put", "P"
    End If


    GetCallOrPut = map(legOption)

End Function