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]

Loop

Or

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 Sub

FIN.

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

If you found this page useful, please consider bookmark it using social media or add a link to this page.

  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • connotea
  • Diigo
  • email
  • Fark
  • Identi.ca
  • Live
  • MisterWong
  • MySpace
  • Netvibes
  • NewsVine
  • PDF
  • Ping.fm
  • Propeller
  • Reddit
  • Simpy
  • StumbleUpon
  • Technorati
  • Twitter
  • Wikio
  • Yahoo! Bookmarks