×

Excel VBA Functions

What is a function?

A function is also called a procedure, but it is not a sub procedure, it’s a function procedure. You have already been using some function in Excel, a mathematical function, like sum, max, min, average, Vlookup. Most of the functions you’re used to using in Excel are built in. But sometimes the requirement that we’re after does not entirely code for the built-in functions.

In VBA, the limitations of the function are you cannot record them. A procedure we can record, and thus, the code is automatically created for us. But a function must be hand-written by ourselves. 

To create a function, you use the Function keyword, followed by a function name and parentheses. Unlike a sub procedure, because a function returns a value, you must specify the type of value the function will produce. We will briefly review how to create our customized function in the following tutorial.

Instead of creating your function, you can use one of those that comes with the VBA language. This language provides a very extensive library of functions and offers many built-in functions that can be used in your code.

Function Categories

We will review the most popular built-in functions of VBA. Select the function name, to go to a full description of the function with examples of use.

1. VBA Text Functions

Function Description
Format This function applies a format to the given expression and returns the output as a string.
InStr It is used to return a position of a substring with the String.
InStrRev It is used to return the position of a substring within a string, seeking from right to left.
Left This function returns a left substring from the start of a supplied string.
Len It returns the length of the given string.
LCase This function converts the given string to lower case text.
LTrim It is used to remove the spaces from the given string.
Mid This function is used to returns a substring from the middle of the given string.
Replace It replaces a substring within the given string.
Right This function returns a substring from the end of the given string.
RemoveVowels This function is used to remove the vowels (a,e,I,o,u) from the specified string.
RTrim It is used to remove the trailing spaces from the string.
Space This function creates a string with the specified Datenumber of spaces.
StrComp It is used to compare two strings and return an integer value based on the comparison.
StrConv This function converts a string into a specified format.
String It creates a string comprising of several repeated characters.
StrReverse This function reverses the given string.
Trim This function removes the leading spaces from the String.
UCase This function is used to convert the string to upper case.

2. Informative Functions

Function Description
IsArray This function returns a Boolean value True if the supplied variable is an array.
IsDate This function checks whether the given expression is a date or not and returns a Boolean value True if the supplied variable is a Date.
IsEmpty This function returns a Boolean value True if the variable is Empty.
IsError This function returns a Boolean value True if the specified expression signifies an error.
IsMissing It is used to test if an optional argument to a procedure is missing.
IsNull This function returns a Boolean value True if the variable is Null.
IsNumeric This function returns a Boolean value True if the variable is numeric.
IsObject This function tests if a supplied variable represents an object variable

3. VBA Flow Functions

Function Description
Choose This function is used to select a value from the given list of arguments.
If It is used to evaluate an expression and returns one of two values (based on whether the expression calculates to True or False)
Switch This function is used to evaluate a list of Boolean expressions and returns a value associated with the first true expression.

4. VBA Error Handling Functions

Function Description
CVErr This function generates an Error data type for a given error code.
Error It is used to Return the error message based on a specified error code.

5. VBA Conversion Functions

Function Description
Asc This function returns an integer representing the ASCII code for the specified character.
CBool It is used to convert an expression to a Boolean data type.
CByte It is used to convert an expression to a Byte data type.
CCur It is used to convert an expression to a Currency data type.
CDate It is used to convert an expression to a Date data type.
CDbl It is used to convert an expression to a Double data type.
CDec It is used to convert an expression to a Decimal data type.
Chr This function returns the character value for the given character code.
Cint It converts an expression to an integer data type.
CLng It converts an expression to a Long data type.
CSng It converts an expression to a Single data type.
CStr It converts an expression to a String data type.
CVar It converts an expression to a Variant data type.
FormatCurrency This function returns a string value after applying a currency format to the specified expression.
FormatDateTime This function returns a string value after applying a date/time format to the specified expression.
FormatNumber This function returns a string value after applying a number format to the specified expression.
FormatPercent This function returns a string value after applying a percentage format to the specified expression.
Hex This function returns a string value after converting a numeric value to hexadecimal notation.
Oct This function returns a string value after converting a numeric value to octal.
Str This function is used to convert a numeric value to string.
Val This function is used to convert the specified string to a numeric value

6. Date & Time Functions

Function Description
Date This function returns the current date.
DateAdd This function adds a time interval to a date or time.
DateDiff This function is used to return the number of intervals between two dates and/or times
DatePart This function returns a part (day, month, year) of the specified date/time.
DateSerial It returns a Date from the given year, month and day number.
DateValue This function returns a Date from a String representation of a date/time.
Day It is used to return the day number (from 1 to 31) for the specified date.
Hour This function returns the hour component for the specified time.
Minute This function is used to return the minute component for the specified time.
Month This function is used to return the month number (from 1 to 12) for the given date.
MonthName This function is used to return the month name for the given month number (from 1 to 12).
Now This function returns the current date and time.
Second This function returns the second component for the given time.
Time This function returns the current time.
Timer It is used to return the number of seconds that have passed since midnight.
TimeSerial This function returns a Time from a supplied hour, minute and second.
TimeValue This function is used to return a Time from a String representation of a date/time.
Weekday It is used to return an integer (from 1 to 7), representing the weekday for the given date.
WeekdayName This function returns the weekday name for a specified integer (from 1 to 7).
Year This function returns the year of a supplied date.

7. Math & Trig Functions

Function Description
Abs This function returns the absolute value for the given number.
Atn It calculates the arctangent of a specified number.
Cos This function calculates the cosine for the given angle.
Exp This function calculates the value of ex for a specified value of x.
Fix It is used to truncate a number to an integer.
Int This function returns the integer portion of a number.
Log This function calculates the natural logarithm for the given number.
Rnd This function generates a random number between 0 and 1.
Round This function rounds a number to the given number of decimal places.
Sgn This function returns an integer representing the arithmetic sign of a number.
Sin It is used calculate the sine for the given angle.
Tan It is used to calculate the tangent of a specified angle.
Sqr This function returns the square root of a number.

