VBA Blogs: Going Through the Visual Basics – Part 15
7 December 2018
We thought we’d run an elementary series going through the rudiments of Visual Basic for Applications (VBA) as a springboard for newer users. This blog takes a WHILE to get going...
In a programming, a control structure determines the order in which statements are executed. The iteration control structure is used for repetitively executing a block of code multiple times.
The iteration structure executes a sequence of statements repeatedly if a condition holds true. One such type is WHILE.
The WHILE…WEND loop executes a series of statements as long as a given condition is True. The syntax is very simple:
The condition must result in a Boolean value of True or False. WHILE tests the condition and if it is True then proceeds to execute the statements inside the loop.
Dim counter As Integer
counter = 0
While counter < 5
counter = counter + 1
While loops are preferred when the number of iterations is unknown. For example, modelling how many days it takes to reach sales a target, or running through a worksheet column until it reaches an empty cell.
Notice how the condition is tested first – this means that the code will not run at all if the condition is not met. WHILE…WEND is a remnant from BASIC where VBA originated from. These are not as powerful as DO…LOOP (covered soon).