Procedures and Functions in Excel VBA



Procedures and Functions makes our project modular, readable, lean and professional. They, too, prevent us from writing codes for a function which is repeated several times. Thus, it also helps in optimizing our design.
In Excel-VBA, a procedure is created using keyword sub whereas a function is represented with function keyword. The main difference between Procedure and Function is the value returned by the function.

Procedure
The primary way to define a procedure is 

[Public|Private] Sub PROCEDURE_NAME(arg1, arg2, arg3…)
……
…..
End sub 

Public and Private determines access level of a procedure. If a procedure is made private, it can be called by other procedures of the same module. A procedure created as public can be called by procedures of the same module and by procedures of other modules.
Argument can be either optional or mandatory. By default it is mandatory. To make is optional, we need to add a keyword optional while defining arguments

Sub My_Name(name as string, optional DOB as date, optional address as string)
…….
End sub

A procedure may be called with or without optional arguments but not without mandatory arguments. Order of the arguments is important too.
IsMissing(Arg) is a in-built function to evaluate and take action in case one or more optional arguments are missing.

Sub My_Name(name as string, optional DOB as date, optional address as string)
…….
If (IsMissing(DOB)) then
MsgBox “Date of Birth is Missing”
End if

If (IsMissing(address)) then
MsgBox “Address is missing”
End if
End sub


‘ calling procedure from the main function
Sub Main_Procedure()
…..
Dim name, address as string
Dim date_of_birth as Date
Name = “Naveen”
date_of_birth = “15-10-1982”
address = “GKP”
My_Name  Name, date_of_birth, address
My_Name Name, , address
My_Name Name
My_NameName,date_of_birth,

End sub

The arguments value can be addressed either by reference (ByRef) or by value (ByVal). Default is by reference, where address of the argument is transferred. Hence, it is possible to modify the value of the argument. When arguments are passed using ByVal, only their value is passed and it cannot be changed in the procedure.

Sub My_Name(byrefname as string, byvalDOB as date, byref address as string)
…….
End sub

Functions
As mentioned earlier, unlike a procedure a function returns a value.

Syntax or format of function is 

[Public|Private]FunctionFunctionName() As DataType

End Function

Data Type determines the type of value the function will return when executed. Like procedure, it too has access type, name and arguments.

Public function My_Name(FN as String, LN as String) as string
My_Name = FN + LN
End function


Sub Main_Procedure()
Dim A as string
Dim B as string
A= “Naveen”
B=”Tiwari”
MsgboxMy_Name(A, B)
…
End sub 

This blog gives a very light overview of functions and procedures. Please follow the link to explore more and in-depth. 

We have following data in an excel sheet.
 

Example of Function  
 
 

 

Example of Procedure