对于对具有可选参数的函数的函数的调用,我遇到语法错误。在这些条件下,文档中列出了有关语法错误的各种方案,但我没有看到我理解在这里应用的方案。有人可以帮我理解为什么这个调用语法不好?我在没有Call
语句的情况下尝试了另一种形式但是也没有用。这是以克为单位的tometric调用。
以下是代码:
' Grams -- convert ounces or pounds and ounces to grams.
' This wrapper calls function tometric flagging conversion to grams. If grams
' is called with one value, amt_x represents the number of ounces to convert
' and amt_y is set to zero as a flag. If called with two values, amt_x and
' amt_y represent the number of pounds and ounces to convert. Grams returns
' the number of grams calculated by tometric, which does the calculation.
Public Function grams(amt_x As Double, Optional amt_y As Double = 0) As Double
' ****** The following generates a syntax error
grams = call tometric("g", amt_x, amt_y)
End Function
' tometric -- Convert ounces or pounds and ounces to either grams or kilograms.
' The Unit argument specifies whether to convert to grams ("g") or kilograms
' ("kg"). The amounts to convert are either ounces or pounds and ounces, as
' documented in the grams function.
Public Function tometric(unit As String, amt1 As Double, Optional amt2 As Double = 0)
If unit <> "kg" And unit <> "g" Then
MsgBox "unrecognized conversion unit specified: " & unit
Else
' If there are three arguments, amt1 = pounds and amt2 = ounces, else
' amt1 = ounces
If amt2 = 0 Then
tometric = WorksheetFunction.Convert(amt1, "ozm", unit)
Else
tometric = WorksheetFunction.Convert((amt1 * 16) + amt2, "ozm", unit)
End If
End Function