Excel VBA Looping Tutorial
Do while … loop, do until … loop, do … loop while, do … loop until, and while … wend are the format of looping that we can use in Excel VBA.
The main reason why we need to perform looping is to simplified a repeating tasks. Rather then manually calling a block of code over and over again, it’s more simple and readable if we make it into loop.
There are two kind of loop
First, looping as long as the condition is true or false. This type of loop check the value of condition first, before the looping begin to start.
Second, repeat the task until the condition achieved. This type of loop will check the value of condition when the looping is finish.
Which one that we gonna pick is depend on our condition or purpose.
Syntax
Do [{While | Until} condition]
[statements]
[Exit Do]
[statements]
LoopOr
Do
[statements]
[Exit Do]
[statements]
Loop [{While | Until} condition]And
While condition [statements] Wend
While … wend is the same with do while … loop, and do while … loop is more structured and flexible compare to while … wend statement.
Examples:
' example of do while ... loop
Sub ChkFirstWhile()
counter = 0
myNum = 20
' do while myNum > 10
' we perform condition check before we do loop
' if the first check fail to meet the criteria
' no loop will be perform
Do While myNum > 10
myNum = myNum - 1
counter = counter + 1
Loop
MsgBox "The loop made " & counter & " repetitions."
End Sub
' example of do ... loop while
Sub ChkLastWhile()
counter = 0
myNum = 9
' do ... loop while myNum > 10
' we perform condition check after the loop
' we do at least 1 loop
Do
myNum = myNum - 1
counter = counter + 1
Loop While myNum > 10
MsgBox "The loop made " & counter & " repetitions."
End Sub
' example of do until ... loop
' the principle is the same with do while ... loop
Sub ChkFirstUntil()
counter = 0
myNum = 20
Do Until myNum = 10
myNum = myNum - 1
counter = counter + 1
Loop
MsgBox "The loop made " & counter & " repetitions."
End Sub
' example of do ... loop until
' the principle is the same with do ... loop while
Sub ChkLastUntil()
counter = 0
myNum = 1
Do
myNum = myNum + 1
counter = counter + 1
Loop Until myNum = 10
MsgBox "The loop made " & counter & " repetitions."
End Sub
' example of while ... wend loop
Sub ChkWhile()
Dim Counter
Counter = 0 ' Initialize variable.
While Counter < 20 ' Test value of Counter.
Counter = Counter + 1 ' Increment Counter.
Wend ' End While loop when Counter > 19.
Debug.Print Counter ' Prints 20 in the Immediate window.
End SubFIN.
Related Entries
VBA Delete Excel Rows Based on Certain Date
Let say I have a bunch of formatted data in my Excel sheet, and in Column A I have a dates. Then I wanna filter all the data base on certain date, for example 1 Jan 2009, and delete all others data before that date. The algorithm is like this: I’ll create a loop from the...
Check If Excel Workbook is Already Open or Not
In my case, I work a lot with transferring data from current Excel Workbook to another Excel Workbook, and to be able to do that, of course I need to make sure whether the destination Workbook is already open or not. The following excel vba function assigned to check whether a workbook we need is open...
Excel VBA Add Set of Worksheets Automatically
There is a time when we must create a set of Excel Worksheets templates on a regular basis in our work. For example, a set of Excel Worksheet for each month of the year or may be based on the type of work we should done in certain time base. Here a simple example of how...
Sum Unique/Distinct Values in Excel
Usually in the sequential database system, we can SUM only unique values in table column by adding all the values from SELECT DISTINCT query result only. Based on the same principle, we can also create a simple VBA function in Microsoft Excel by adding the values only available in a collection of unique values we have...
Excel Extract Cell Comments
Excel VBA/Macro example below will extract comment from every cells with comment, and put the summary in the cell selected by user using Excel input box (read more about how to get cell reference using input box here). Sub CreateCommentsSummary()Dim rgComments As Range, rgCell As Range, rgOutput As Range, iRow As Integer,...
Valuable Resources
- Microsoft Excel 2003/2007 Video Tutorials
Step-by-step video guide to mastering Charts, PivotTable, Data Analysis and Macro programming in Microsoft Excel in 5 hours.
- 101 Secrets of Microsoft Excel
Discover 101 of Excels little-known secrets that have been hiding right under your nose.
- Top Tips & Tricks to Get You Started in Microsoft Excel 2007
This is an ebook by Carol Bratt's, a Microsoft Certified Professional who has plenty of experience explaining things in simple English.
- Microsoft Office 2003/XP/2007 Training Videos
Discover an easy to use, hands-on interactive course that will teach you how to unleash the true power of Microsoft Office 2003/XP/2007.
If you found this page useful, please consider bookmark it using social media or add a link to this page.
Incoming search engine terms: excel vba loops, excel vba loop, excel macro while loop, excel vba for loop, vba tutorial, excel vba counter, vba macro loop, loop until vba, excel macro loop, for loops in excel, excel 2007 vba while loop, vba looping, vba looping examples, vba loops, excel vba do while