Contents
Excel VBA statements
When writing VBA code in Excel, a set of built-in operators is used at every step. These operators are divided into mathematical, string, comparison and logical operators. Next, we will look at each group of operators in detail.
Mathematical Operators
The main VBA math operators are listed in the table below.
The right column of the table shows the default operator precedence in the absence of parentheses. By adding parentheses to an expression, you can change the order in which VBA statements are executed as you wish.
Operator | Action | priority (1 – highest; 5 – lowest) |
---|---|---|
^ | exponentiation operator | 1 |
* | multiplication operator | 2 |
/ | division operator | 2 |
Division without remainder – returns the result of dividing two numbers without a remainder. For example, 74 will return the result 1 | 3 | |
Courage | Modulo (remainder) operator – returns the remainder after dividing two numbers. For example, 8 Against 3 will return the result 2. | 4 |
+ | Addition operator | 5 |
— | subtraction operator | 5 |
String Operators
The basic string operator in Excel VBA is the concatenation operator & (merge):
Operator | Action |
---|---|
& | concatenation operator. For example, the expression «A» & «B» will return the result AB. |
Comparison Operators
Comparison operators are used to compare two numbers or strings and return a boolean value of type Boolean (True or False). The main Excel VBA comparison operators are listed in this table:
Operator | Action |
---|---|
= | Equally |
<> | Not equal |
< | Less |
> | Больше |
<= | Less than or equal |
>= | Greater than or equal |
Logical operators
Logical operators, like comparison operators, return a boolean value of type Boolean (True or False). The main logical operators of Excel VBA are listed in the table below:
Operator | Action |
---|---|
And | conjunction operation, logical operator И. For example, the expression A And B will return True, if A и B both are equal True, otherwise return False. |
Or | Disjunction operation, logical operator OR. For example, the expression A Or B will return True, if A or B are equal True, and will return False, if A и B both are equal False. |
Not | Negation operation, logical operator NOT. For example, the expression Not A will return True, if A equally False, or return False, if A equally True. |
The table above does not list all the logical operators available in VBA. A complete list of logical operators can be found at the Visual Basic Developer Center.
Built-in Functions
There are many built-in functions available in VBA that can be used when writing code. Listed below are some of the most commonly used:
Function | Action | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Abs | Returns the absolute value of the given number. Example:
| ||||||||||||||||||||||
Chr | Returns the ANSI character corresponding to the numeric value of the parameter. Example:
| ||||||||||||||||||||||
Date | Returns the current system date. | ||||||||||||||||||||||
DateAdd | Adds a specified time interval to the given date. Function syntax:
Where is the argument interval determines the type of time interval added to the given date in the amount specified in the argument number. Argument interval can take one of the following values:
Example:
| ||||||||||||||||||||||
DateDiff | Calculates the number of specified time intervals between two given dates. Example:
| ||||||||||||||||||||||
Day | Returns an integer corresponding to the day of the month in the given date. Example: Day(«29/01/2015») returns the number 29. | ||||||||||||||||||||||
hour | Returns an integer corresponding to the number of hours at the given time. Example: Hour(«22:45:00») returns the number 22. | ||||||||||||||||||||||
InStr | It takes an integer and two strings as arguments. Returns the position of occurrence of the second string within the first, starting the search at the position given by an integer. Example:
Note: The number argument may not be specified, in which case the search starts from the first character of the string specified in the second argument of the function. | ||||||||||||||||||||||
Int | Returns the integer part of the given number. Example: Int(5.79) returns result 5. | ||||||||||||||||||||||
Isdate | Returns Trueif the given value is a date, or False – if the date is not. Example:
| ||||||||||||||||||||||
IsError | Returns Trueif the given value is an error, or False – if it is not an error. | ||||||||||||||||||||||
IsMissing | The name of an optional procedure argument is passed as an argument to the function. IsMissing returns Trueif no value was passed for the procedure argument in question. | ||||||||||||||||||||||
IsNumeric | Returns Trueif the given value can be treated as a number, otherwise returns False. | ||||||||||||||||||||||
Left | Returns the specified number of characters from the beginning of the given string. The function syntax is like this:
where line is the original string, and length is the number of characters to return, counting from the beginning of the string. Example:
| ||||||||||||||||||||||
Len | Returns the number of characters in a string. Example: Len(“abcdej”) returns the number 7. | ||||||||||||||||||||||
Month | Returns an integer corresponding to the month of the given date. Example: Month(«29/01/2015») returns the value 1. | ||||||||||||||||||||||
Mid | Returns the specified number of characters from the middle of the given string. Function syntax: Mid(line, start, length) where line is the original string start – the position of the beginning of the string to be extracted, length is the number of characters to be extracted. Example:
| ||||||||||||||||||||||
Minute | Returns an integer corresponding to the number of minutes in the given time. Example: Minute(«22:45:15») returns the value 45. | ||||||||||||||||||||||
Now | Returns the current system date and time. | ||||||||||||||||||||||
Right | Returns the specified number of characters from the end of the given string. Function syntax: Right(line, length) Where line is the original string, and length is the number of characters to extract, counting from the end of the given string. Example:
| ||||||||||||||||||||||
Second | Returns an integer corresponding to the number of seconds in the given time. Example: Second(«22:45:15») returns the value 15. | ||||||||||||||||||||||
Sqr | Returns the square root of the numeric value passed in the argument. Example:
| ||||||||||||||||||||||
Time | Returns the current system time. | ||||||||||||||||||||||
Ubound | Returns the superscript of the specified array dimension. Note: For multidimensional arrays, an optional argument may be the index of which dimension to return. If not specified, the default is 1. | ||||||||||||||||||||||
year | Returns an integer corresponding to the year of the given date. Example: Year(«29/01/2015») returns the value 2015. |
This list includes only a selection of the most commonly used built-in Excel Visual Basic functions. An exhaustive list of VBA functions available for use in Excel macros can be found on the Visual Basic Developer Center.