Need Help With Loop Until in VBA

palaceofreverie

New Member
Joined
Nov 4, 2013
Messages
7
Hey guys,

I've been starting to use VBA recently so its still pretty new to me
Although, i do know you can use it to add to a number in specific integers until a value is reached, I'm not completely sure how to write this.

for example

A1=1 and B1=5

If A1 is < B1 then A1 + .5 until A1 >= B1

Anyone have any suggestions,
whatever help provided would be much appreciated.
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
I guess your example is just for learning the structure.

Code:
Sub LoopUntil()
Do While Range("a1") < Range("b1")
    Range("a1") = Range("a1") + 0.5
Loop


End Sub


Best regards
Per Erik
 
Upvote 0
Thanks for your response!

Does there happen to be a way to do this automatically?
I've entered the code you provided and it doesn't seem to do anything with the numbers in the cells.

If there is a way to do this automatically, is there also a way to apply this to a command button?
 
Upvote 0
Well

You will have to run the code first.
Either by clicking Alt+F8 or assign it to a button.

To assign it to a button go to the developer tab and insert the first button (Under Form Control) and select the macro LoopUntil.


Per Erik
 
Upvote 0
Thank you very much, this worked perfectly!

Might I add that the vba code came out looking like this:

Sub LoopUntil()
Do While Range("a1") < Range("b1")
Range("a1") = Range("a1") + 0.5
Loop


End Sub


Private Sub CommandButton1_Click()
LoopUntil
End Sub

Thank you once again
 
Upvote 0

Forum statistics

Threads
1,214,990
Messages
6,122,626
Members
449,093
Latest member
catterz66

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