What is sub and private sub in VBA?

In VBA, a Public Sub can be accessed (called) from any code module. A Private Sub can only be called from the module in which it resides. This concept is named Scope. Objects and variables also have scope.

What is the purpose of private sub?

A sub procedure can take two forms, i.e., private and public. The modifiers “private” and “public” allows users to use the subs differently. The private sub procedure can only be used in the current module. The public sub allows users to use the procedure in all modules present in the workbook.

What does sub () mean in VBA?

Subroutine procedure
SUB means Subroutine procedure, it’s a series of VBScript statements, where it does not return a result or value. Sub procedure usually take arguments or code (i.e. variables, constants or expressions that are passed by a calling procedure) which are carried out to perform a specific task.

What is the difference between sub and private sub?

The basic concept is that Public variables, subs or functions can be seen and used by all modules in the workbook while Private variables, subs and functions can only be used by code within the same module.

What does sub and End Sub mean?

A Sub procedure is a series of Visual Basic statements enclosed by the Sub and End Sub statements. The Sub procedure performs a task and then returns control to the calling code, but it does not return a value to the calling code. You can define a Sub procedure in modules, classes, and structures.

Can subs have arguments in VBA?

A statement in a Sub or Function procedure can pass values to called procedures by using named arguments. You can list named arguments in any order.

Can VBA subs take arguments?

Like a Function procedure, a Sub procedure is a separate procedure that can take arguments, perform a series of statements, and change the value of its arguments. However, unlike a Function procedure, which returns a value, a Sub procedure can’t be used in an expression.

What is the purpose of Private Sub and End Sub?

Where there is no difference between dim and private in VBA?

There is no different between using Dim or Private on a module/class level variable, because they mean the same thing. Inside an actual code block or method, you can’t use the private keyword.

What is End Sub VBA?

VBA Exit Sub is a statement that you use to exit a sub-procedure or a function. As you know, each line is a macro executes one after another, and when you add the “Exit Sub” VBA, exit the procedure without running the rest of the code that comes after that. It works best with loops and the message box.