vba函数2参数1返回不起作用

时间:2016-01-06 10:44:48

标签: vba excel-vba excel

我想要一个函数,我可以插入2个整数,然后返回一个整数作为结果

Sub test()
Dim x As Integer
x = getConst 1, 0 ' here is where i get the error message
MsgBox "Vakue" & x
End Sub

Function getConst(ry As Integer, rx As Integer) As Integer
getConst = 3 'actually that one: getConst = 34 * ry * ry - 30 * ry + 8.5 * rx * rx + 7.5 * rx, but that should make any difference
End Function

我习惯于Java编码,它应该有点像这样,但它说“Erwartet:Anweisungsende”所以用英语〜“Expectet end of clause”

2 个答案:

答案 0 :(得分:0)

从函数中检索值需要使用括号

x = getConst(1, 0)

但是当你把它们称为一个程序(没有检索返回值)时,你可以忘记这个假体:

getConst 1, 0

答案 1 :(得分:0)

您遇到的唯一问题是Sub Test()中函数调用周围缺少括号...即

x = getConst(1, 0)

在实际函数中,你应该重新考虑使用整数变量乘以小数。

Sub test()
Dim x As Integer

    x = getConst(1, 0)
    MsgBox "Value " & x

End Sub

Function getConst(ry As Integer, rx As Integer) As Integer
' getConst = 3 'actually that one:
' getConst = 34 * ry * ry - 30 * ry + 8.5 * rx * rx + 7.5 * rx 
' but that should make any difference

    getConst = 34 * ry ^ 2 - 30 * ry + 8.5 * rx ^ 2 + 7.5 * rx

End Function