我正在尝试开发一个宏来计算各种系统中的泵头值。我无法弄清楚如何制定目标寻求。请注意,我是第一次使用VBA用户,这是一个课程作业。代码如下:
Global NameEngr As String
Global Dateinput As String
Global Location As String
Global k As Double
Global g As Double
Global visc As Double
Global f As Double
Global L As Double
Global D As Double
Global Kminor As Double
Global Q As Double
Global Z As Double
Sub PumpHead()
UserForm1.Show
'Calculate the pump head based on desired flow rate.
Range("A1").Select
ActiveCell.Value = "Engineer's Name"
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = NameEngr
ActiveCell.Offset(1, -1).Select
ActiveCell.Value = "Date"
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = Dateinput
ActiveCell.Offset(1, -1).Select
ActiveCell.Value = "Location"
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = Location
Range("A5").Select
v = (Q) / ((3.14159 / 4) * (D ^ 2))
ActiveCell.Value = v
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = "Velocity"
ActiveCell.Offset(1, -1).Select
ActiveCell.Value = "Reynold's Number"
ActiveCell.Offset(0, 1).Select
Re = (v * D) / (visc)
ActiveCell.Value = Re
ActiveCell.Offset(1, -1).Select
ActiveCell.Value = "LHS Colebrook Equation"
ActiveCell.Offset(0, 1).Select
LHSCole = 1 / ((f) ^ (1 / 2))
ActiveCell.Value = LHSCole
ActiveCell.Offset(1, -1).Select
ActiveCell.Value = "RHS Colebrook Equation"
ActiveCell.Offset(0, 1).Select
RHSCole = (-2) * Log(((k / D) / 3.7) + (2.51 / (Re * (f ^ (1 / 2)))))
ActiveCell.Value = RHSCole
ActiveCell.Offset(1, -1).Select
ActiveCell.Value = "Difference"
ActiveCell.Offset(0, 1).Select
Diff = LHSCole - RHSCole
ActiveCell.Value = Diff
ActiveCell.Offset(1, -1).Select
ActiveCell.Value = "Friction Factor"
ActiveCell.Offset(0, 1).Select
**f = (Range("B7:B9").GoalSeek(Goal:=0, ChangingCell:=("B10")))**
ActiveCell.Value = f
ActiveCell.Offset(1, -1).Select
ActiveCell.Value = "Major Head Loss"
ActiveCell.Offset(0, 1).Select
hf = f * (L / D) * ((v ^ 2) / (2 * g))
ActiveCell.Value = hf
ActiveCell.Offset(1, -1).Select
ActiveCell.Value = "Minor Head Loss"
ActiveCell.Offset(0, 1).Select
hm = Kminor * ((v ^ 2) / (2 * g))
ActiveCell.Value = hm
ActiveCell.Offset(1, -1).Select
ActiveCell.Value = "Total Head Loss"
ActiveCell.Offset(0, 1).Select
hl = hm + hf
ActiveCell.Value = hl
ActiveCell.Offset(1, -1).Select
ActiveCell.Value = "Pump Head"
ActiveCell.Offset(0, 1).Select
hp = hl + Z
ActiveCell.Value = hp
End Sub
答案 0 :(得分:0)
目标范围可能只包含一个单元格,更改单元格缺少范围关键字
指令看起来应该是
Range("B7").GoalSeek(Goal:=0, ChangingCell:=Range("B10")))
如果你需要获得B7,B8,B9 = 0,添加一个新单元格,这3个单元格的总和= 0,并在目标搜索命令中调整目标单元格。