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

Aligning Positive and Negative Whole Numbers in a Column

When you use a table to present numeric information, you may want to have Word align the numbers in the table. This can ...

Discover More

Moving Object Anchors

When you insert an object into your document, it is anchored to a paragraph. If you want to change the paragraph to which ...

Discover More

Underlining Quoted Text

Do you have a document in which you need to convert all the quoted text (text surrounded by quotes) to underlined text? ...

Discover More

Solve Real Business Problems Master business modeling and analysis techniques with Excel and transform data into bottom-line results. This hands-on, scenario-focused guide shows you how to use the latest Excel tools to integrate data from multiple tables. Check out Microsoft Excel 2013 Data Analysis and Business Modeling today!

More ExcelTips (ribbon)

Selecting Columns in VBA when Cells are Merged

If you have a macro that selects different columns in a worksheet while processing information, you may get some ...

Discover More

Hiding Macros

Need to hide some macros in your workbook? There are three ways you can do it, as covered in this discussion.

Discover More

Copying Worksheet Code Automatically

When creating a workbook to be used by others, you may want any worksheets they add to the workbook to contain some ...

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 two more than 9?

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.