How to Use While Loop Statement in Excel VBA Macro - Simple Steps

Who I am
Craig Zacker
@craigzacker
Author and references

The modules that are created in the macro section allow the developer user to comply procedures deemed impossible.

This way, if using the charms on the toolbar does not allow you to perform a function, the encoding built into Visual Basic Editor will be able to create the parameters within Excel in order to obtain the desired result.

What is the While Loop statement in VBA about and how does it work?

Creating encodings that contain the English term instruction While o Wend it is reflected to obtain the repetition of a specific type of procedure.



The macrocode is made up of complements that will make it possible to form the parameters mentioned above, among which are established:

  • Mainly, the condition is mandatory determined through a string or a number whose value determines whether the content is true or false.
  • In case the information entered and requested by the person meet the conditions stated in the code, these are satisfied and the loop is executed.
  • On the other hand the statement is an optional parameter, but it is he who assigns meaning to the probability of the result.

It is based on a number of different procedures that need to be done over and over again until the user sets a false condition.

Method of operation

Whatever the structure established in the code, the cycle has the ability to repeat itself until the condition is satisfied.

When the function or procedure starts to run, it starts to be analyzed from top to bottom.


In this way the cycle studies the condition that has been placed on the upper line and if the rest of the information falls within the parameters, it is taken as true and the underlying lines are executed " While ".


Once the deadline is reached " loop «, The program returns to the beginning and re-analyzes the condition that has been entered repeatedly.

When there is a change in the information in the table that causes the condition you become false to stop repeating the cycle indefinitely.

Here's how While stops execution and the program flow established by the code continues to execute the instructions below " loop «.

Sample code using the While Loop

Education While Loop in VBA is set up as follows in the example of a hospital room listing to determine if it is available for assignment to a patient who needs it.

Sub habestato ()

Riga = 2

Run while cells (row, 1) <> «»

If (Cells (riga, 2) <500 And Cells (riga, 3) = «Sporco») Allora


Cells (line, 4) = "Not available on the 4th floor"

End if

If (Cells (row, 2) <500 And Cells (row, 3) = «Clean») Then

Cells (line, 4) = "´Available on the 4th floor"

End if

If (Cells (riga, 2) <400 And Cells (riga, 3) = «Sporco») Allora

Cells (line, 4) = "Not available on the 3th floor"

End if

If (Cells (riga, 2) <400 And Cells (riga, 3) = «Pulisci») Allora


Cells (row, 4) = "Available on the 3rd floor"

End if

If (Cells (riga, 2) <300 And Cells (riga, 3) = «Sporco») Allora

Cells (line, 4) = "Not available on the 2th floor"

End if

If (Cells (riga, 2) <300 And Cells (riga, 3) = «Pulisci») Allora

Cells (row, 4) = "Available on the 2rd floor"

End if

If (Cells (riga, 2) <200 And Cells (riga, 3) = «Sporco») Allora

Cells (line, 4) = "Not available on the 1th floor"

End if

If (Cells (riga, 2) <200 And Cells (riga, 3) = «Pulisci») Allora

Cells (row, 4) = "Available on the 1rd floor"

End if

Riga = Riga + 1

Loop

End subtitle

add a comment of How to Use While Loop Statement in Excel VBA Macro - Simple Steps
Comment sent successfully! We will review it in the next few hours.