使用目标寻求

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

这里列出的一些常量(例如密度,pipe道尺寸等)我最终打算从工作表中读取或者自动计算,但是现在我正在一步一步地进行。

现在,我已经满意了,我已经通过输出生成的数字检查了这一点,我希望使用“目标寻找”以某个预定义的stream量查找stream量值。

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

所以我用非常简单的话来说就是:

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

(2)一些计算

(3)由此产生的压降。

(4)如果由此产生的压降不等于预定义值(位于单元格G3中),则应调整(1)中的stream量值并重新计算。

(5)当产生的压降等于预定值时,告诉我用来计算的stream量值是多less。

有任何想法吗?

好的,我在这里采取了一个裂缝..有可能是一个更好的方法,这假设了一个直接的关系(而不是相反)..我把你的一些variables变成常量,并把压力计算function,并将数据types更改为双。 这是一个可以在工作表中使用的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”是您理想的压降数