×

Procedures in VBA

Procedures in VBA

A procedure is a block of statements or units of computer code that performs some action. It is enclosed with a declaration statement, and its primary purpose is to carry out a particular task or action. VBA supports two types of procedures: Sub procedures and Function procedures.

Sub Procedure

The Sub procedure consists of a series of statements and can be executed in several ways. Here’s an example of a simple Sub procedure called Test: This procedure calculates a simple sum and then displays the result in a message box.

VBA Sub procedure names must follow specific rules. The Four elements which are required to declare VBA Sub procedure are as follows:

  1. Sub Statement: The Sub keyword is used to declare the beginning of the VBA Sub procedure. 
  2. Name: It represents the name of the VBA Sub procedure. The first character must be a letter rest can be letters, numbers, or certain punctuation characters (excluding #, $, %, &, @, ^, *, ! and spaces). The maximum number of characters a name can have is 255.
  3. Parentheses: Parentheses are used to declare the arguments from other procedures where the arguments must be separated by comma (,). Although it’s optional i.e., you can even declare a VBA without any arguments just by putting empty parenthesis {}.
  4. Statements: Sub procedure has a block of statements that is enclosed by the declaration and End declaration statements
  5. End Sub Keyword: An End Sub keyword is used to exit the VBA Sub procedure in which the statements are included.

Program:

Sub Example()
Mul = 1 * 1
MsgBox “ The multiplicative answer is” & Mul
End Sub 
Procedures in VBA Excel

Function procedures

A Function procedure returns a single value (or possibly an array). A Function can be called from another VBA procedure or used in a worksheet formula. A VBA function returns a value by allocating a value to its name in one or more statements of the procedure. A Function procedure is similar to a Sub procedure, with only one difference that a function can also return a value.

VBA Function procedure names must follow specific rules. The Four elements which are required to declare VBA Function procedure are as follows:

  1. Function Statement: The Function keyword is used to declare the beginning of the VBA Function procedure. 
  2. Name: It represents the name of the VBA Function procedure. The first character must be a letter rest can be letters, numbers, or certain punctuation characters (excluding #, $, %, &, @, ^, *, ! and spaces). The maximum number of characters a name can have is 255.
  3. Parentheses: Parentheses are used to declare the arguments from other procedures where the arguments must be separated by comma (,). Although it’s optional i.e., you can even declare a VBA Function without any arguments just by putting empty parenthesis {}.
  4. Statements: Function procedure has a block of statements that is enclosed by the declaration and End declaration statements
  5. End Function Keyword: An End Function keyword is used to exit the VBA Function procedure in which the statements are included.

Program:

Function MulTwo (arg1, arg2) 
MulTwo= arg1 * arg2
End Function 
Function procedures

Related Topics

Excel VBA Filter Function

VBA Filter Function: The Filter function in VBA returns a subset for the given string array, based on specified criteria. Syntax Filter (SourceArray, Match, [Include], [Compare]) Parameter SourceArray (required) – This parameter the array of Strings that you...

2 minutes read.

Excel VBA DateAdd Function

The DateAdd function in VBA adds a time interval to a supplied date and/or time and returns the resultant date/time. Syntax Dateadd (Interval, Number, Date) Parameter Interval (required) – This parameter a string specifying the interval to be...

1 minute read.

Excel VBA RTrim Function

VBA RTrim Function: The Rtrim function in VBA removes the leading spaces from the text in the specified string. Syntax RTrim (String) Parameter String (required) – This parameter represents the string from which you want...

1 minute read.

Excel VBA FormatNumber Function

VBA FormatNumber Function: The FormatNumber function in VBA is used to apply a number format to a numeric expression, and it returns the result as a string. Syntax FormatNumber (Expression, [NumDigitsAfterDecimal], [IncludeLeadingDigit], [UseParensForNegativeNumbers], [GroupDigits]) Parameter Expression (required)...

2 minutes read.

VBA Type Mismatch Error

What is a Type Mismatch Error? VBA Type Mismatch Error is a run time error in excel, which often occurs when the data types contained in a VBA code are not matched...

3 minutes read.

Excel VBA IsDate Function

VBA IsDate Function: The IsDate function in VBA returns a Boolean value indicating whether the given expression is interpreted as a VBA Date or not. Syntax IsDate (Expression) Parameter Expression (required) – This parameter...

1 minute read.

Excel VBA TimeValue Function

Excel VBA TimeValue Function: The TimeValue function in VBA returns a Time from the specified String interpretation of a time /date where the date information for the given string is...

1 minute read.

Excel VBA Minute Function

The Minute function in VBA returns the minute component for the specified time. Syntax Minute (Time) Parameter Time (required) – This parameter represents the time. Return This function returns the minute component for the specified time. Example 1 Sub MinteFunction_Example1() ...

1 minute read.

Excel VBA: IF…..THEN …ELSE Statement

VBA : IF…..THEN …ELSE Statement: This function enables you to check one condition and, based on that, then run one of the two statement blocks present. If the ‘IF’ condition...

2 minutes read.

Excel VBA FormatPercent Function

VBA FormatPercent Function: The FormatPercent function in VBA is used to apply a percent format to a numeric expression, and it returns the result as a string. Syntax FormatPercent (Expression, [NumDigitsAfterDecimal], [IncludeLeadingDigit], [UseParensForNegativeNumbers], [GroupDigits]) Parameter Expression (required)...

2 minutes read.

Excel VBA InStr Function

VBA InStr Function The InStr function in VBA searches for a substring inside the given string. It returns the position of a substring within a string, as an integer if the substring is found...

1 minute read.

Excel VBA GoTo Statement

GoTo Statement he GoTo statement branches unconditionally to a specified line in a procedure. It is used to transfer the program control to a new statement, which is headed by a label. It sends...

2 minutes read.

Excel VBA Now Function

Excel VBA Now Function: The Now function in VBA returns the current date and time.  Syntax Now () Parameter NA Return This function returns the current date and time.  Example 1 Sub NowFunction_Example1() 'It will return...

1 minute read.

Excel VBA LTrim Function

VBA LTrim Function: The LTrim function in VBA removes the leading spaces from a supplied text string. Syntax LTrim (String) Parameter String (required) - This parameter represents he text string that you want to remove...

1 minute read.

ActiveX Controls

ActiveX Controls are one of the most used Excel Controls to automate applications with Excel VBA. It has the same controls, unlike Form Controls (Command Button, combo box, checkbox, etc.), but it...

3 minutes read.

VBA Updating Pivot Table

VBA- Updating Pivot Table The pivot table is an important feature to explore, summarize, and interpret the bulk amount of data. It helps data evaluating, reviewing, as well as making useful...

3 minutes read.

VBA Subscript out of Range

What is Subscript out of Range? The VBA Subscript out of Range error (which is also called as Run-Time Error 9) mostly triggers when the user selects any cell, sheet, or workbook which does...

5 minutes read.

Excel VBA Sin Function

VBA Sin Function: The Sin function in VBA returns the sine value for a supplied angle. Syntax Sin (Number) Parameter Number (required) –This parameter represents the angle (in radians) to calculate the sine value. Return This function...

1 minute read.

Excel VBA Trim Function

The Trim function in VBA removes the leading and trailing spaces from the specified string. Syntax Trim (String) Parameter String (required) – This parameter represents the string from which you want to remove the leading and...

1 minute read.

Excel VBA FormatCurrency Function

VBA FormatCurrency Function: The FormatCurrency function in VBA is used to apply a currency format to a numeric expression and returns the result as a string. Syntax FormatCurrency (Expression, [NumDigitsAfterDecimal], [IncludeLeadingDigit], [UseParensForNegativeNumbers], [GroupDigits]) Parameter Expression (required)...

2 minutes read.