Excel VBA Loops

For … Next Loop | Do … Loop | While … Wend loop

This post will guide you how to use loops statements in MS excel VBA. There are three most useful Loops statements as bellows: For …Next loop, Do … Loop and While …Wend loop.

You can use Loop statement to perform the same statement multiple times in MS VBA program.

If we want to add up the numbers 1 to 10, we can easily to get the results. But if we need to add up the numbers 1 to a 100. You wouldn’t have to type them all out to do addition operation, now we can use the loop.

For … Next Loop example

Sample1: add up the numbers 1 to 100

1# open visual Basic Editor, then insert a module and name as: myForNextLoopDemo1

2# enter into the below VBA codes in code window.

Sub myForNextLoopDemo1()
Dim i As Integer, iSum As Integer
iSum = 0
For i = 1 To 100
    iSum = iSum + i
Next
MsgBox iSum, , "For ...Next Loop"
End Sub

vba For Next Loop example1

3# back to workbook and run the above macro.

vba For Next Loop example1

vba For Next Loop example1

Do … Loop example

Sample2: add up the numbers 1 to 100 using Do Loop

1# open visual Basic Editor, then insert a module and name as: myDoLoopDemo1

2# enter into the below VBA codes in code window.

Sub myDoLoopDemo1()

Dim i As Integer, iSum As Integer

iSum = 0

i = 1

Do Until i > 100

iSum = iSum + i

i = i + 1

Loop

MsgBox iSum, , "Do ...Loop"

End Sub

 

vba Do Loop example1

3# back to workbook and run the above macro.

vba Do Loop example1

vba Do Loop example1

While …Wend loop Example

Sample3: add up the numbers 1 to 100 using While … Wend Loop

1# open visual Basic Editor, then insert a module and name as: myWhileWendLoopDemo1

2# enter into the below VBA codes in code window.

Sub myWhileWendLoopDemo1()

Dim i As Integer, iSum As Integer
iSum = 0
i = 1
While i < 101
    iSum = iSum + i
    i = i + 1
Wend
MsgBox iSum, , "While ...Wend Loop"
End Sub

 

While Wend loop Example1

3# back to workbook and run the above macro.

While Wend loop Example1

While Wend loop Example1

Related Posts

VBA Macro For VLOOKUP From Another Sheet

In the previous post, you should know that how to fix or remove the #N/A error when using VLOOKUP formula to lookup value from another sheet. And this post will show you how to use VBA code to vlookup data ...

How To Remove Special Characters in Excel

This post will show you how to remove special characters from text strings in Excel. And I am going to introduce two ways to remove special characters (@&)#%$) from a string in Excel. Removing Special Characters using Power Query If ...

How To Insert Comments in Protected Worksheet in Excel

This post will show you how to allow comments in a protected worksheet in Excel. You can easily to insert comments into cells in a normal worksheet in Excel, but if want to insert a comment in a worksheet that ...

How To Convert Text to Upper Cases(Using VBA) in Excel

This post will show you how to switch from lower case to upper case in Excel. and I am going to show you two different ways of converting text to upper cases using formula or VBA macro in Excel 2013,Excel ...

How To Hide Every Other Row in Excel (Using VBA)

This post will show you how to hide alternate rows or columns in Excel or how to hide every third, fourth, fifth row or column in Excel. If you want to hide every other row in your current worksheet, how ...

Comments

So empty here ... leave a comment!

Leave a Reply

Sidebar