Hello,
How do I put a loop execution limit. I'm using Do ... Loop Until.
Explaining better: I have the condition of my loop, but in some cases the condition is not met, and it will never be ... but the macro keeps running. What a way to stop this macro after a number of executed loops, for example, stop the code after 20 loops performed.
I hope I have explained well ... my English is not the best.
Thanks.
How do I put a loop execution limit
-
- Posts: 3
- Joined: Wed May 29, 2019 11:00 pm
How do I put a loop execution limit
OpenOffice 3.1 on Windows 10
Re: How do I put a loop execution limit
Please upload your macro code here (in an ODF type sample file).
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
Re: How do I put a loop execution limit
Can you test outside the code for a condition that will not complete, then if a completing condition, execute the code?
Edit: Or force a condition to complete by changing the boundary condition test from (for example) exact equality to a broader test such as greater than or equal to? |
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
-
- Posts: 3
- Joined: Wed May 29, 2019 11:00 pm
Re: How do I put a loop execution limit
I hope you understand. What I want to change is with an English comment.
Code: Select all
'MODULO 03'
Sub EQUILIBRA
Rem Salvar Documento:
Dim Document As Object
Dim Dispatcher As Object
Document = ThisComponent.CurrentController.Frame
Dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
Dispatcher.executeDispatch(document, ".uno:Save", "", 0, Array())
Rem Executar a macro na folha ativa:
Dim oDoc
Dim oSheet
Dim oCell
oDoc = ThisComponent
oSheet = oDoc.getcurrentcontroller.activesheet
Dim NomePROJETO As string
oCell = oSheet.getCellByPosition(1, 14)
NomePROJETO = oCell.getString("B15")
Rem Substituir a primeira Vazão:
Dim NumANEIS As Integer
Dim nTRECHOS As Integer
Dim SomaTRECHOS As Integer
Dim i As Integer
Dim j As Integer
Dim yInicial As Integer
Dim yfinal As Integer
Dim yQn As Integer
Dim Qn As Double
oCell = oSheet.getCellByPosition(1, 20)
NumANEIS = oCell.Value(1, 20)
'For i = 1 To NumANEIS Step 1
'oCell = oSheet.getCellByPosition(2, 27 + i)
'nTRECHOS = oCell.Value(2, 27 + i)
'oCell = oSheet.getCellByPosition(3, 26 + i)
'SomaTRECHOS = oCell.Value(3, 26 + i)
'yInicial = 21 + SomaTRECHOS + (10*(i-1))
'yfinal = yInicial + nTRECHOS - 1
'For j = yInicial To yfinal Step 1
'oCell = oSheet.getCellByPosition(20, j)
'Qn = oCell.Value(20, j)
'oCell = oSheet.getCellByPosition(8, j)
'oCell.setValue(Qn)
'Next j
'Next i
Dim DeltaQ0 As Single
'For i = 1 To NumANEIS Step 1
'oCell = oSheet.getCellByPosition(2, 27 + i)
'nTRECHOS = oCell.Value(2, 27 + i)
'oCell = oSheet.getCellByPosition(3, 26 + i)
'SomaTRECHOS = oCell.Value(3, 26 + i)
'yInicial = 20 + SomaTRECHOS + (10*(i-1))
'yfinal = yInicial + nTRECHOS
'YDeltaQ0 = 22 + SomaTRECHOS + (10*(i-1)) + nTRECHOS
'For j = yInicial To yfinal Step 1
'oCell = oSheet.getCellByPosition(12, YDeltaQ0)
'DeltaQ0 = oCell.Value(12, YDeltaQ0)
'oCell = oSheet.getCellByPosition(14, j)
'oCell.setValue(DeltaQ0)
'Next j
'Next i
Rem Confere se a rede está equilibrada:
Dim ErroMIN As Single
Dim hSOMA As Single
Dim Y_hSOMA As Integer
Dim n As Integer
Dim v As Integer
'Verifica NÃO EQUILIBRIO da rede:
For i = 1 To NumANEIS Step 1
Do
For j = 1 To NumANEIS Step 1
oCell = oSheet.getCellByPosition(2, 27 + j)
nTRECHOS = oCell.Value(2, 27 + j)
oCell = oSheet.getCellByPosition(3, 26 + j)
SomaTRECHOS = oCell.Value(3, 26 + j)
yInicial = 21 + SomaTRECHOS + (10*(j - 1))
yfinal = yInicial + nTRECHOS - 1
YDeltaQ0 = 22 + SomaTRECHOS + (10*(j-1)) + nTRECHOS
For n = yInicial To yfinal Step 1
oCell = oSheet.getCellByPosition(20, n)
Qn = oCell.Value(20, n)
oCell = oSheet.getCellByPosition(8, n)
oCell.setValue(Qn)
Next n
For v = yInicial To yfinal Step 1
oCell = oSheet.getCellByPosition(12, YDeltaQ0)
DeltaQ0 = oCell.Value(12, YDeltaQ0)
oCell = oSheet.getCellByPosition(14, v)
oCell.setValue(DeltaQ0)
Next v
Next j
oCell = oSheet.getCellByPosition(3, 26 + i)
SomaTRECHOS = oCell.Value(3, 26 + i)
oCell = oSheet.getCellByPosition(2, 27 + i)
nTRECHOS = oCell.Value(2, 27 + i)
Y_hSOMA = 21 + SomaTRECHOS + (10*(i-1)) + nTRECHOS
oCell = oSheet.getCellByPosition(12, Y_hSOMA)
hSOMA = oCell.Value(12, Y_hSOMA)
oCell = oSheet().getCellByPosition(7, 12) 'H13
ErroMIN = oCell.Value(7, 12)
If hSOMA > ErroMIN Then
hSOMA = 1
ElseIf hSOMA < -ErroMIN Then
hSOMA = 1
ElseIf hSOMA < ErroMIN Then
hSOMA = 0
ElseIf hSOMA > -ErroMIN Then
hSOMA = 0
End If
Loop Until hSOMA = 0 'I want this loop for when it runs a number that I determine for maximum loops
Next i
'Verifica EQUILIBRIO da rede:
For i = 1 To NumANEIS Step 1
oCell = oSheet.getCellByPosition(3, 26 + i)
SomaTRECHOS = oCell.Value(3, 26 + i)
oCell = oSheet.getCellByPosition(2, 27 + i)
nTRECHOS = oCell.Value(2, 27 + i)
Y_hSOMA = 21 + SomaTRECHOS + (10*(i-1)) + nTRECHOS
oCell = oSheet.getCellByPosition(12, Y_hSOMA)
hSOMA = oCell.Value(12, Y_hSOMA)
oCell = oSheet().getCellByPosition(7, 12) 'H13
ErroMIN = oCell.Value(7, 12)
If hSOMA > ErroMIN Then
hSOMA = 1
ElseIf hSOMA < -ErroMIN Then
hSOMA = 1
ElseIf hSOMA < ErroMIN Then
hSOMA = 0
ElseIf hSOMA > -ErroMIN Then
hSOMA = 0
End If
If hSOMA = 0 Then
oCell = oSheet.getCellByPosition(1, 23)
oCell.setString("EQUILIBRADA")
End If
Next i
MsgBox ("A rede está EQUILIBRADA", 64, "HC LIBRE")
End sub
Zizi64 wrote:Please upload your macro code here (in an ODF type sample file).
OpenOffice 3.1 on Windows 10
Re: How do I put a loop execution limit
Example:
Code: Select all
Sub fpCos()
i=0
v = 1 : v_old = 0
Do until (v=v_old) OR (i>=100)
v_old = v
v = Cos(v_old)
i = i + 1
Loop
If v=v_old Then
Print "Regarding the available precison the fixed-point v=" & v & " was reached after " & i & " steps."
Else
Print "After the maximum of 100 steps: v_old=" & v_old & " and v=" & v & " was reached."
End If
End Sub
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
---
Lupp from München