JIYIK CN >

Current Location:Home > Learning > PROGRAM > Vba >

Calling a Sub in VBA

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

We will show you how to call a sub within another sub through examples in VBA.

Calling a Sub in VBA

When dealing with multiple subroutines, we may encounter situations where we need to call multiple subroutines for the same function.

Some functions require writing a lot of code, and to make these codes easy to understand; we need to divide them into multiple parts. We need to call these parts in a single subroutine to ensure that the whole process works in a streamlined manner.

This tutorial will teach us to call multiple subs in a single sub. Let us go through an example in which we will get the sum of multiple columns and then get the sum of multiple sums as shown below.

Sub sumOfA()
Dim result As Double
result = WorksheetFunction.Sum(Range("A2:A8"))
Range("A11") = "Sum Of A"
Range("A12") = result
End Sub
Sub sumOfB()
Dim result As Double
result = WorksheetFunction.Sum(Range("B2:B8"))
Range("B11") = "Sum Of B"
Range("B12") = result
End Sub
Sub totalSum()
Call sumOfA
Call sumOfB
Dim result As Double
result = WorksheetFunction.Sum(Range("A12:B12"))
Range("C11") = "Total Sum"
Range("C12") = result
End Sub

Output:

Calling a Sub in VBA

As we can see from the example above, we want to get the sum of multiple columns, and then we want to get the sum of the sums we got. To do this, we break the code into three different subroutines and call these two subroutines in the last subroutine.

Breaking up your code into multiple functions is a good practice to keep them simple and easy to follow.

Previous: None

Next:Exit Sub 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

Exit Sub in VBA

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

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 inpu

在 VBA 中调用 Sub

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

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

Scan to Read All Tech Tutorials

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

Recommended

Tags

Scan the Code
Easier Access Tutorial