Excel VBA Loops Statements in Office 365 ~ With Examples!!
Tags: excelvbaloopsstateme, vbamacros
Published: 2020-12-22 07:19:16

---
title: Excel VBA Loops Statements in Office 365 ~ With Examples!!
published: true
date: 2020-12-21 13:12:34 UTC
tags: ExcelVBALoopsStateme,VBAMacros
canonical_url: https://geekexcel.com/excel-vba-loops-statements-in-office-365-with-examples/
---

Generally, loop statements will execute one or more statements repeatedly until a specific condition is satisfied. For instance, if you want to generate numbers from 1 to 1000, you need to give the statement as increasing the value by 1 for each iteration. In this article, we will see the three most useful **Excel [VBA](https://geekexcel.com/excel-office-365-vba-overview-definition-and-examples/) Loops Statements in Office 365** with clear-cut examples. Let’s get into this article!!

## For … Next Loop:

Here, we are going to **add the numbers from 1 to 50** with the help of For …Next Loop statement. Follow the below steps.

- You need to [**open** the **Visual Basic Editor**](https://geekexcel.com/shortcut-to-open-vba-editor-in-microsoft-excel-365/) by clicking the shortcut [**ALT** + **F11**](https://geekexcel.com/shortcut-to-open-vba-editor-in-microsoft-excel-365/) Keys.
- Then, on the **Insert Tab, select** the **Module** option from the menu.

![Insert Tab](https://eadn-wc04-371788.nxedge.io/cdn/wp-content/uploads/2020/12/Insert-Tab-20.png)<figcaption id="caption-attachment-19831">Insert Tab</figcaption>

- Then, you need to **enter the below given VBA code** in the code window.

VBA Code for addup numbers

Sub myForNextLoop()  
Dim i As Integer, iSum As Integer  
iSum = 0  
For i = 1 To 50  
iSum = iSum + i  
Next  
MsgBox iSum, , “For …Next Loop”  
End Sub

- Finally, you need to **save the code.**

![Save the code](https://eadn-wc04-371788.nxedge.io/cdn/wp-content/uploads/2020/12/1-29.png)<figcaption id="caption-attachment-20143">Save the code</figcaption>

- Now, you need to **close** this **window.**
- Then, go back to the workbook and **run** the **macro** by **clicking** the **Macros** option under the **Developer Tab.**

![Click Macros option](https://eadn-wc04-371788.nxedge.io/cdn/wp-content/uploads/2020/12/Click-Macros-option.png)<figcaption id="caption-attachment-19833">Click Macros option</figcaption>

- Then, it will display the **Macro Dialog Box** , where you need to **select** the **macro name** and **hit** the **Run** button.

![Macro Dialog Box](https://eadn-wc04-371788.nxedge.io/cdn/wp-content/uploads/2020/12/2-19.png)<figcaption id="caption-attachment-20144">Macro Dialog Box</figcaption>

- Then, it will display the output as shown in the below screenshot.

![Output](https://eadn-wc04-371788.nxedge.io/cdn/wp-content/uploads/2020/12/3-17.png)<figcaption id="caption-attachment-20145">Output</figcaption>

## Do …Loop:

Let’s see how to **add numbers from 1 to 100** using Do Loop.

- You need to [**open** the **Visual Basic Editor**](https://geekexcel.com/shortcut-to-open-vba-editor-in-microsoft-excel-365/) by clicking the shortcut [**ALT** + **F11**](https://geekexcel.com/shortcut-to-open-vba-editor-in-microsoft-excel-365/) Keys.
- Then, on the **Insert Tab, select** the **Module** option from the menu.

![Insert Tab](https://eadn-wc04-371788.nxedge.io/cdn/wp-content/uploads/2020/12/Insert-Tab-20.png)<figcaption id="caption-attachment-19831">Insert Tab</figcaption>

- Then, you need to **enter the below given VBA code** in the code window.

VBA Code for addup numbers

Sub myDoLoop()

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

- Finally, you need to **save the code.**

![Enter the code](https://eadn-wc04-371788.nxedge.io/cdn/wp-content/uploads/2020/12/4-16.png)<figcaption id="caption-attachment-20146">Enter the code</figcaption>

- Now, you need to **close** this **window.**
- Then, go back to the workbook and **run** the **macro** by **clicking** the **Macros** option under the **Developer Tab.**

![Click Macros option](https://eadn-wc04-371788.nxedge.io/cdn/wp-content/uploads/2020/12/Click-Macros-option.png)<figcaption id="caption-attachment-19833">Click Macros option</figcaption>

- Then, it will display the **Macro Dialog Box** , where you need to **select** the **macro name** and **hit** the **Run** button.

![Click Run option](https://eadn-wc04-371788.nxedge.io/cdn/wp-content/uploads/2020/12/5-14.png)<figcaption id="caption-attachment-20147">Click Run option</figcaption>

- Then, it will display the output as shown in the below screenshot.

![Result](https://eadn-wc04-371788.nxedge.io/cdn/wp-content/uploads/2020/12/6-14.png)<figcaption id="caption-attachment-20148">Result</figcaption>

## While …Wend Loop:

In this section, we are going to **add numbers from 1 to 100**  using While Wend Loop. To do this, follow the below steps.

- You need to [**open** the **Visual Basic Editor**](https://geekexcel.com/shortcut-to-open-vba-editor-in-microsoft-excel-365/) by clicking the shortcut [**ALT** + **F11**](https://geekexcel.com/shortcut-to-open-vba-editor-in-microsoft-excel-365/) Keys.
- Then, on the **Insert Tab, select** the **Module** option from the menu.

![Insert Tab](https://eadn-wc04-371788.nxedge.io/cdn/wp-content/uploads/2020/12/Insert-Tab-20.png)<figcaption id="caption-attachment-19831">Insert Tab</figcaption>

- Then, you need to **enter the below given VBA code** in the code window.

VBA Code for addup numbers

Sub myWhileWendLoop()

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

- Finally, you need to **save the code.**

![Save the Code](https://eadn-wc04-371788.nxedge.io/cdn/wp-content/uploads/2020/12/12-5.png)<figcaption id="caption-attachment-20157">Save the Code</figcaption>

- Now, you need to **close** this **window.**
- Then, go back to the workbook and **run** the **macro** by **clicking** the **Macros** option under the **Developer Tab.**

![Click Macros option](https://eadn-wc04-371788.nxedge.io/cdn/wp-content/uploads/2020/12/Click-Macros-option.png)<figcaption id="caption-attachment-19833">Click Macros option</figcaption>

- Then, it will display the **Macro Dialog Box** , where you need to **select** the **macro name** and **hit** the **Run** button.

![Macro Dialog Box](https://eadn-wc04-371788.nxedge.io/cdn/wp-content/uploads/2020/12/8-9.png)<figcaption id="caption-attachment-20152">Macro Dialog Box</figcaption>

- Then, it will display the output as shown in the below screenshot.

![Result](https://eadn-wc04-371788.nxedge.io/cdn/wp-content/uploads/2020/12/6-14.png)<figcaption id="caption-attachment-20148">Result</figcaption>

## Verdict:

In the above article, you will easily understand how to use **Excel [VBA](https://geekexcel.com/excel-office-365-vba-overview-definition-and-examples/) Loops Statements in Office 365** with clear-cut examples. Kindly, leave your **worthwhile feedback** in the comment section. Thanks for visiting **[Geek Excel.](https://geekexcel.com/)** Keep Learning!

### Related Posts:

- [Excel VBA InputBox Function in Office 365 With Examples!!](https://geekexcel.com/excel-vba-inputbox-function-office-365/)
- [Excel VBA Date And Time Function in Office 365 ~ Easy Tutorial!!](https://geekexcel.com/excel-vba-date-and-time-function/)