我正在创建一个迭代循环来解决Darcy-Weisbach闭合管道环路。有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
'做,直到水头损失<= 0.0001'不符合条件进入你的循环。改为'尽管...' – 2013-04-09 21:51:06
'做虽然HeadLoss> 0.0001'。另外,你有没有尝试向宏中添加断点以查看它停止的位置?并检查你的所有值是什么? – user2140261 2013-04-09 22:01:31