Exiting a do until loop and moving to next cell

Chrisciuffo

New Member
Joined
Jul 21, 2015
Messages
13
-I have 3 columns: todays price, yesterdays price, and the difference
-I need the variation to be between 0 and .20 (in absolute value)
-I'm trying to do multiple iterations on todays price, multiplying it by .999 or 1.001 (depending if its positive or negative), to bring the difference between 0 and .20 in absolute value
-After the value is between 0 and .20, I want to move onto the next cell and do the same alteration to todays price

So far I have used a nested if loop which has worked, but I feel there's a more efficient way of doing it with a do until loop. It would be a great learning experience if someone could help me figure out this code. Below is the code I have now:

For j = 1 To 100 'doing it 100 times to ensure the value is met (between .20 and 0)
For i = 2 To rowcount
If Cells(i, 6) >= 0.19 Then 'And Cells(i, 6) >= 0 Then
Cells(i, 5) = Cells(i, 5) * 0.999 'lower price a little bit
ElseIf Cells(i, 6) <= -0.19 And Cells(i, 6) <= 0 Then
Cells(i, 5) = Cells(i, 5) * 1.001 'raise price a little bit
End If
Next i
Next j

Thanks in advance
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Don't know that a Do While loop would offer any advantage. A loop is a loop, no matter how it is initiated. See if this mod will speed it up a little.
Code:
For j = 1 To 100 'doing it 100 times to ensure the value is met (between .20 and 0)
    For i = 2 To rowcount
         If Cells(i, 6) >= 0.19 Then 'And Cells(i, 6) >= 0 Then
             Cells(i, 5) = Cells(i, 5) * 0.999 'lower price a little bit
	     If Cells(i, 6).Value < Abs(.20) Then Exit For
         ElseIf Cells(i, 6) <= -0.19 And Cells(i, 6) <= 0 Then
             Cells(i, 5) = Cells(i, 5) * 1.001 'raise price a little bit
	     If Cells(i, 6).Value < Abs(.20) Then Exit For
         End If
     Next i
 Next j
 
Upvote 0
Thank you JLGWhiz

I more so wanted to learn how to exit a loop. I am new to VBA and am starting to use it VERY heavily at work, which is presenting quite a challenge.
Your method has seemed to have sped up the loop a bit - thanks for helping me learn
 
Upvote 0
Thank you JLGWhiz

I more so wanted to learn how to exit a loop. I am new to VBA and am starting to use it VERY heavily at work, which is presenting quite a challenge.
Your method has seemed to have sped up the loop a bit - thanks for helping me learn
No problem,
Regards, JLG
 
Upvote 0

Forum statistics

Threads
1,215,616
Messages
6,125,862
Members
449,266
Latest member
davinroach

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