for loop macro stops when certain conditions are met

Warflion

New Member
Joined
Aug 11, 2017
Messages
10
So I am trying to achieve a for loop macro that stops when certain conditions are met.

Say A1 is the counter that starts at 0. for every tick you get 20% of A1 +1 and 80% of A1 -1, and A1 is always >=0. The macro stops when A1 reaches say 5.

How do I achieve this?

Thanks in advance.
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Welcome to the Board!

There are various ways to structure the loop. One way would be something like this:

Code:
Do
'   Check to exit loop
    If A1 >= 5 Then Exit Do
'   Other code in loop below
    ...
Loop
 
Upvote 0
Thank very much. I will give it a try and come back if it didnt work.
:)

Welcome to the Board!

There are various ways to structure the loop. One way would be something like this:

Code:
Do
'   Check to exit loop
    If A1 >= 5 Then Exit Do
'   Other code in loop below
    ...
Loop
 
Upvote 0
You are welcome!:)

Oh, one more thing.

I know that for loop needs something like for i = 1 to XXXX. If im only referring to one cell that changes value itself, what should i assign to the xxx so it will move on to the next i?
 
Upvote 0
In a For Loop, the number will automatically increment by 1 unless you tell it otherwise.

If you do not have a defined end (XXXX), then you can use a Do loop like this:
Code:
Do
'    Stuff here
     ....
'    Incrementer
     i=i+1
Loop
You just need to be sure to still include the condition to exit the loop (like shown in my previous post).
 
Last edited:
Upvote 0
In a For Loop, the number will automatically increment by 1 unless you tell it otherwise.

If you do not have a defined end (XXXX), then you can use a Do loop like this:
Code:
Do
'    Stuff here
     ....
'    Incrementer
     i=i+1
Loop
You just need to be sure to still include the condition to exit the loop (like shown in my previous post).

Hi, so i tried these codes and got compile error message saying "Next without for". Could you please review the code and tell me what is wrong?
Q2. I'd like to add a counter at B1 for how many loops it ran to achieve, in this case, A1 >= 5, how to do it?
Q3. After one loop is done, I want it to reset everything (set A1 back to 0), run the loop again, and record the new counter on B2, until B1 to B100 are all filled. Should I do a double loop?

Following is the code i currently have. Thank you in advance.

#A1 = 0, B1 is a counter, C1 = ROUND(RAND(),2)
Sub Test() Do
If a1 >= 5 Then Exit Do
i = i + 1
For i = 1 To i
If Range(a1).Value <= 0 Then
Range(a1).Value = 0
Else
Range(a1).Value = Range(a1).Value
If Range(C1).Value >= 0.8 Then
Range(a1).Value = Range(a1).Value + 1
Else
Range(a1).Value = Range(a1).Value - 1
End If
Next i

End Sub
 
Upvote 0
#A1 = 0, B1 is a counter, C1 = ROUND(RAND(),2)
Sub Test()
Do

If a1 >= 5 Then Exit Do
i = i + 1
For i = 1 To i
If Range(a1).Value <= 0 Then
Range(a1).Value = 0
Else
Range(a1).Value = Range(a1).Value
If Range(C1).Value >= 0.8 Then
Range(a1).Value = Range(a1).Value + 1
Else
Range(a1).Value = Range(a1).Value - 1
End If
Next i

End Sub
 
Upvote 0
Hi, so i tried these codes and got compile error message saying "Next without for". Could you please review the code and tell me what is wrong?
Q2. I'd like to add a counter at B1 for how many loops it ran to achieve, in this case, A1 >= 5, how to do it?
Q3. After one loop is done, I want it to reset everything (set A1 back to 0), run the loop again, and record the new counter on B2, until B1 to B100 are all filled. Should I do a double loop?

Following is the code i currently have. Thank you in advance.

#A1 = 0, B1 is a counter, C1 = ROUND(RAND(),2)
Sub Test() Do
If a1 >= 5 Then Exit Do
i = i + 1
For i = 1 To i
If Range(a1).Value <= 0 Then
Range(a1).Value = 0
Else
Range(a1).Value = Range(a1).Value
If Range(C1).Value >= 0.8 Then
Range(a1).Value = Range(a1).Value + 1
Else
Range(a1).Value = Range(a1).Value - 1
End If
Next i

End Sub

Got it up running. I forgot "" thats why. Now i just need help with Q2 and Q3. ;)
 
Upvote 0
Got it up running. I forgot "" thats why. Now i just need help with Q2 and Q3. ;)
I think I solved it. However there is something wrong I couldn't figure out...The code below it only record counter on B1, B3, B5...etc. In theory, it should record counters from B1 to B100. What was wrong?

Thank you in advance and sorry about the mass replies...

Sub Test2()
For y = 1 To 100

Do
If Range("A1").Value = 2 Then Exit Do

For i = 1 To i
If Range("A1").Value = -1 Then
Range("A1").Value = 0
End If

If Range("C1").Value >= 0.8 Then
Range("A1").Value = Range("A1").Value + 1
Else
Range("A1").Value = Range("A1").Value - 1
End If
Range("B" & y).Value = Range("B" & y).Value + 1
i = i + 1
Next i
Loop
y = y + 1
Range("A1").Value = 0
Next y



End Sub
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top