使用目标寻求

时间:2015-11-05 13:28:04

标签: excel vba excel-vba

我正在拆分我写的一个非常旧的电子表格,并尝试使用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

这里列出的一些常量(例如密度,管道大小等)我最终打算从工作表中读取或自动计算,但是现在我一次只进行一步。

既然我已经满意这是有效的,我已经通过输出生成的数字来检查,我想使用Goal Seek来查找某个预定流量的流速值。 / p>

所以我想做的是让VBA循环通过不同的流速值,直到达到所需的压降值。我将告诉VBA Excel表格中单元格中所需的压降。我希望这个计算完全存在于VBA中而没有任何工作表公式。

所以,我用非常简单的术语得到了以下内容:

(1)起始流量(我猜这应该在VBA代码中定义,否则Goal Seek不会有起点)

(2)一些计算

(3)压力下降。

(4)如果产生的压降不等于预定值(位于单元格G3中),则应调整(1)中的流速值并再次运行计算。

(5)当产生的压降等于预定义值时,告诉我用于计算这个值的流量值是什么。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

好吧,我对此采取了一个解决方案。这可能是一个更好的方法,这假设一个直接的关系(而不是反向)..我将一些变量移动到常量中并将压力计算结果放入函数中,并更改数据类型加倍。它是一个可以在工作表中使用的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)

其中&#34; A3&#34;是你理想的压降数