1 | initial version |
To create an 'If Then' loop in VBA, follow these steps:
Here is an example of an If Then loop that counts from 1 to 10:
Sub IfThenLoop()
Dim i As Integer
For i = 1 to 10
If i < 5 Then
Debug.Print "i is less than 5"
Else
Debug.Print "i is greater than or equal to 5"
End If
Next i
End Sub
This code displays a message indicating whether the value of i is less than 5 or greater than or equal to 5. The loop runs 10 times, incrementing the value of i each time, and executing the code within the loop for each iteration.