JIYIK CN >

Current Location:Home > Learning > PROGRAM > Vba >

Exit Sub in VBA

Author:JIYIK Last Updated:2025/04/16 Views:

We will look at different ways to exit a sub in VBA through examples.

Using the Exit Sub Statement in VBA

While using Sub in VBA, we may want to exit it or prevent it from executing further if an error occurs or the user provides wrong input. We can also terminate the sub at any time using exit sub.

In VBA, each line is executed line by line, so if we have reached the result or the expected response at a certain point of execution is not what we want, we can terminate the sub using exit sub.

There are many situations where we can use this feature. Let's go through an example and try to exit the sub in the middle of the code.

Sample code:

# VBA
Sub roundFunc()
Dim num As Double
num = 5.468
Exit Sub
MsgBox Application.WorksheetFunction.RoundUp(num, 1)
End Sub

From the above example, I have added the exit sub code before the message box and when we run this code, no message box appears.

Let's take another if-elseexample using the if statement. If the statement is true, it will execute the code until the end. Otherwise, it will break.

Sample code:

# VBA
Sub roundFunc()
Dim num As Double
num = 5.468
If Application.WorksheetFunction.RoundUp(num, 1) = 6 Then
    MsgBox num
Else
    MsgBox "Wrong Number"
    Exit Sub
num = num + num
End If
End Sub

Output:

Exiting a sub after msgBox in VBA

When we are dealing with important data, we want to make sure that if there is any mistake or any input error, we need to exit the sub immediately to ensure that no data is lost or there is no wrong data inside the company data.

Previous: None

Next:Sum function in VBA

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Remove duplicates in VBA

Publish Date:2025/04/16 Views:104 Category:Vba

We will explain how to remove duplicates in VBA by example. Remove duplicates in VBA When working with an Excel worksheet that contains a lot of data, it is likely that this data contains some duplicates. Any duplicates must be removed to a

Rounding in VBA

Publish Date:2025/04/16 Views:162 Category:Vba

Round() We will introduce the and functions in VBA through examples RoundUp() . Round() Using the or RoundUp() function in VBA When working with Excel worksheets containing numbers and calculations, we get results that are fractions. Someti

Adding a new row in VBA

Publish Date:2025/04/16 Views:194 Category:Vba

We will show you how to continue the code on the next line in VBA by example. We will also show you how to go to the next line in a message box using different methods in VBA. Adding a new row in VBA There are two different situations in pr

Sum function in VBA

Publish Date:2025/04/16 Views:126 Category:Vba

We will show you how to use sum in VBA. Sum function in VBA Sum is the most commonly used function in excel. This function is very useful as we can use sum to get the total from the financial table. We will learn how to use the sum function

VBA 中的 Exit Sub

Publish Date:2023/03/19 Views:389 Category:Vba

我们将演示如何在 VBA 中使用 exit sub。

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial