Awọn oniṣẹ VBA ati Awọn iṣẹ ti a ṣe sinu

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.

Awọn oniṣẹ Mathematiki

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.

onišẹActionni ayo

(1 – highest; 5 – lowest)

^exponentiation operator1
*onišẹ isodipupo2
/division operator2
Division without remainder – returns the result of dividing two numbers without a remainder. For example, 74 will return the result 13
ìgboyàModulo (remainder) operator – returns the remainder after dividing two numbers. For example, 8 Against 3 will return the result 2.4
+Oṣiṣẹ afikun5
-subtraction operator5

Awọn oniṣẹ okun

The basic string operator in Excel VBA is the concatenation operator & (merge):

onišẹAction
&concatenation operator. For example, the expression «A» & «B» will return the result AB.

Awọn oniṣẹ Ifiwera

Comparison operators are used to compare two numbers or strings and return a boolean value of type Bolianu (True or False). The main Excel VBA comparison operators are listed in this table:

onišẹAction
=Bakanna
<>Ko dogba
<Ti o kere
>Alaye diẹ
<=Kere ju tabi dọgba
>=Ti o tobi ju tabi dọgba

Logical operators

Logical operators, like comparison operators, return a boolean value of type Bolianu (True or False). The main logical operators of Excel VBA are listed in the table below:

onišẹAction
aticonjunction operation, logical operator И. For example, the expression A And B yoo pada otitọ, ti o ba A и B both are equal otitọ, otherwise return eke.
OrDisjunction operation, logical operator OR. For example, the expression A Or B yoo pada otitọ, ti o ba A or B dogba otitọ, and will return eke, ti o ba A и B both are equal eke.
koNegation operation, logical operator NOT. For example, the expression Not A yoo pada otitọ, ti o ba A bakanna eke, or return eke, ti o ba A bakanna otitọ.

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:

iṣẹAction
absReturns the absolute value of the given number.

apere:

  • Abs(-20) returns the value 20;
  • Abs(20) pada iye 20.
BẸNReturns the ANSI character corresponding to the numeric value of the parameter.

apere:

  • Chr(10) returns a line break;
  • Chr(97) returns a character a.
ọjọReturns the current system date.
DateAddAdds a specified time interval to the given date. Function syntax:

DateAdd(интервал, число, дата)

Where is the argument aarin determines the type of time interval added to the given ọjọ in the amount specified in the argument nọmba.

Ọrọ ariyanjiyan aarin can take one of the following values:

Aariniye
bẹẹniodun
qmẹẹdogun
mosù
yọjọ ti awọn ọdún
dọjọ
wọjọ ti awọn ọsẹ
wwọsẹ
hwakati
niseju
skeji

apere:

  • DateAdd(«d», 32, «01/01/2015») adds 32 days to the date 01/01/2015 and thus returns the date 02/02/2015.
  • DateAdd(«ww», 36, «01/01/2015») adds 36 weeks to the date 01/01/2015 and returns the date 09/09/2015.
DateDiffCalculates the number of specified time intervals between two given dates.

apere:

  • DateDiff(«d», «01/01/2015», «02/02/2015») calculates the number of days between 01/01/2015 and 02/02/2015, returns 32.
  • DateDiff(«ww», «01/01/2015», «03/03/2016») calculates the number of weeks between 01/01/2015 and 03/03/2016, returns 61.
DayReturns an integer corresponding to the day of the month in the given date.

apere: Day(«29/01/2015») returns the number 29.

wakatiReturns an integer corresponding to the number of hours at the given time.

apere: Hour(«22:45:00») returns the number 22.

InStrIt 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.

apere:

  • InStr(1, “Here is the search word”, “word”) returns the number 13.
  • InStr(14, “Here is the search word, and here is another search word”, “word”) returns the number 38.

akiyesi: 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.

intReturns the integer part of the given number.

apere: Int(5.79) returns result 5.

Isdatepadà otitọif the given value is a date, or eke – if the date is not.

apere:

  • IsDate(«01/01/2015») padà otitọ;
  • IsDate(100) padà eke.
IsErrorpadà otitọif the given value is an error, or eke – if it is not an error.
IsMissingThe name of an optional procedure argument is passed as an argument to the function. IsMissing padà otitọif no value was passed for the procedure argument in question.
IsNumericpadà otitọif the given value can be treated as a number, otherwise returns eke.
osiReturns the specified number of characters from the beginning of the given string. The function syntax is like this:

Left(строка, длина)

ibi ti ila is the original string, and ipari is the number of characters to return, counting from the beginning of the string.

apere:

  • Left(“abvgdejziklmn”, 4) returns the string “abcg”;
  • Left(“abvgdejziklmn”, 1) returns the string “a”.
LenReturns the number of characters in a string.

apere: Len(“abcdej”) returns the number 7.

osùReturns an integer corresponding to the month of the given date.

apere: Month(«29/01/2015») pada iye 1.

AarinReturns the specified number of characters from the middle of the given string. Function syntax:

Mid(ila, ibere, ipari)

ibi ti ila is the original string ibere – the position of the beginning of the string to be extracted, ipari is the number of characters to be extracted.

apere:

  • Mid(“abvgdejziklmn”, 4, 5) returns the string “where”;
  • Mid(“abvgdejziklmn”, 10, 2) returns the string “cl”.
MinuteReturns an integer corresponding to the number of minutes in the given time. Example: Minute(«22:45:15») pada iye 45.
bayiReturns the current system date and time.
ọtunReturns the specified number of characters from the end of the given string. Function syntax:

Right(ila, ipari)

ibi ti ila is the original string, and ipari is the number of characters to extract, counting from the end of the given string.

apere:

  • Right(«abvgdezhziklmn», 4) returns the string “clmn”;
  • Right(«abvgdezhziklmn», 1) returns the string “n”.
kejiReturns an integer corresponding to the number of seconds in the given time.

apere: Second(«22:45:15») pada iye 15.

SqrReturns the square root of the numeric value passed in the argument.

apere:

  • Sqr(4) returns the value 2;
  • Sqr(16) pada iye 4.
TimeReturns the current system time.
UboundReturns the superscript of the specified array dimension.

akiyesi: For multidimensional arrays, an optional argument may be the index of which dimension to return. If not specified, the default is 1.

odunReturns an integer corresponding to the year of the given date. Example: Year(«29/01/2015») pada iye 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.

Fi a Reply