Learn how to use all Excel VBA functions used in Macros. VBA functions are built-in functions that are used in Excel’s programming environment called Visual Basic for Applications (VBA).
Below is a list of Excel VBA functions sorted alphabetically. If you would like a list of these functions sorted by category, click on the following button:
Sort Alphabetically
(Enter a value in the field above to quickly find functions in the list below)
CHOOSE (VBA) | Returns a value from a list of values based on a given position |
ASC (VBA) | Returns ASCII value of a character |
CHR (VBA) | Returns the character based on the ASCII value |
CONCATENATE with & (VBA) | Used to join 2 or more strings together using the & operator |
FORMAT STRINGS (VBA) | Takes a string expression and returns it as a formatted string |
INSTR (VBA) | Returns the position of the first occurrence of a substring in a string |
INSTRREV (VBA) | Returns the position of the first occurrence of a string in another string, starting from the end of the string |
LCASE (VBA) | Converts a string to lowercase |
LEFT (VBA) | Extract a substring from a string, starting from the left-most character |
LEN (VBA) | Returns the length of the specified string |
LTRIM (VBA) | Removes leading spaces from a string |
MID (VBA) | Extracts a substring from a string (starting at any position) |
REPLACE (VBA) | Replaces a sequence of characters in a string with another set of characters |
RIGHT (VBA) | Extracts a substring from a string starting from the right-most character |
RTRIM (VBA) | Removes trailing spaces from a string |
SPACE (VBA) | Returns a string with a specified number of spaces |
SPLIT (VBA) | Used to split a string into substrings based on a delimiter |
STR (VBA) | Returns a string representation of a number |
STRCOMP (VBA) | Returns an integer value representing the result of a string comparison |
STRCONV (VBA) | Returns a string converted to uppercase, lowercase, proper case or Unicode |
STRREVERSE (VBA) | Returns a string whose characters are in reverse order |
TRIM (VBA) | Returns a text value with the leading and trailing spaces removed |
UCASE (VBA) | Converts a string to all uppercase |
VAL (VBA) | Returns the numbers found in a string |
DATE (VBA) | Returns the current system date |
DATEADD (VBA) | Returns a date after which a certain time/date interval has been added |
DATEDIFF (VBA) | Returns the difference between two date values, based on the interval specified |
DATEPART (VBA) | Returns a specified part of a given date |
DATESERIAL (VBA) | Returns a date given a year, month, and day value |
DATEVALUE (VBA) | Returns the serial number of a date |
DAY (VBA) | Returns the day of the month (a number from 1 to 31) given a date value |
FORMAT DATES (VBA) | Takes a date expression and returns it as a formatted string |
HOUR (VBA) | Returns the hours (a number from 0 to 23) from a time value |
MINUTE (VBA) | Returns the minutes (a number from 0 to 59) from a time value |
MONTH (VBA) | Returns the month (a number from 1 to 12) given a date value |
MONTHNAME (VBA) | Returns a string representing the month given a number from 1 to 12 |
NOW (VBA) | Returns the current system date and time |
TIMESERIAL (VBA) | Returns a time given an hour, minute, and second value |
TIMEVALUE (VBA) | Returns the serial number of a time |
WEEKDAY (VBA) | Returns a number representing the day of the week, given a date value |
WEEKDAYNAME (VBA) | Returns a string representing the day of the week given a number from 1 to 7 |
YEAR (VBA) | Returns a four-digit year (a number from 1900 to 9999) given a date value |
ABS (VBA) | Returns the absolute value of a number |
ATN (VBA) | Returns the arctangent of a number |
COS (VBA) | Returns the cosine of an angle |
EXP (VBA) | Returns e raised to the nth power |
FIX (VBA) | Returns the integer portion of a number |
FORMAT NUMBERS (VBA) | Takes a numeric expression and returns it as a formatted string |
INT (VBA) | Returns the integer portion of a number |
LOG (VBA) | Returns the natural logarithm of a number |
MOD (VBA) | Returns the remainder after a number is divided by a divisor |
RANDOMIZE (VBA) | Used to change the seed value used by the random number generator for the RND function |
RND (VBA) | Used to generate a random number (integer value) |
ROUND (VBA) | Returns a number rounded to a specified number of digits |
SGN (VBA) | Returns the sign of a number |
SIN (VBA) | Returns the sine of an angle |
SQR (VBA) | Returns the square root of a number |
TAN (VBA) | Returns the tangent of an angle |
AND (VBA) | Returns TRUE if all conditions are TRUE |
CASE (VBA) | Has the functionality of an IF-THEN-ELSE statement |
FOR…NEXT (VBA) | Used to create a FOR LOOP |
IF-THEN-ELSE (VBA) | Returns a value if a specified condition evaluates to TRUE or another value if it evaluates to FALSE |
OR (VBA) | Returns TRUE if any of the conditions are TRUE |
SWITCH (VBA) | Evaluates a list of expressions and returns the corresponding value for the first expression in the list that is TRUE |
WHILE…WEND (VBA) | Used to create a WHILE LOOP |
ENVIRON (VBA) | Returns the value of an operating system environment variable |
ISDATE (VBA) | Returns TRUE if the expression is a valid date |
ISEMPTY (VBA) | Used to check for blank cells or uninitialized variables |
ISERROR (VBA) | Used to check for error values |
ISNULL (VBA) | Used to check for a NULL value |
ISNUMERIC (VBA) | Used to check for a numeric value |
DDB (VBA) | Returns the depreciation of an asset based on the double-declining balance method |
FV (VBA) | Returns the future value of an investment |
IPMT (VBA) | Returns the interest payment for an investment |
IRR (VBA) | Returns the internal rate of return for a series of cash flows |
MIRR (VBA) | Returns the modified internal rate of return for a series of cash flows |
NPER (VBA) | Returns the number of periods for an investment |
NPV (VBA) | Returns the net present value of an investment |
PMT (VBA) | Returns the payment amount for a loan |
PPMT (VBA) | Returns the payment on the principal for a particular payment |
PV (VBA) | Returns the present value of an investment |
RATE (VBA) | Returns the interest rate for an annuity |
SLN (VBA) | Returns the depreciation of an asset based on the straight-line depreciation method |
SYD (VBA) | Returns the depreciation of an asset based on the sum-of-years’ digits depreciation method |
CHDIR (VBA) | Used to change the current directory or folder |
CHDRIVE (VBA) | Used to change the current drive |
CURDIR (VBA) | Returns the current path |
DIR (VBA) | Returns the first filename that matches the pathname and attributes specified |
FILEDATETIME (VBA) | Returns the date and time of when a file was created or last modified |
FILELEN (VBA) | Returns the size of a file in bytes |
GETATTR (VBA) | Returns an integer that represents the attributes of a file, folder, or directory |
MKDIR (VBA) | Used to create a new folder or directory |
SETATTR (VBA) | Used to set the attributes of a file |
CBOOL (VBA) | Converts a value to a boolean |
CBYTE (VBA) | Converts a value to a byte (ie: number between 0 and 255) |
CCUR (VBA) | Converts a value to currency |
CDATE (VBA) | Converts a value to a date |
CDBL (VBA) | Converts a value to a double |
CDEC (VBA) | Converts a value to a decimal number |
CINT (VBA) | Converts a value to an integer |
CLNG (VBA) | Converts a value to a long integer |
CSNG (VBA) | Converts a value to a single-precision number |
CSTR (VBA) | Converts a value to a string |
CVAR (VBA) | Converts a value to a variant |
VBA Excel Functions are most important which are used while writing procedures or macros. We have list of Functions in Excel VBA. These functions are built in functions. We can also write user defined functions as per our requirements. We have different categories of functions.
Those are VBA Date and Time Functions, VBA Text/String Functions, VBA Information Functions, VBA Logical Functions, VBA File and Directory Functions, VBA Math and Trig Functions, VBA Financial Functions and VBA Lookup and Reference Functions. Each category has list of functions to deal with different type of data. In this tutorial all functions are explained with syntax and multiple examples.
Table of Contents:
- VBA Date & Time Functions
- VBA Text/String Functions
- VBA Information Functions
- VBA File & Directory Functions
- VBA Data Type Conversion Functions
- VBA Logical Functions
- VBA Math(Mathematical) & Trig Functions
- VBA Financial Functions
- Instructions to Run VBA Macro Code
- Other Useful Resources
Here are the different categories and list of all VBA Excel Functions.
VBA Date & Time Functions:
Here are the list of VBA Date and Time Functions.
Function Name | Description |
---|---|
VBA Date | VBA Date function returns current system date. |
VBA DateAdd | VBA DateAdd function returns a date after which a certain time or date interval has been added. |
VBA DateDiff | VBA DateDiff function returns the difference between two date values, based on the interval specified. |
VBA DatePart | VBA DatePart function returns a specified part of a supplied date or time |
VBA DateSerial | VBA DateSerial function returns a date value from a supplied year, month and day. |
VBA DateValue | VBA DateValue function returns the date value for specified string input. |
VBA Day | VBA Day function returns day from date. |
VBA Hour | VBA Hour function returns hour from time. |
VBA Minute | VBA Minute function returns Minute from time. |
VBA Month | VBA Month function returns month from date. |
VBA MonthName | VBA MonthName function returns a string representing the month given a number from 1 to 12. |
VBA Now | VBA Now function returns system date and time. |
VBA Time | VBA Time function returns current system time. |
VBA TimeSerial | VBA TimeSerial function returns time. |
VBA TimeValue | VBA TimeValue function returns the date value for specified input represents date. |
VBA Weekday | VBA Weekday function returns a number representing the day of the week, given a date value. |
VBA WeekdayName | VBA WeekdayName function returns a string representing the day of the week given a number from 1 to 7. |
VBA Year | VBA Year function returns year from date. |
Click on the following link for quick reference of VBA Date & Time functions.
VBA Date & Time Functions
VBA Projects with Source Code
Help Desk Management System
Help Desk Management System is an Excel VBA Project with Full Source Code, realtime VBA Project to Manage Help desk Tickets. Explore the Code and Professional VBA Applications.
View Details
Ultimate TOC Builder
Ultimate TOC Builder is Excel VBA Project with Source Code. Real-time VBA Project to Create TOC. Explore the Code to Build Professional VBA Applications.
View Details
- Real time VBA Projects
- Access to Full VBA Source Code
- Well commented Code
VBA Text/String Functions:
Here are the list of VBA Text/String Functions.
Function Name | Description |
---|---|
VBA Asc | VBA ASC function returns the ASCII value of the first character from a string. |
VBA Chr | VBA Chr function returns a character of the ASCII value. |
VBA Format | VBA Format function returns a formatted string from a string expression. |
VBA InStr | VBA InStr function returns the position of a substring within a string. |
VBA InStrRev | VBA InStrRev function returns the position of a substring within a string, searching from right to left. |
VBA LCase | VBA LCase function converts a string to lowercase text. |
VBA Left | VBA Left function returns the specified number of characters from a given string, starting from the left most character. |
VBA Len | VBA Len function returns the length of a given string. |
VBA LTrim | VBA LTrim function removes leading spaces from a given string. |
VBA Mid | VBA Mid function returns a specified number of characters from a specified string. |
VBA Replace | VBA Replace function replaces a substring with another string in a given string. |
VBA Right | VBA Right function returns the specified number of characters from a given string, starting from the right most character. |
VBA RTrim | VBA RTrim function removes trailing spaces from a given string. |
VBA Space | VBA Space function returns a string with a specified number of spaces. |
VBA Split | VBA Split function returns an array of substrings based on a delimiter. |
VBA Str | VBA Str function returns a string which represents a number. |
VBA String | VBA String function returns a string which consists of repeated characters. |
VBA StrComp | VBA StrComp function returns an integer value representing the result of string comparision. |
VBA StrConv | VBA StrConv function converts a string to a specified format(vbUpperCase, vbLowerCase, vbUnicode, vbWide, vbNarrow, vbProperCase, etc.) |
VBA StrReverse | VBA StrReverse function reverses a given string. |
VBA Trim | VBA Trim function removes leading and trailing spaces from a given string. |
VBA UCase | VBA UCase function converts a string to uppercase text. |
Click on the following link for quick reference of VBA Text/String functions
VBA Text/String Functions
VBA Information Functions:
Here are the list of VBA Information Functions.
Function Name | Description |
---|---|
VBA ENVIRON | VBA ENVIRON function returns value of the current operating system environment variable. |
VBA IsArray | VBA IsArray function checks a specified variable is an array or not. |
VBA IsDate | VBA IsDate function checks specified Expression is date or not. It returns True if it is valid date. |
VBA IsEmpty | VBA IsEmpty function checks specified Expression is empty or not. |
VBA IsError | VBA IsError function checks specified Expression is error or not. |
VBA IsMissing | VBA IsMissing function checks specified an optional argument is missing or not. |
VBA IsNull | VBA IsNull function checks specified Expression is Null or not. |
VBA IsNumeric | VBA IsNumeric function checks specified Expression is Numeric or not. |
VBA IsObject | VBA IsObject function checks specified variable is an object or not. |
Click on the following link for quick reference of VBA Information functions
VBA Information Functions
VBA File & Directory Functions:
Here are the list of VBA File & Directory Functions.
Function Name | Description |
---|---|
VBA ChDir | VBA ChDir function changes the current folder or directory. |
VBA ChDrive | VBA ChDrive function changes the current drive. |
VBA CurDir | VBA CurDir function returns the current path. |
VBA Dir | VBA Dir function returns the first name of a file or directory that matches a pattern. |
VBA FileAttr | VBA FileAttr function returns the file mode for opened files using the open statement. |
VBA FileCopy | VBA FileCopy function copies a file from the source directory to the destination directory. |
VBA FileDateTime | VBA FileDateTime function returns the date and time when a file or directory was created or last modified. |
VBA FileLen | VBA FileLen function returns the size / length of a file in bytes. |
VBA GetAttr | VBA GetAttr function returns the attributes of a file or directory. |
VBA Kill | VBA Kill function deletes a file based on the specified Path Name. |
VBA MkDir | VBA MkDir function creates a new folder or directory . |
VBA Name | VBA Name function moves the file to the new directory or folder. |
VBA RmDir | VBA RmDir function removes an existing directory or folder. |
VBA SetAttr | VBA SetAttr function sets the attributes of a file or directory. |
Click on the following link for quick reference of VBA File & Directory functions
VBA File & Directory Functions
VBA Data Type Conversion Functions:
Here are the list of VBA Data Type Conversion Functions.
Function Name | Description |
---|---|
VBA CBool | VBA CBool function converts an expression to a Boolean data type. |
VBA CByte | VBA CByte function converts an expression to a Byte data type. |
VBA CCur | VBA CCur function converts an expression to a Currency data type. |
VBA CDate | VBA CDate function converts an expression to a Date data type. |
VBA CDbl | VBA CDbl function converts an expression to a Double data type. |
VBA CDec | VBA CDec function converts an expression to a Decimal data subtype. |
VBA CInt | VBA CInt function converts an expression to an Integer data type. |
VBA CLng | VBA CLng function converts an expression to a Long data type. |
VBA CSng | VBA CSng function converts an expression to a Single data type. |
VBA CStr | VBA CStr function converts an expression to a String data type. |
VBA CVar | VBA CVar function converts an expression to a Variant data type. |
Click on the following link for quick reference of VBA Data Type Conversion functions
VBA Data Type Conversion Functions
VBA Projects with Source Code
Help Desk Management System
Help Desk Management System is an Excel VBA Project with Full Source Code, realtime VBA Project to Manage Help desk Tickets. Explore the Code and Professional VBA Applications.
View Details
Ultimate TOC Builder
Ultimate TOC Builder is Excel VBA Project with Source Code. Real-time VBA Project to Create TOC. Explore the Code to Build Professional VBA Applications.
View Details
- Real time VBA Projects
- Access to Full VBA Source Code
- Well commented Code
VBA Logical Functions:
Here are the list of VBA Logical Functions.
Function Name | Description |
---|---|
VBA AND | VBA AND function returns true, if all the conditions are TRUE. |
VBA OR | VBA OR function returns TRUE, if any one condition is TRUE. |
VBA NOT | VBA Not function returns TRUE, if a condition is False. It returns False, if condition is TRUE. |
Click on the following link for quick reference of VBA Logical functions
VBA Logical Functions
VBA Math(Mathematical) & Trig Functions:
Here are the list of VBA Math(Mathematical) & Trig Functions.
Function Name | Description |
---|---|
VBA Abs | VBA Abs function converts a value to an absolute number. |
VBA Atn | VBA Atn function returns the arctangent of a number in radians, not degrees. |
VBA Cos | VBA Cos function returns the cosine value of an angle in radians. |
VBA Exp | VBA Exp function returns an exponential value for a specified number. |
VBA Fix | VBA Fix returns the integer portion of a number. |
VBA FormatNumber | VBA FormatNumber function converts the given expression as formatted number. |
VBA FormatCurrency | VBA FormatCurrency function converts the given expression as formatted currency. |
VBA FormatPercent | VBA FormatPercent function converts the given expression as formatted percentage. |
VBA Int | VBA Int function rounds a specified number down to an integer. |
VBA Log | VBA Log function returns the natural logarithm of a specified number. |
VBA Randomize | VBA Randomize function changes the number in RND function which is used for random number generator. |
VBA Rnd | VBA Rnd function returns random number between 0 and 1. |
VBA Round | VBA Round function rounds a number to a specified number of decimal digits. |
VBA Sgn | VBA Sgn function returns the sign of a number. |
VBA Sin | VBA Sin function returns the sine value of an angle in radians. |
VBA Tan | VBA Tan function returns the tangent value of an angle. |
VBA Sqr | VBA Sqr function returns the square root of a number. |
Click on the following link for quick reference of VBA Math(Mathematical) & Trig functions
VBA Math(Mathematical) & Trig Functions
VBA Financial Functions:
Here are the list of VBA Financial Functions.
Function Name | Description |
---|---|
VBA DDB | VBA DDB calculates the future value of an asset over a specified period of time. |
VBA FV | VBA FV function calculates the future value of an annuity. |
VBA IPmt | VBA IPmt function calculates the interest part of a payment in a specific period of regular interval time. |
VBA IRR | VBA IRR function calculates the interest rate of return for a series of unequal cash flows at regular intervals. |
VBA MIRR | VBA MIRR function calculates the modified interest rate of return for a series of unequal cash flows at regular intervals. |
VBA NPer | VBA NPer function calculates the number of periods for an annuity. |
VBA NPV | VBA NPV function calculates the net present value of an annuity. |
VBA Pmt | VBA Pmt function calculates the number of periodic payments of an annuity. |
VBA PPmt | VBA PPmt function calculates the principal amount paid in a specific period for an annuity. |
VBA PV | VBA PV function calculates the present value of an annuity. |
VBA Rate | VBA Rate function calculates the interest rate per payment period of an annuity. |
VBA SLN | VBA SLN function calculates the straight line depreciation of an asset for a single period of time. |
VBA SYD | VBA SYD function calculates the sum of years’ digits depreciation of an asset for a specific period of time. |
Click on the following link for quick reference of VBA Financial functions
VBA Financial Functions
Instructions to Run VBA Macro Code or Procedure:
You can refer the following link for the step by step instructions.
Instructions to run VBA Macro Code
Other Useful Resources:
Click on the following links of the useful resources. These helps to learn and gain more knowledge.
VBA Tutorial VBA Functions List VBA Arrays in Excel Blog
VBA Editor Keyboard Shortcut Keys List VBA Interview Questions & Answers
Once you get started with VBA, the next important thing is to learn how to use in-built VBA functions while writing a VBA code.
So here I have listed the Top 100 VBA Functions (Category Wise) in detail with examples and sample codes, and these functions are listed under specific categories (10) to make you understand the purpose of each function easily.
Below is the list of categories of the major VBA functions, all the functions are further listed under these categories:
VBA TEXT (String) Functions
VBA TEXT functions help you to manipulate text value(s) from a cell, range of cells, or from an entire worksheet or workbook. In simple words, these functions are specifically built for changing text values.
Below we have a list of 18 TEXT functions that you can learn to use:
Name | Description |
---|---|
VBA FORMAT Function | It helps you to apply a specific format and then return that string in the result. |
VBA INSTR Function | It looks for a sub-string from a string and returns the starting position of the sub-string in the result. |
VBA INSTRREV Function | It also looks for a substring from a string and returns the starting position of the sub-string in the result (looking from right to left.) |
VBA LCASE Function | It converts a string into a lowercase string where you have all the characters small. |
VBA LEFT Function | Return a substring from a string using the count of characters supplied (starting from the left side of the string). |
VBA LEN Function | It counts the number of characters from the supplied value and returns a number as the count of those characters. |
VBA LTRIM Function | It removes the leading spaces (from the left side) from a string and returns that string without those spaces in the result. |
VBA MID Function | Return a substring from a string using the starting position and count of characters supplied. |
VBA REPLACE Function | It finds a substring from a string and replaces it with another substring and returns the new string in the result. |
VBA RIGHT Function | Return a substring from a string using the count of characters supplied (starting from the right side of the string). |
VBA RTRIM Function | It removes the trailing spaces (from the right side) from a string and returns that string without those spaces in the result. |
VBA SPACE Function | It creates a string with a specified number of spaces by the user and returns it in the result. |
VBA STRCOMP Function | It compares two strings and returns the result as an integer. |
VBA STRCONV Function | It converts a string into a specific format and returns a new string in the result. |
VBA STRING Function | It takes a character and repeats it a number of times and returns it in the result as a string. |
VBA STRREVERSE Function | It reverses a string and returns it in the result. |
VBA TRIM Function | It removes the spaces from the starting and ending of a string and returns a new string in the result. |
VBA UCASE Function | It converts a string into an upper case string where you have all the characters are in the capital. |
VBA DATE functions are specifically built to deal with dates from data. With these functions, you can extract part of a date, add or subtract days from a date, and the difference between two dates.
Below we have a list of 12 DATE functions that you can learn to use:
Name | Description |
---|---|
VBA DATE Function | It returns the current date value in the result using the system’s setting. |
VBA DATEADD Function | It adds an interval to the date (and time). |
VBA DATEPART Function | It returns part (day, month, and years) from a date. |
VBA DATESERIAL Function | It creates a date by using the day, month, and year supplied. |
VBA DATEVALUE Function | It converts a date that is stored as a string or a string that represents a date into an actual date. |
VBA DAY Function | It takes the day value from a valid date and ignores the rest of the components (month and year) and returns it in the result. |
VBA FileDateTime Function | It returns the last modified date and time (as a string) of a file (workbook). |
VBA MONTH Function | It takes the month value from a valid date and ignores the rest of the components (day and year) and returns it in the result. |
VBA MONTHNAME Function | It returns the name of the month using the month number (ranging from 1 to 12). |
VBA WEEKDAY Function | It returns the weekday (ranging from 1 to 7) of a date supplied. |
VBA WEEKDAYNAME Function | It returns the name of the day using the day number (ranging from 0 to 7). |
VBA YEAR Function | It takes the year value from a valid date and ignores the rest of the components (day and month) and returns it in the result. |
VBA TIME Functions
VBA Time functions help you to work with time values and manipulate them if you want. You can extract a part from a time value, add hours, minutes, and hours to a time, and calculate the difference between two-time values.
Below we have a list of 8 TIME functions that you can learn to use:
Name | Description |
---|---|
VBA HOUR Function | It takes the hour value from a valid time and ignores the rest of the components (minutes and seconds) and returns it in the result. |
VBA MINUTE Function | It takes the minutes value from a valid time and ignores the rest of the components (hours and seconds) and returns it in the result. |
VBA NOW Function | It returns the current date and time value in the result using the system’s setting. |
VBA SECOND Function | It takes the seconds value from a valid time and ignores the rest of the components (hours and minutes) and returns it in the result. |
VBA TIME Function | It returns the current time value in the result using the system’s setting. |
VBA TIMER Function | It returns the number of seconds that have elapsed since midnight (12:00 AM). |
VBA TIMESERIAL Function | It returns a valid time value (as per VBA) by using hours, minutes, and seconds specified by the user. |
VBA TIMEVALUE Function | It takes time value from a value that has date and time and returns it in the result. |
VBA MATHS Functions
VBA MATHS functions are designed to perform specific mathematical calculations. In simple words, these functions perform calculations that are complex to perform otherwise.
Below we have a list of 13 MATHS functions that you can learn to use:
Name | Description |
---|---|
VBA ABS Function | It converts a numeric value into an absolute number and returns it in the result. |
VBA ATN Function | It calculates the arctangent of the number supplied and returns it in the result. |
VBA COS Function | It calculates the cosine of the angle supplied and returns it in the result. |
VBA EXP Function | It calculates the value of ex for the value of x supplied and returns it in the result. |
VBA FIX Function | It returns a number in the result after truncating a number to an integer. |
VBA INT Function | It takes a number and returns the integer part of it in the result as a number. |
VBA LOG Function | It calculates the natural logarithm for the natural logarithm by the user and returns it in the result. |
VBA RND Function | It returns a random number in the result ranging from 0 to 1. |
VBA ROUND Function | It rounds a number to a specific number of decimals and returns the new rounded number in the result. |
VBA SGN Function | It returns an integer representing the arithmetic sign of the number supplied. |
VBA SIN Function | It calculates the sine of the angle supplied and returns it in the result. |
VBA SQR Function | It calculates the square root of the number supplied and returns it in the result. |
VBA TAN Function | It calculates the tangent of the angle value supplied. |
VBA Logical Functions
VBA LOGICAL functions help you to test conditions and return a value if that condition and other if that value is FALSE.
Below we have 1 LOGICAL function which you can learn to use and you can also use VBA IF in your codes:
Name | Description |
---|---|
VBA IIF Function | It tests a condition and returns the specified value if the condition is TRUE and some other value (specified) if that condition is FALSE. |
VBA Information Functions
VBA INFORMATION functions help you to get specific information from the Excel application environment. You can check if a value is a date if there’s an error or if you have an object.
Below we have a list of 8 INFORMATION functions that you can learn to use:
Name | Description |
---|---|
VBA ISARRAY Function | It tests if the supplied expression is an array and returns the result as TRUE or FALSE. |
VBA ISDATE Function | It tests if the supplied expression is a date and returns the result as TRUE or FALSE. |
VBA ISEMPTY Function | It tests if the supplied expression is empty and returns the result as TRUE or FALSE. |
VBA ISERROR Function | It tests if the supplied expression is an error and returns the result as TRUE or FALSE. |
VBA ISMISSING Function | It tests if an argument for a procedure is missing and returns the result as TRUE or FALSE. |
VBA ISNULL Function | It tests if the supplied expression is NULL and returns the result as TRUE or FALSE. |
VBA ISNUMERIC Function | It tests if the supplied expression is a number and returns the result as TRUE or FALSE. |
VBA ISOBJECT Function | It tests if the supplied expression is an object and returns the result as TRUE or FALSE. |
VBA Financial Functions
VBA Financial functions help you to perform financial calculations with the values you input. You can calculate depreciation, future values, or rate of interest for investments and loans.
Below we have a list of 12 FINANCIAL functions which you can learn to use:
Name | Description |
---|---|
VBA DDB Function | It calculates depreciation on an asset for a specific period using the Double Declining Balance Method. |
VBA FV Function | It calculates the future value of a loan or investment. |
VBA IPMT Function | It calculates the interest amount of a loan or investment for a specific period. |
VBA IRR Function | It calculates the internal rate of return for a series of periodic cash flows. |
VBA MIRR Function | It calculates the modified internal rate of return for a series of periodic cash flows. |
VBA NPER Function | It calculates the number of periods for an investment or a loan. |
VBA NPV Function | It calculates the net present value of an investment. |
VBA PMT Function | It calculates the principal amount of a loan or investment for a specific period. |
VBA PV Function | It calculates the present value of a loan or investment. |
VBA RATE Function | It calculates the interest rate for a loan or investment. |
VBA SLN Function | It calculates the straight-line depreciation of an asset for a single period. |
VBA SYD Function | It calculates the sum-of-years’ digits depreciation for a specified period in the lifetime of an asset. |
VBA ARRAY Functions
VBA ARRAYS functions help you deal with arrays. You can filter values from an array, and join and split a string.
Below we have a list of 6 ARRAY functions that you can learn to use:
Name | Description |
---|---|
VBA ARRAY Function | It creates an array with a set of values supplied by you. |
VBA FILTER Function | It returns a subset from an array of strings using criteria. |
VBA JOIN Function | It joins multiple strings into one string and returns it as the result. |
VBA LBOUND Function | It returns the lowest subscript for a dimension of an array. |
VBA SPLIT Function | It splits a string into multiple strings using. |
VBA UBOUND Function | It returns the upper subscript for a dimension of an array. |
VBA Data Type Conversion Functions
VBA Data Type Conv. functions help you convert a value from one data type to another data type, like converting a number which is stored as text into a long data type.
Below we have a list of 20 Data Type Conv. functions which you can learn to use:
Name | Description |
---|---|
VBA CBOOL Function | It converts the supplied expression into the boolean data value and returns it in the result. |
VBA CBYTE Function | It converts the supplied expression into the byte data value and returns it in the result. |
VBA CCUR Function | It converts the supplied expression into the current data value and returns it in the result. |
VBA CDATE Function | It converts the supplied expression into the date data value and returns it in the result. |
VBA CDBL Function | It converts the supplied expression into the double data value and returns it in the result. |
VBA CDEC Function | It converts the supplied expression into the decimal data value and returns it in the result. |
VBA CHR Function | It returns the character using a character code supplied. |
VBA CINT Function | It converts the supplied expression into the integer data value and returns it in the result. |
VBA CLNG Function | It converts the supplied expression into the long data value and returns it in the result. |
VBA CSNG Function | It converts the supplied expression into a single data value and returns it in the result. |
VBA CSTR Function | It converts the supplied expression into the string data value and returns it in the result. |
VBA CVAR Function | It converts the supplied expression into the variant data value and returns it in the result. |
VBA FORMATCURRENCY Function | It applies the currency format to the supplied expression and returns it in the result. |
VBA FORMATDATETIME Function | It applies the date format to the supplied expression and returns it in the result. |
VBA FORMATNUMBER Function | It applies the number format to the supplied expression and returns it in the result. |
VBA FORMATPERCENT Function | It applies the percentage format to the supplied expression and returns it in the result. |
VBA HEX Function | It returns a string by converting a numeric value to hexadecimal notation. |
VBA OCT Function | It returns a string by converting a numeric value into an octal notation |
VBA STR Function | It converts a numeric value into a string and returns the result. |
VBA VAL Function | It converts a string into a numeric value and it returns it in the result. |
VBA Error Handling Functions
VBA Error Handling functions help you to get information about the error that occurs in a VBA code. Below we have 1 Error Handling function which you can learn to use and you can also use error handling techniques in your codes:
Name | Description |
---|---|
VBA CVERR Function | It returns an error data type by using the error data number supplied. |
Excel VBA Functions
We have seen that we can use the worksheet functions in VBA, i.e., the Excel worksheet functions in VBA coding using the Application.Worksheet method. But how do we use a function of VBA in Excel well? Such functions are called user-defined functions. When a user creates a function in VBA, we can use it in an Excel worksheet.
Although we have many functions in Excel to manipulate the data, sometimes we need to have some customization in the tools to save time as we do some tasks repeatedly. For example, we have predefined functions in excel like SUM, COUNTIFThe COUNTIF function in Excel counts the number of cells within a range based on pre-defined criteria. It is used to count cells that include dates, numbers, or text. For example, COUNTIF(A1:A10,”Trump”) will count the number of cells within the range A1:A10 that contain the text “Trump”
read more, SUMIFThe SUMIF Excel function calculates the sum of a range of cells based on given criteria. The criteria can include dates, numbers, and text. For example, the formula “=SUMIF(B1:B5, “<=12”)” adds the values in the cell range B1:B5, which are less than or equal to 12.
read more, COUNTIFS,”COUNTIFSread more VLOOKUPThe VLOOKUP excel function searches for a particular value and returns a corresponding match based on a unique identifier. A unique identifier is uniquely associated with all the records of the database. For instance, employee ID, student roll number, customer contact number, seller email address, etc., are unique identifiers.
read more, INDEX, MATCH in excelThe MATCH function looks for a specific value and returns its relative position in a given range of cells. The output is the first position found for the given value. Being a lookup and reference function, it works for both an exact and approximate match. For example, if the range A11:A15 consists of the numbers 2, 9, 8, 14, 32, the formula “MATCH(8,A11:A15,0)” returns 3. This is because the number 8 is at the third position.
read more, etc., but we do some tasks daily for which a single command or function is not available in Excel. Using VBA, we can create the User Defined Functions (UDFUser Defined Function in VBA is a group of customized commands created to give out a certain result. It is a flexibility given to a user to design functions similar to those already provided in Excel.read more) custom function.
Table of contents
- Excel VBA Functions
- What do VBA Functions do?
- How to Create Custom Functions using VBA?
- Example
- Step 1: Find Total Marks
- Step 2: Create ResultOfStudent Function
- Step 3: Apply ResultOfStudents Function to Get Result
- Step 4: Create ‘GradeForStudent’ Function to get Grades
- Example
- Recommended Articles
What do VBA Functions do?
- They carry out certain calculations; and
- Return a value
In VBA, while defining the function, we use the following syntax to specify the parameters and their data type.
Data type here is the type of dataData type is the core character of any variable, it represents what is the type of value we can store in the variable and what is the limit or the range of values which can be stored in the variable, data types are built-in VBA and user or developer needs to be aware which type of value can be stored in which data type. Data types assign to variables tells the compiler storage size of the variable.read more the variable will hold. It can hold any value (data type or class object).
We can connect the object with its property or method using the period or dot (.) symbol.
How to Create Custom Functions using VBA?
You can download this VBA Function Excel Template here – VBA Function Excel Template
Example
Suppose we have the following data from a school where we need to find the total marks scored by the student, result, and grade.
To sum up, the marks scored by an individual student in all subjects, we have an inbuilt function, SUM. But, determining the grade and result based on the criteria set out by the school is not available in Excel by default.
It is the reason why we need to create user-defined functions.
Step 1: Find Total Marks
First, we will find the total marks using the SUM function in excelThe SUM function in excel adds the numerical values in a range of cells. Being categorized under the Math and Trigonometry function, it is entered by typing “=SUM” followed by the values to be summed. The values supplied to the function can be numbers, cell references or ranges.read more.
Then, press the “Enter” key to get the result.
Drag the formula to the rest of the cells.
Now, to find out the result (passed, failed, or essential repeat), the criteria set by the school is that.
- Suppose a student has scored more than or equal to 200 as total marks out of 500. In addition, suppose they have also not failed in any subject (scored more than 32 in each subject), a student is passed.
- If the student scored more than or equal to 200 but failed in 1 or 2 subjects, then a student will get “Essential Repeat” in those subjects.
- If the student has scored either less than 200 or fails in 3 or more subjects, then the student failed.
Step 2: Create ResultOfStudent Function
To create a function named ‘ResultOfStudent,’ we need to open “Visual Basic Editor” by using any of the methods below:
- Using the Developer tab excel.Enabling the developer tab in excel can help the user perform various functions for VBA, Macros and Add-ins like importing and exporting XML, designing forms, etc. This tab is disabled by default on excel; thus, the user needs to enable it first from the options menu.read more
If the Developer tab is not available in MS Excel, then we can get that by using the following steps:
- Right-click anywhere on the ribbon. Then, choose to Customize the Ribbon in excelRibbons in Excel 2016 are designed to help you easily locate the command you want to use. Ribbons are organized into logical groups called Tabs, each of which has its own set of functions.read more‘.
When we choose this command, the “Excel Options” dialog box opens.
- We need to check the box for “Developer” to get the tab.
- Using the shortcut key, Alt+F11.
- When we open the VBA editorThe Visual Basic for Applications Editor is a scripting interface. These scripts are primarily responsible for the creation and execution of macros in Microsoft software.read more, we need to insert the module by going to the Insert menu and choosing a module.
- We need to paste the following code into the module.
Function ResultOfStudents(Marks As Range) As String Dim mycell As Range Dim Total As Integer Dim CountOfFailedSubject As Integer For Each mycell In Marks Total = Total + mycell.Value If mycell.Value < 33 Then CountOfFailedSubject = CountOfFailedSubject + 1 End If Next mycell If Total >= 200 And CountOfFailedSubject <= 2 And CountOfFailedSubject > 0 Then ResultOfStudents = "Essential Repeat" ElseIf Total >= 200 And CountOfFailedSubject = 0 Then ResultOfStudents = "Passed" Else ResultOfStudents = "Failed" End If End Function
The above function returns the result for a student.
We need to understand how this code is working.
The first statement, ‘Function ResultOfStudents(Marks As Range) As String,’ declares a function named ‘ResultOfStudents’ that will accept a range as input for marks and return the result as a string.
Dim mycell As Range Dim Total As Integer Dim CountOfFailedSubject As Integer
These three statements declare variables, i.e.,
- ‘myCell’ as a Range,
- ‘Total’ as Integer (to store total marks scored by a student),
- ‘‘CountOfFailedSubject’ is an Integer (to store the number of subjects a student has failed).
For Each mycell In Marks Total = Total + mycell.Value If mycell.Value < 33 Then CountOfFailedSubject = CountOfFailedSubject + 1 End If Next mycell
This code checks for every cell in the ‘Marks’ range. It adds the value of every cell in the ‘Total’ variable. If the cell’s value is less than 33, add 1 to the ‘CountOfFailedSubject’ variable.
If Total >= 200 And CountOfFailedSubject <= 2 And CountOfFailedSubject > 0 Then ResultOfStudents = "Essential Repeat" ElseIf Total >= 200 And CountOfFailedSubject = 0 Then ResultOfStudents = "Passed" Else ResultOfStudents = "Failed" End If
This code checks the value of ‘Total’ and ‘CountOfFailedSubject’ and passes the ‘Essential Report,’ ‘Passed,’ or ‘Failed’ to the ‘ResultOfStudents.’
Step 3: Apply ResultOfStudents Function to Get Result
ResultOfStudents function takes marks, i.e., selecting 5 marks scored by the student.
Now, select the range of cells, B2: F2.
Drag the formula to the rest of the cells.
Step 4: Create ‘GradeForStudent’ Function to get Grades
Now to find out the grade for the student, we will create one more function named ‘GradeForStudent.’
The code would be:
Function GradeForStudent(TotalMarks As Integer, Result As String) As String If TotalMarks > 440 And TotalMarks <= 500 And ((Result = "Passed" Or Result = "Essential Repeat") Or Result = "Essential Repeat") Then GradeForStudent = "A" ElseIf TotalMarks > 380 And TotalMarks <= 440 And (Result = "Passed" Or Result = "Essential Repeat") Then GradeForStudent = "B" ElseIf TotalMarks > 320 And TotalMarks <= 380 And (Result = "Passed" Or Result = "Essential Repeat") Then GradeForStudent = "C" ElseIf TotalMarks > 260 And TotalMarks <= 320 And (Result = "Passed" Or Result = "Essential Repeat") Then GradeForStudent = "D" ElseIf TotalMarks >= 200 And TotalMarks <= 260 And (Result = "Passed" Or Result = "Essential Repeat") Then GradeForStudent = "E" ElseIf TotalMarks < 200 Or Result = "Failed" Then GradeForStudent = "F" End If End Function
This function assigns a ‘Grade’ to the student based on the ‘Total Marks’ and ‘Result.’
We need to write the formula and open the brackets in cell H2. Press Ctrl+Shift+A to find out the arguments.
The GradeForStudent function takes Total marks (sum of marks) and the result of the student as an argument to calculate the grade.
Now, select the individual cells, G2 and H2.
Now, we need to press Ctrl+D after selecting the cells to copy down the formulas.
We can highlight the values of less than 33 with the red background color to find out the subjects in which the student failed.
Recommended Articles
This article is a guide to VBA Functions. Here, we discuss creating custom functions using VBA code, practical examples, and a downloadable Excel template. You may learn more about VBA from the following articles:-
- VBA IIFThe «VBA IIF» condition evaluates the supplied expression or logical test and returns TRUE or FALSE as a result.read more
- SUMIF With VLOOKUPSUMIF is used to sum cells based on some condition, which takes arguments of range, criteria, or condition, and cells to sum. When there is a large amount of data available in multiple columns, we can use VLOOKUP as the criteria.read more
- Excel SUMIF Between Two DatesWhen we wish to work with data that has serial numbers with different dates and the condition to sum the values is based between two dates, we use Sumif between two dates. read more
- Excel VBA Delete RowIn VBA, to delete the command and to delete any rows together, the trick is that we give a single row reference if we need to delete a single row. However, for the multiple columns, we can provide numerous row references.read more