简单的Excel VBA if if ifse错误?

我有这个VBA Excel代码的问题:=区域(A2,A3)不会返回任何值,如果A3不存在。

Function Area(Length, Optional Width) If IsMissing(Width) Then Area = Length * Length Else Area = Length * Width End If End Function 

你混淆了IsMissing和IsEmpty。

如果没有任何东西传递给函数,例如:= Area(A1),那么宽度将会丢失。

但是,如果你传递一个参数给函数,其中B1不包含任何内容:= Area(A1,B1),那么Width将是Empty,而不是Missing。

要处理这两种情况:

 If IsMissing(Width) Or IsEmpty(Width) Then Area = Length * Length Else Area = Length * Width End If 

IsMissing不适用于简单的数据types(例如Integer或Double),因为与Variants不同,它们没有“丢失”标志位的规定。 因此,types化可选参数的语法允许您指定一个默认值。 如果在调用该过程时省略参数,则参数将具有此默认值,如下例所示:

也许这可以帮助:

 Function Area(Length, Optional Width as String = "missing") If Width = "missing" Then Area = Length * Length Else Area = Length * Width End If End Function 

来自: http : //office.microsoft.com/en-in/access-help/ismissing-function-HA001228866.aspx