我正在创建一个迭代循环来解决达西-魏斯巴赫闭管循环。有4个循环。我正在尝试对所有 4 个循环重复迭代,直到水头损失低于某个值(0.0001)。我录制了一个宏,然后尝试修改它以尽可能多地迭代以低于 0.0001。该宏基本上是从以前的迭代和循环中提取值并将它们放入新的迭代中。我对 vba 真的很陌生,我不确定这里有什么问题,但是宏根本不会运行。它没有说有任何错误,它只是不做任何事情。也许有人知道问题是什么?
Sub Iterate()
'
' Iterate Macro
'
'
Dim RowStart As Long
Dim HeadLoss As Long
Dim Count As Long
RowStart = 19
HeadLoss = 0
Count = 2
Do Until HeadLoss <= 0.0001
' Copy Previous Iteration
Range(Cells(RowStart, 1), Cells(RowStart + 32, 7)).Select
Selection.Copy
Range(Cells(RowStart + 34, 1)).Select
ActiveSheet.Paste
Range(Cells(RowStart + 34, 1)).Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "Iteration" & Count
' Loop 1
Range(Cells(RowStart + 37, 2)).Select
ActiveCell.FormulaR1C1 = "=R[-34]C[5]"
Range(Cells(RowStart + 38, 2)).Select
ActiveCell.FormulaR1C1 = "=-R[-18]C[5]"
Range(Cells(RowStart + 39, 2)).Select
ActiveCell.FormulaR1C1 = "=R[-34]C[5]"
Range(Cells(RowStart + 40, 2)).Select
ActiveCell.FormulaR1C1 = "=-R[-29]C[5]"
Range(Cells(RowStart + 37, 7)).Select
ActiveCell.FormulaR1C1 = "=RC[-5]-R61C5"
Range(Cells(RowStart + 37, 7)).Select
Selection.AutoFill Destination:=Range(Cells(RowStart + 37, 7),
Cells(RowStart + 40, 7)), Type:=xlFillDefault
' Loop 2
Range(Cells(RowStart + 45, 2)).Select
ActiveCell.FormulaR1C1 = "=-R[-5]C[5]"
Range(Cells(RowStart + 46, 2)).Select
ActiveCell.FormulaR1C1 = "=R[-34]C[5]"
Range(Cells(RowStart + 47, 2)).Select
ActiveCell.FormulaR1C1 = "=R[-34]C[5]"
Range(Cells(RowStart + 48, 2)).Select
ActiveCell.FormulaR1C1 = "=-R[-18]C[5]"
Range(Cells(RowStart + 45, 7)).Select
ActiveCell.FormulaR1C1 = "=RC[-5]-R69C5"
Range(Cells(RowStart + 45, 7)).Select
Selection.AutoFill Destination:=Range(Cells(RowStart + 45, 7),
Cells(RowStart + 48, 7)), Type:=xlFillDefault
' Loop 3
Range(Cells(RowStart + 53, 2)).Select
ActiveCell.FormulaR1C1 = "=R[-34]C[5]"
Range(Cells(RowStart + 54, 2)).Select
ActiveCell.FormulaR1C1 = "=-R[-50]C[5]"
Range(Cells(RowStart + 55, 2)).Select
ActiveCell.FormulaR1C1 = "=-R[-16]C[5]"
Range(Cells(RowStart + 56, 2)).Select
ActiveCell.FormulaR1C1 = "=-R[-29]C[5]"
Range(Cells(RowStart + 53, 7)).Select
ActiveCell.FormulaR1C1 = "=RC[-5]-R77C5"
Range(Cells(RowStart + 53, 7)).Select
Selection.AutoFill Destination:=Range(Cells(RowStart + 53, 7),
Cells(RowStart + 56, 7)), Type:=xlFillDefault
' Loop 4
Range(Cells(RowStart + 61, 2)).Select
ActiveCell.FormulaR1C1 = "=-R[-5]C[5]"
Range(Cells(RowStart + 62, 2)).Select
ActiveCell.FormulaR1C1 = "=R[-34]C[5]"
Range(Cells(RowStart + 63, 2)).Select
ActiveCell.FormulaR1C1 = "=R[-34]C[5]"
Range(Cells(RowStart + 64, 2)).Select
ActiveCell.FormulaR1C1 = "=-R[-16]C[5]"
Range(Cells(RowStart + 61, 7)).Select
ActiveCell.FormulaR1C1 = "=RC[-5]-R84C5"
Range(Cells(RowStart + 61, 7)).Select
Selection.AutoFill Destination:=Range(Cells(RowStart + 61, 7),
Cells(RowStart + 64, 7)), Type:=xlFillDefault
HeadLoss = Cells(RowStart + 41, 5).Value + Cells(RowStart + 49, 5).Value +
Cells(RowStart + 57, 5).Value + Cells(RowStart + 65, 5).Value
RowStart = RowStart + 34
Count = Count + 1
Loop
End Sub
谢谢你们的帮助,我肯定接近了。不过,我还有一个问题。我希望宏循环直到 HeadLoss 小于 0.0001。目前迭代只会运行一次,HeadLoss 约为 0.2058,明显大于 0.0001。为什么它只在一次迭代后停止?
Sub Iterate()
'
' Iterate Macro
'
'
Dim RowStart As Long
Dim HeadLoss As Long
Dim Count As Long
RowStart = 19
HeadLoss = 1
Count = 2
Do While HeadLoss >= 0.0001
' Copy Previous Iteration
With ActiveSheet
Range(.Cells(RowStart, 1), .Cells((RowStart + 32), 7)).Copy
.Cells(RowStart + 34, 1)
.Cells(RowStart + 34, 1).Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "Iteration" & Count
' Loop 1
.Cells(RowStart + 37, 2).FormulaR1C1 = "=R[-34]C[5]"
.Cells(RowStart + 38, 2).FormulaR1C1 = "=-R[-18]C[5]"
.Cells(RowStart + 39, 2).FormulaR1C1 = "=R[-34]C[5]"
.Cells(RowStart + 40, 2).FormulaR1C1 = "=-R[-29]C[5]"
.Cells(RowStart + 37, 7).FormulaR1C1 = "=RC[-5]-R61C5"
.Cells(RowStart + 37, 7).AutoFill Destination:=Range(Cells(RowStart + 37, 7),
Cells(RowStart + 40, 7)), Type:=xlFillDefault
' Loop 2
.Cells(RowStart + 45, 2).FormulaR1C1 = "=-R[-5]C[5]"
.Cells(RowStart + 46, 2).FormulaR1C1 = "=R[-34]C[5]"
.Cells(RowStart + 47, 2).FormulaR1C1 = "=R[-34]C[5]"
.Cells(RowStart + 48, 2).FormulaR1C1 = "=-R[-18]C[5]"
.Cells(RowStart + 45, 7).FormulaR1C1 = "=RC[-5]-R69C5"
.Cells(RowStart + 45, 7).AutoFill Destination:=Range(Cells(RowStart + 45, 7),
Cells(RowStart + 48, 7)), Type:=xlFillDefault
' Loop 3
.Cells(RowStart + 53, 2).FormulaR1C1 = "=R[-34]C[5]"
.Cells(RowStart + 54, 2).FormulaR1C1 = "=-R[-50]C[5]"
.Cells(RowStart + 55, 2).FormulaR1C1 = "=-R[-16]C[5]"
.Cells(RowStart + 56, 2).FormulaR1C1 = "=-R[-29]C[5]"
.Cells(RowStart + 53, 7).FormulaR1C1 = "=RC[-5]-R77C5"
.Cells(RowStart + 53, 7).AutoFill Destination:=Range(Cells(RowStart + 53, 7),
Cells(RowStart + 56, 7)), Type:=xlFillDefault
' Loop 4
.Cells(RowStart + 61, 2).FormulaR1C1 = "=-R[-5]C[5]"
.Cells(RowStart + 62, 2).FormulaR1C1 = "=R[-34]C[5]"
.Cells(RowStart + 63, 2).FormulaR1C1 = "=R[-34]C[5]"
.Cells(RowStart + 64, 2).FormulaR1C1 = "=-R[-16]C[5]"
.Cells(RowStart + 61, 7).FormulaR1C1 = "=RC[-5]-R84C5"
.Cells(RowStart + 61, 7).AutoFill Destination:=Range(Cells(RowStart + 61, 7),
Cells(RowStart + 64, 7)), Type:=xlFillDefault
' Check to see if another iteration is needed
HeadLoss = Abs(Cells(RowStart + 41, 5).Value) +
Abs(Cells(RowStart + 49, 5).Value) +
Abs(Cells(RowStart + 57, 5).Value)+
Abs(Cells(RowStart + 65, 5).Value)
RowStart = RowStart + 34
Count = Count + 1
End With
Loop
End Sub