Excel 用户定义的函数不能在 Excel 2007 中使用 ArgumentDescriptions。我试图在我的添加中绕过它

Excel User-defined functions can't use ArgumentDescriptions in Excel 2007. I'm trying to bypass it in my Add

我在 Excel 中创建了几个用户定义的函数,并创建了一个简单的加载项,以便与同事共享。

我使用 Application.MacroOptions 是为了提供一些有关如何使用这些功能的有用指示。特别是,我使用 ArgumentDescriptions 来描述参数。

Application.MacroOptions _
      Macro:=FuncName, _
      Description:=FuncDesc, _
      Category:=Category, _
      ArgumentDescriptions:=ArgDesc _
      HelpFile:= Helpfile

我刚刚发现 Excel 2007 不支持 ArgumentDescriptions 参数,这导致此版本出错。排除此参数可解决问题。

我不想分发两个版本的加载项。如果版本比 Excel 2007 更新,我尝试使用此代码包含参数,但如果在 Excel 2007 中使用,它仍然会因无法识别的参数名称而被绊倒:

If Left(Application.Version, 2) > 12 Then
     Application.MacroOptions Macro:=FuncName, ArgumentDescriptions:=ArgDesc
End If

有没有办法解决这个问题,这样我就可以只用一个加载项来完成所有这些?

谢谢

解决函数中 Named argument not found 错误的一种简单方法是将宏选项的设置移动到 2 个单独的函数中:

 If Left(Application.Version, 2) > 12 Then
    setUpNewMacroOptions
 Else
    setUpOldMacroOptions
 End If

然后是 2 个函数:

 Public Sub setUpOldMacroOptions()
    Application.MacroOptions Macro:="myMacro", Description:="desc", Category:="cat", HelpFile:="file"
 End Sub

 Public Sub setUpNewMacroOptions()
    Application.MacroOptions Macro:="myMacro", Description:="desc", Category:="cat", ArgumentDescriptions:="blah", HelpFile:="file"
 End Sub

由于在 Excel 2007 中从未调用过 setUpNewMacroOptions 函数,因此 VBA 编译器不会尝试验证该函数,因此没有错误。

此过程在 Excel 2010 年使用参数描述符时有效,而在 2007 年没有参数描述符时有效:

    Private Sub AddDesc(Macro As String, Description As String, _
Category As String, ArgumentDescriptions As Variant)
        #If VBA7 Then 'in Excel 2010 and over
            Application.MacroOptions Macro:=Macro, Description:=Description, _
                Category:=Category, ArgumentDescriptions:=ArgumentDescriptions
        #Else 'in Excel 2007 and under
            Application.MacroOptions Macro:=Macro, Description:=Description, _
                Category:=Category
        #End If
    End Sub

此致, 彼得·克拉索伊