如何在Excel函数中声明变量?

时间:2017-04-09 13:02:57

标签: excel vba

更新帖子

我已将代码更改为。

Option Explicit
Public Function Isokinectic(streamVelocity As Double, sampleVelocity As Double, probeDiameter As Double, density As Double, particleDiameter As Double, viscosity As Double)

        Dim s As Double
        Dim stk_coefficent As Double
        Dim stk As Double
        Dim d As Double

        s = ((0.16 * 10 ^ -4) / particleDiameter) + 1
        stk_coefficent = (1 / (18 * viscosity)) * density * particleDiameter * particleDiameter * s
        stk = (streamVelocity / probeDiameter) * stk_coefficent
        d = 1 + (2 + 0.62 * (sampleVelocity / streamVelocity)) * stk
        Isokinectic = 1 + ((streamVelocity / sampleVelocity) - 1) * (1 - 1 / (d))

End Function

但它不起作用。但是,当添加子MAIN()时,就像这样。它有效。

Option Explicit
Function Isokinectic(streamVelocity As Double, sampleVelocity As Double, probeDiameter As Double, density As Double, particleDiameter As Double, viscosity As Double)

        Dim s As Double
        Dim stk_coefficent As Double
        Dim stk As Double
        Dim d As Double

        s = ((0.16 * 10 ^ -4) / particleDiameter) + 1
        stk_coefficent = (1 / (18 * viscosity)) * density * particleDiameter * particleDiameter * s
        stk = (streamVelocity / probeDiameter) * stk_coefficent
        d = 1 + (2 + 0.62 * (sampleVelocity / streamVelocity)) * stk
        Isokinectic = 1 + ((streamVelocity / sampleVelocity) - 1) * (1 - 1 / (d))

End Function

Sub MAIN()
    MsgBox Isokinectic(0.1, 0.2, 0.3, 0.4, 0.5, 0.6)
End Sub

原帖:

我尝试在excel函数中编写方程式代码。但我得到#value

Function Isokinectic(streamVelocity As Double, sampleVelocity As Double, probeDiameter As Double, density As Double, particleDiamter As Double, visocity As Double)

        Dim s As Double
        Dim stk_coefficent As Double
        Dim stk As Double
        Dim d As Double

        s = (0.16 * 10^ - 4 / particleDiameter) + 1

        stk_coefficent = (1 / (18 * viscosity)) * density * particleDiamter * particleDiameter * s

        stk = (streamVelocity / probeDiameter) * stk_coefficent

        d = 1 + (2 + 0.62 * (sampleVelocity / streamVelocity)) * stk

        Isokinectic = 1 + ((streamVelocity / sampleVelocity) - 1) * (1 - 1 / (1 + d))

        End Function

我在这段代码中做错了什么?

2 个答案:

答案 0 :(得分:0)

稍作清理:

Option Explicit
Function Isokinectic(streamVelocity As Double, sampleVelocity As Double, probeDiameter As Double, density As Double, particleDiameter As Double, viscosity As Double)

        Dim s As Double
        Dim stk_coefficent As Double
        Dim stk As Double
        Dim d As Double

        s = ((0.16 * 10 ^ -4) / particleDiameter) + 1
        stk_coefficent = (1 / (18 * viscosity)) * density * particleDiameter * particleDiameter * s
        stk = (streamVelocity / probeDiameter) * stk_coefficent
        d = 1 + (2 + 0.62 * (sampleVelocity / streamVelocity)) * stk
        Isokinectic = 1 + ((streamVelocity / sampleVelocity) - 1) * (1 - 1 / (1 + d))

End Function

Sub MAIN()
    MsgBox Isokinectic(0.1, 0.2, 0.3, 0.4, 0.5, 0.6)
End Sub

enter image description here

答案 1 :(得分:0)

在您的原始帖子中,您已经计算了s,d,stk,但是在后期您没有获得Velocity&的价值。其他,是价值错误的原因。 由于你已经将Value放在公式中,因此你可以在函数中编写该命令行,你的函数将起作用,不要忘记从单元格传递Velocity的值。