Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, and 2016. 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

by Allen Wyatt
(last updated September 9, 2017)

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.

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, and 2016. 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

Including Footnotes and Endnotes in Word Counts

When you have Word calculate how many words there are in a document, it normally doesn't pay attention to text in ...

Discover More

Putting a Different Date in a Header

Today's date is easy to add to a header, but what if you want to add a date that is adjusted in some manner? Adding ...

Discover More

Understanding Single Line Spacing

Single line spacing, the default spacing used in a paragraph, allows Word to adjust the spacing of individual lines in a ...

Discover More

Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!

More ExcelTips (ribbon)

Installing the Date Picker

When creating your own user interfaces within Excel, the various developer controls can add a nice touch. One such ...

Discover More

Friendly and Informative Error Handling

When creating macros, it is helpful to know what is going on within the macro itself in case an error crops up. Here's ...

Discover More

Documenting Changes in VBA Code

Your company may be regulated by requirements that it document any changes to the macros in an Excel worksheet. Your ...

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}] 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 0 + 1?

2017-09-10 19:21:29

Craig S

Absolutely agree with Alex B - I've never used the "While ... Wend" construct in 15 years (or more) of VB6/VBA programming. I have a philosophical problem with it, in the first instance - it's so reminiscent of TRS-80 BASIC (never used it, but did see a lot of it, and it fills me with horror) that I can't bring myself to use it. But, more importantly, the "Do ... Loop" constructs are much more versatile, and also have a better syntactical link with the "For ... Next" construct.


2017-09-09 23:50:24

Alex B

In order not to send a less experienced vba programmer down the wrong path, I think the point should be made that the While/Wend loop is essentially obsolete and is there for backward compatibility. So it’s worth knowing in case you come across it but you wouldn’t use it when writing new code.

Most programmers would use the Do loop, which is more flexible, allows the following:-
Do While …..Loop
Do Until …..Loop
The following will run the code at least once since the test is at the end.
Do …..Loop While
Do …..Loop Until

Do support exiting the loop and this is not available in the While / Wend.
- Exit Do


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.