Thursday 16 April 2020

DO UNTIL LOOP IN MACROS (VBA)


Namaste πŸ™πŸ˜Š My Dear Buddies…

We here at Feel Excel once again welcome you all.

So, today we have a new macro thing for you.


In our last blog we learn about the use of IF ELSE function.
In that example we have to select every grade cell and run the macro each time. (In case you have not refer it, please click here.)

This makes our process a bit long.


What if we get the grades of all the student by running the macro only once?


For this we’ll use the function called Do Until LOOP.


Loops functions are basically used to repeat a particular code or macro until some condition is fulfilled.


For Example, if we want to repeat a code 10 times, we’ll use following loop: -


A = 1                   (Defining value of variable i.e. A as 1)

Do Until A > 10 (Do Until is kind of loop function, Repeat Until A is greater than 10 i.e.10 times)

“VBA CODE”   (Our VBA Code)

A = A+1             (Every time loop runs A’s value will be increased by 1)

Loop                  (For closing Loop Function)



Let’s try Do Until Loop in our example related to Grades of Students covered in last blog.

1)      Following is the example


2)      Go to Developer Tab => Macros => Select Macro => Edit.


3)      You can see now the code related to Grading of student.



4)      We will add following lines: -

Range(“D5”).Select   (This will select the 1st Student Grade cell)

Do Until ActiveCell.Previous.Value = ""    ( "" indicates Blank Value) 

"IF CONDITION CODE" (which we wrote in IF AND Else blog) 

ActiveCell.Offset(Rowoffset +1, Columnoffset).Select (This will select the Cell below Active cell i.e. “D6” in case of 1st student grade) 

Loop (for closing DO Until Loop)



Points from above code :-
a) Range ("D5") will be selected at start as it's the first Student's Grade.
b) The Code will be repeated Until Mark's Cell (i.e. ActiveCell.Previous.Value) is blank, in our case it will repeat till the value of Mark column is blank i.e. Cell("C7").
c) After every time code runs it will select the cell below the active cell by this code -  ActiveCell.Offset(Rowoffset +1, Columnoffset).Select

5)      Let’s Try the code by pressing shortcut key i.e. (Ctrl +Shft +R) which we set for the macro.

6)      We can see the Grades of all the student appears at once.


7)      Let’s add few more names of student along with their marks.


8)   Run the macro.


Bazinga! It’s perfectly working.

So, friends that’s all for this blog. Hope you learned about Do Until Loop function today.

We’ll come soon with another Macro Thing until then,
You can download the worksheet of above example from here.

Namaste πŸ™πŸ˜Š

You can watch the video of above at: -


No comments:

Post a Comment