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

Jumping to a Range

Need a quick way to jump to a particular part of your worksheet? You can do it by using the Go To dialog box.

Discover More

Word Find and Replace (Table of Contents)

The Find and Replace tool is one of the most-used tools provided by Word. However, few people know how to effectively, ...

Discover More

Problems Pasting Information into a Worksheet

What do you do if pasting information into a worksheet brings Excel to its knees? This tip looks at just a few ideas you ...

Discover More

Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!

More ExcelTips (ribbon)

Checking if a Workbook is Already Open

Knowing if a workbook is already open can be a prerequisite to your macro working correctly. Here's how to check it out.

Discover More

Understanding the Select Case Structure

One of the powerful programming structures available in VBA is the Select Case structure. This tip explains how you can ...

Discover More

Selectively Importing Records

Want to easily control which records get imported from a text file into Excel? It's easy to do when you write the macro ...

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

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.