2017-04-09 103 views
0

更新後的帖子如何在Excel函數中聲明變量?

我改變了代碼。

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 

但它不起作用。但是,添加sub 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函數方程。但我得到了#值

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 

任何我在這段代碼中做錯了嗎?

+0

它對我來說運行良好。你怎麼打電話?> –

+0

插入功能。然後輸入變量。 – JOHN

+0

錯字標題*** particleDiamter ***不是*** particleDiameter ***可能還有其他問題 –

回答

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

+0

非常感謝!有用! 但是,當我刪除sub MAIN()部分。並輸入= Isokinectic(),它不起作用。這是爲什麼? – JOHN

+0

它是#value。 LOL – JOHN

+1

它現在有效。看來我需要在VBA項目中插入模塊。不是表格。 – JOHN

0

在你原來的職位你已經計算出S,d,STK但在你得到的速度&其他值後一階段沒有在那裏,是Value錯誤的原因。 由於使用了子程序,因爲您已經將公式中的Value設置爲正常工作,請將函數中的該命令行寫入您的函數,否則請勿忘記從單元格傳遞Velocity的值。