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

Using GEOMEAN with a Large List

When performing a statistical analysis on a large dataset, you may want to use GEOMEAN to figure out the geometric mean ...

Discover More

Changing the Height of a Font

Scaling the width of a font is easy to do with Word's formatting capabilities. Scaling the height of the fonts is not so ...

Discover More

Quickly Moving Your Table

For those who love to use the mouse during editing, you can use the little critter to help move your tables to exactly ...

Discover More

Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!

More ExcelTips (ribbon)

Selecting the First Cell In a Row

When creating macros, you'll often have a need to select different cells in the worksheet. Here's how to select the first ...

Discover More

Calling a Subroutine from a UDF

Excel allows you to create a special type of macro called a user-defined function (UDF). These can let you add to the ...

Discover More

Default Worksheet when Opening

When opening a workbook, you may want to make sure that a particular worksheet is always displayed first. The only way to ...

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 8 - 5?

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.