An Excel VBA Subroutine is a code written in VBA (Visual Basic for Applications) that performs a specific action or task. In this tutorial, we will learn how to call a subroutine in VBA with an example.
When you write a Sub in VBA, it is automatically assigned a name. For example, if you write a Sub called "PrintHello", the name of the Sub will be "PrintHello". You can also give your Sub a different name when you declare it.
To call a Sub in VBA, you need to use the Call keyword followed by the name of the Sub. For example, to call the PrintHello Sub, you would use the following code:
When you run this code, the PrintHello Sub will be executed and "Hello World!" will be printed on the screen.
A Subroutine is a piece of code (a set of instructions) that performs a specific task. You can use subroutines to break down large chunks of code into smaller, more manageable pieces. Subroutines make your code easier to read and understand, and can be reused throughout your program.
There are two ways to call a subroutine in VBA:
1. By using the Call keyword:
2. By using the name of the subroutine:
When you want to use a Sub procedure, you can call it from another Sub procedure or from a Function procedure. A Sub procedure is like a Function procedure, except that it doesn't return a value. You can call a Sub procedure by using its name followed by parentheses, like this:
' Statements go here.
You can also call a Sub procedure by using the Call keyword, like this:
An Excel VBA Subroutine is a code that resides within a Module, and they don’t have a specific return type. A Sub can take arguments (which are values that you pass to it) just like a Function. You can call a Subroutine by using the Call statement or by simply using the Sub name followed by parentheses enclosing any required arguments.
Here’s an example of how you might use a Subroutine in your code:
'some code here
In the above example, we’ve created a simple Subroutine named “MySub” which takes one argument called “MyArgument”. When this code is executed, the macro will do whatever is inside the body of the Sub (between the “Sub” and “End Sub” statements). In this case, there is no code inside so