2015-11-05 32 views
1

我正在拆分我寫的一個非常舊的電子表格,並嘗試使用VBA將它重新組合在一起。到目前爲止,我這一點,這似乎工作:使用目標搜索

Sub PipeData() 

Dim FlowRate As Single 
Dim Density As Single 
Dim DynamicViscosity As Single 
Dim PipeSize As Single 
Dim Pi As Single 
Dim ReynoldsNumber As Single 
Dim Lamda As Single 
Dim EquivalentRoughness As Single 
Dim RelativeRoughness As Single 
Dim Velocity As Single 
Dim PressureDrop As Single 

Density = 977.8 
DynamicViscosity = 0.0004 
PipeSize = 36.1 
Pi = WorksheetFunction.Pi() 
EquivalentRoughness = 0.046 
RelativeRoughness = EquivalentRoughness/PipeSize 

FlowRate = Cells(2, 7) 


ReynoldsNumber = (4 * FlowRate)/(DynamicViscosity * Pi * (PipeSize/1000)) 

If ReynoldsNumber < 2000 Then 
    Lamda = 64/ReynoldsNumber 

Else 
    Lamda = ((1/(-1.8 * WorksheetFunction.Log((6.9/ReynoldsNumber) + ((RelativeRoughness/3.71)^1.11))))^2) 

End If 

Velocity = ((4 * FlowRate)/(Pi * Density * ((PipeSize/1000)^2))) 

PressureDrop = ((Lamda * Density) * (Velocity^2))/(2 * (PipeSize/1000)) 

End Sub 

此處列舉的一些常量(例如密度,管道尺寸等),我終於打算從工作表讀取或自動計算,但現在我一次只進行一步。

現在,我很滿意這個作品,我已經通過輸出生成的數字檢查,我想用單變量求解找到在某個預先定義的流量的流量值。

所以我想要做的是讓VBA循環通過不同的流量值,直到達到所需的壓降值。我會告訴VBA Excel表格中一個單元格中所需的壓降。我希望這個計算完全存在於VBA中,而不需要任何工作表公式。

所以我有,在非常簡單的術語,以下內容:

(1)起動流量(我想這應該在VBA代碼來定義,否則單變量求解不會有一個起點)

(2)一些計算

(3)產生的壓降。

(4)如果產生的壓力降是不等於預先定義的值(位於小區G3)在(1)應調節和計算再次運行的流量值。 (5)當產生的壓降等於預定值時,告訴我用於計算的流量值是多少。

任何想法?

+0

是什麼FLOW_RATE和理想pressure_drop的初始值?即。目標是什麼? – neuralgroove

+0

理想情況下,我不會有flow_rate的起始值。然而,因爲這可能會對goalseek施加不合理的約束,所以我們假設flow_rate的初始值爲1.理想的壓降,即目標爲250。 – OuluChris

回答

0

OK,我參加了一個裂縫在this..there可能是一個更好的辦法,這假設有直接的關係(未倒數)..我有些感動你的變量爲常數,把壓力計算的功能,改變了數據類型以加倍。這是一個可以在工作表中使用的UDF。

Const Density As Double = 977.8 
Const DynamicViscosity As Double = 0.0004 
Const PipeSize As Double = 36.1 
Const Pi As Double = 3.14159265358979 
Const EquivalentRoughness As Double = 0.046 
Const RelativeRoughness As Double = EquivalentRoughness/PipeSize 
Const Sig As Double = 0.0000000001 'this indicates how accurate you want your answer 
Dim FlowRate As Double 
Dim ReynoldsNumber As Double 
Dim Lamda As Double 
Dim Velocity As Double 

Function PipeData(IdealPressureDrop As Long) 
    FlowRate = 1000 + Sig 
    Stepper = 100 
    If PressureDrop(FlowRate) > IdealPressureDrop Then 
     FlowRateGoal = GoalSeek(FlowRate, Stepper, -1, IdealPressureDrop) 
    Else 
     FlowRateGoal = GoalSeek(FlowRate, Stepper, 1, IdealPressureDrop) 
    End If 
    PipeData = FlowRateGoal 
End Function 

Function GoalSeek(FlowRate, Stepper, Direction, IdealPressureDrop) 
calcagain: 
    Select Case Direction 
    Case 1 
     Do While PressureDrop(FlowRate) < IdealPressureDrop 
      oFR = FlowRate 
      FlowRate = FlowRate + Stepper     
     Loop 
    Case -1 
     Do While PressureDrop(FlowRate) > IdealPressureDrop 
      oFR = FlowRate 
      FlowRate = FlowRate - Stepper     
     Loop 
    End Select 
    Stepper = Stepper/10 
    If Stepper < Sig Then GoTo getout 
     FlowRate = oFR 
    GoTo calcagain 
getout: 
    GoalSeek = FlowRate 
End Function 

Function PressureDrop(FlowRate) 
    ReynoldsNumber = (4 * FlowRate)/(DynamicViscosity * Pi * (PipeSize/1000)) 
    If ReynoldsNumber < 2000 Then 
     Lamda = 64/ReynoldsNumber 
    Else 
     Lamda = ((1/(-1.8 * WorksheetFunction.Log((6.9/ReynoldsNumber) + ((RelativeRoughness/3.71)^1.11))))^2) 
    End If 
    Velocity = ((4 * FlowRate)/(Pi * Density * ((PipeSize/1000)^2))) 
    PressureDrop = ((Lamda * Density) * (Velocity^2))/(2 * (PipeSize/1000)) 
End Function 

這現在可以在工作表中引用與

=PipeData(A3) 

其中「A3」是您理想的壓力降數