我正在創建一個迭代循環來解決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