8. VBA Array Functions

Function Description
Array This function creates an array, containing a specified set of values.
Filter This function is used to return a subset of a given string array, based on specified criteria.
Join This function joins several substrings into a single string.
LBound This function returns the lowest subscript for a dimension of an array.
Split It is used to split a Text String into several Substrings.
UBound This function returns the highest subscript for a dimension of an array.

Excel Worksheet Functions

VBA worksheet functions are usually faster than normal VBA code written to achieve the same results. Therefore, it is advisable to have a good knowledge of how to use worksheet functions in VBA. It helps you create VBA applications faster than you'd be able to create without them.

Worksheet function in VBA enables you to refer to a specific worksheet, normally when we craft a module in VBA the code executes in the currently active sheet of the workbook but if we want to execute the code in the specific worksheet we use worksheet function, this function has various uses and applications in VBA.

Generally, when you use Application, and your function produces an error it will return the error value.  But if the WorksheetFunction method is used, VBA will automatically throw a run time error.  One can handle the VBA error, but it’s is usually advisable to avoid the error in the first place.

In order to call worksheet functions using VBA, the WorksheetFunction object is used. The WorksheetFunction object is contained within the application object.

Example 1: Write a macro demonstrating the use of Worksheet Function.

Sub WorksheetFunction_Example()
     'start the code as “WorksheetFunction”
     'When you put a dot (.) it will display the functions available
     Range("B9").Value = WorksheetFunction.Sum(Range("B2:B8"))
 End Sub 

Output

Subjects Marks
Hindi 78
English 65
Math 98
SST 45
Computer 67
Science 87
Sanskrit 88
Total 528
VBA Functions

Related Topics

Excel VBA CBool Function

VBA CBool Function: The CBool function in VBA calculates an expression and returns the result as a Boolean data type. Syntax CBool (Expression) Parameter Expression (required) – This parameter represents the expression that that you want...

1 minute read.

VBA Dim

What is Dim? DIM or Dimension or Declare in Memory is a keyword that is used in VBA to declare a variable with the different data types (Integer, String, variable, Boolean, Double, etc.)...

7 minutes read.

Excel VBA Second Function

Excel VBA Second Function: The Second function in VBA returns the second element for the specified time.  Syntax Second (Time) Parameter Time (required) – This parameter represents the time. Return This function returns the second...

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: Do Until….Loop

DO UNTIL….Loop The “Do Until” Loop is same unlike DO WHILE statement just that it will keep on looping till the condition is not met. This loop is used to repeat a set...

4 minutes read.

Excel VBA Exp Function

VBA Exp Function: The Exp function in VBA returns the value of the exponential function ex (mathematical constant ‘e’ raised to specified power) for the given value of x. Syntax Exp (Number) Parameter Number (required) –This parameter represents...

1 minute read.

For Next loop in VBA

For Next Loop The ”For Next” loop is used for a fixed number of times. It works by implementing the loop for the specified number of times. In this, the user specifies how...

3 minutes read.

Excel VBA User-Defined Functions

User-Defined Functions One of the advantages of VBA is that you can create your own functions using macros. These functions can be called and used as other functions in excel and use them. You can...

3 minutes read.

Steps to Create a Pivot Table

Steps to Create a Pivot Table: Pivot Tables can be easily automated through VBA coding. To create a Pivot Table in VBA, follow the below step by step procedure to...

4 minutes read.

Excel VBA Val Function

VBA Val Function: The Val function in VBA converts the given string into a numeric value. This function ignores spaces and continues to read the characters after space(s). It stops...

1 minute read.

Excel VBA CInt Function

VBA CInt Function: The VBA Cint function converts the specified expression into an Integer. Syntax Cint (Expression) Parameter Expression (required) – This parameter represents the expression that you want to convert to an Integer wherein...

1 minute read.

CodeIgniter Architecture

Here we will understand the architecture and working of the CodeIgniter application, which helps you to elaborate all steps in simple ways. As the above image represents that whenever a request comes from the...

2 minutes read.

Excel VBA IsEmpty Function

VBA IsEmpty Function: The IsEmpty function in VBA returns a Boolean value showing whether the specified Expression is Empty (variant has not been declared) or not. Syntax IsEmpty (Expression) Parameter Expression (required)- This parameter...

1 minute read.

Excel VBA InputBox

Input Box The InputBox function in VBA is used to prompt the users to enter values. The user can click either the OK button or can choose the CANCEL button. If the user clicks...

3 minutes read.

VBA Object Required

What is Object Required Error? VBA Object Required is a run time error which occurs when the user does not define a valid object qualifier, or the assigned object doesn’t exist in the...

6 minutes 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 Global Variable

What is Global Variable? The Global Variables in VBA refers to the variables declared before the start of any macro. They are defined outside the functions and are used by all the functions or...

6 minutes read.

VBA Pivot Table Grouping

VBA- Pivot Table Grouping For an instance, if in our pivot table, we have 11 different age groups from 20 to 30 -  but there might be a possibility that we...

2 minutes read.

Excel VBA IsMissing Function

VBA IsMissing Function: The IsMissing function in VBA checks if any parameter to a procedure is missing or not. It returns a Boolean value True if the specified parameter has not been...

1 minute read.

Finding Last Row or Column in Excel VBA

Finding Last Row or Column in VBA Finding the last used row, column, or cell is one very commonly used task when we write macros and VBA applications.  Like other codes...

5 minutes read.