我有两个独立运行的用户定义的Excel函数,但是当我将它们与“可选参数”结合使用时,其中的一部分不再起作用。
我正在测试的功能如下:
函数PageWidth1()
函数PageWidth2(MyArea作为范围)
函数PageWidth3(可选MyArea作为范围)
他们的代码如下:
Function PageWidth1()
Dim r As Range
Application.Volatile
Set r = Application.Caller.Parent.Range(Application.Caller.Parent.PageSetup.PrintArea)
Debug.Print r.Width
PageWidth1 = r.Width
End Function
和
Function PageWidth2(MyArea As Range)
Dim r As Range
Application.Volatile
Set r = Range(MyArea.Address)
Debug.Print r.Width
PageWidth2 = r.Width
End Function
和
Function PageWidth3(Optional MyArea As Range)
Dim r As Range
Application.Volatile
If Not (IsMissing(MyArea)) Then
Set r = Range(MyArea.Address)
Debug.Print r.Width
PageWidth3 = r.Width
Else
Set r = Application.Caller.Parent.Range(Application.Caller.Parent.PageSetup.PrintArea)
Debug.Print r.Width
PageWidth3 = r.Width
End If
End Function
= PageWidth1(),PageWidth2(MyRange)和PageWidth3(MyRange)都会产生有效的结果。
= PageWidth3()产生并出错。
我要去哪里错了?
答案 0 :(得分:0)
该变量是一个请求的范围,需要定义为String而不是Range。