📜  vba exit while - VBA (1)

📅  最后修改于: 2023-12-03 15:20:57.553000             🧑  作者: Mango

VBA Exit While Statement

In VBA (Visual Basic for Applications), the Exit While statement is used to prematurely exit a While loop. This statement allows programmers to terminate the execution of the loop based on certain conditions, without completing the remaining iterations.

The syntax of the Exit While statement is as follows:

Exit While

Example

Consider the following code snippet that demonstrates the usage of the Exit While statement:

Sub ExitWhileExample()
    Dim i As Integer
    
    i = 1
    While i <= 10
        If i = 5 Then
            Exit While
        End If
        Debug.Print i
        i = i + 1
    Wend
    
    Debug.Print "Loop ended."
End Sub

In this example, the While loop is executed as long as the value of i is less than or equal to 10. However, when i becomes equal to 5, the Exit While statement is encountered and the loop is terminated immediately. As a result, only numbers 1 to 4 will be printed in the Immediate Window.

Markdown Code Block

The following is a markdown code block demonstrating the usage of the Exit While statement:

```vba
Sub ExitWhileExample()
    Dim i As Integer
    
    i = 1
    While i <= 10
        If i = 5 Then
            Exit While
        End If
        Debug.Print i
        i = i + 1
    Wend
    
    Debug.Print "Loop ended."
End Sub
Please note that the code block is wrapped with triple backticks (```) and the language identifier `vba` is specified after the opening triple backticks to indicate the code's language.

Using this markdown code block in a markdown file or online editor that supports syntax highlighting (like GitHub Markdown or VS Code), the code will be properly formatted and displayed with syntax highlighting for VBA.