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

GetCallOrPut = map(legOption)行放入我的error handling程序。 传入的值是=“C”。 当我一步一步地图时,地图正在初始化,我似乎无法弄清楚我错过了什么。 我有两个其他的function,使用类似的地图,并没有在那里的麻烦。 有什么想法吗?

 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 

第二个参数是集合中的关键字。

另外使用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