Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Understanding the While...Wend Structure.

Understanding the While...Wend Structure

Written by Allen Wyatt (last updated April 8, 2023)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021


2

Macros in Excel are written in a language called VBA. Like other programming languages, VBA includes certain programming structures that are used to control how the program executes. One of these structures is the While...Wend structure. This structure has the following syntax:

While condition
    program statements
Wend

When a macro is executing and this structure is encountered, the language tests whatever condition you have defined. You can see examples of conditions in many of the macros used in ExcelTips. If the condition is true, then the program statements between the While and Wend keywords are executed. If the condition is not true, execution of the macro continues with the statement following the Wend keyword. If the conditions are true when Wend is encountered, the macro will loop back up to the While statement and keep executing the loop until the condition becomes false.

Understand that the While...Wend structure is one of a number of different structures that can be used to control how code within a macro is executed. Some VBA programmers contend that While...Wend is "obsolete" and that other structures such as the various Do loops should be used in preference to it. It is true that While...Wend predates the newer structures, but the predating doesn't mean that the structure should never be used.

What structure should you use? The one that makes the most sense to you as you are putting your coding logic together. If you prefer While...Wend, then use it because it will work just fine. If you prefer a different looping structure, then by all means use that one. VBA provides them all so that you can approach a task using whatever makes the most sense to you.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (12425) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021. You can find a version of this tip for the older menu interface of Excel here: Understanding the While...Wend Structure.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

PRIVATE Fields in WordPerfect Documents

Did you ever convert a document from WordPerfect and see PRIVATE fields in it? Here's what those fields mean.

Discover More

Displaying Fields

Fields (sometimes called field codes) allow you to insert dynamic information in your documents. If you want to see the ...

Discover More

Pasting a Hyperlink

When you paste information into a document, you can specify that it be inserted as a hyperlink rather than as normal ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

More ExcelTips (ribbon)

Storing Macros in Templates

How Excel uses templates is different than how Word uses templates. This tip looks at those differences and discusses ...

Discover More

Clearing the Clipboard in a Macro

You may want your macro to clear the clipboard so that people cannot access anything left in the clipboard. That is ...

Discover More

Mouse Click Event in VBA

Need to know if a particular cell is clicked with the mouse? Excel has no particular event handler for clicking in this ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 2 + 2?

2023-04-10 08:23:45

Alex Blakenburg

The reference below indicates that one of the reasons it is no longer used is that the newer Do Loop provides you with the option of invoking the line "Exit Do". While Wend does not have an equivalent, you would need to use the frowned upon GoTo statement.

https://rubberduckvba.com/Inspections/Details/ObsoleteWhileWendStatement?example=2


2023-04-10 05:37:29

Alex Blakenburg

The reference below indicates that one of the reasons it is no longer used is that the newer Do Loop provides you with the option of invoking the line "Exit Do". While Wend does not have an equivalent, you would need to use the frowned upon GoTo statement.

https://rubberduckvba.com/Inspections/Details/ObsoleteWhileWendStatement?example=2


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.