[英]My Do Until macro doesn't do anything
我正在創建一個迭代循環來解決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
Sub Iterate()
'
' Iterate Macro
'
'
Dim RowStart As Long
Dim HeadLoss As Long
Dim Count As Long
RowStart = 19
HeadLoss = 0
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
For i = 1 To 4
.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
Next i
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
End With
Loop
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.