QP School

Full Version: Loop to display numbers in VBA
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Description:

This code uses a loop to display numbers from 1 to 5 in separate message boxes.

Example:

Sub DisplayNumbers()
    Dim i As Integer

    For i = 1 To 5
        MsgBox "Number: " & i
    Next i
End Sub

Explanation:

This code defines a subroutine named DisplayNumbers. 
It declares a variable i to use as a loop counter. 
The For loop runs from 1 to 5, and for each iteration, a message box displays the value of i.

The For loop is a control structure used to execute a block of code repeatedly for a specific range of values. 
In this case, the loop runs five times, and the i variable takes values from 1 to 5.