VBA Comparison Operators. VB logical operators Vba negation

These operators compare two expressions to determine if they are equal, and if not, how they differ. Is, IsNot, and Like are discussed in detail on separate help pages. Comparison operator relationships are discussed on this page.

Result \u003d expression1 comparisonoperator expression2 result \u003d object1 object2 result \u003d string Like pattern

result
Mandatory. The result is a Boolean value representing the result of the comparison.

expression
Mandatory. Arbitrary expression.

comparisonoperator
Mandatory. Any relational comparison operator.

object1, object2
Mandatory. The name of any referenced object.

string
Mandatory. An arbitrary expression of type String.

pattern
Mandatory. Any String expression or range of characters.

The following table provides a list of comparison operators and conditions that determine whether result is True or False.

Note

When comparing strings, string expressions are evaluated based on their alphabetical sort order, which depends on Option Compare.

Option Compare Binary specifies a method for comparing strings based on the sort order determined by the internal binary representation of characters. The sort order is determined by the code page. The following example shows a typical binary sort order.

A< B < E < Z < a < b < e < z < À < Ê < Ø < à < ê < ø

Option Compare Text specifies the method for comparing strings based on the text sort order (case insensitive) determined by the regional settings of the application. When you set Option Compare Text and sort the characters, the previous example applies the following text sort order:

(A \u003d a)< (À= à) < (B=b) < (E=e) < (Ê= ê) < (Ø = ø) < (Z=z)

Locale dependent

When Option Compare Text is set, the result of string comparison may depend on the locale in which the application is running. Two characters in one locale can be considered equal, but not in the other. Locale sensitivity should be considered when using string comparison to make important decisions, such as whether to allow a login attempt. Consider setting Option Compare Binary or calling that is locale aware.

Comparison operators with Object expressions are not allowed with Option Strict On. When Option Strict is Off and either expression1 or expression2 is Object, the run-time types determine how they are compared. The following table shows the comparison of expressions and the comparison results depending on the type of the operands determined at runtime:

A numeric comparison evaluates Nothing to 0. A string comparison treats Nothing as "" (empty string).

Comparison operators (< . <= , > , >= , = , <>) can be overwhelmed; this means that a class or structure can override its behavior when the operand is of the type of that class or structure. If your code uses these operators for such a class or structure, make sure you understand its overridden behavior. For more information see

All arithmetic, comparison, logical, and concatenation operators are detailed here. I tried to collect everything there is to know about these operators. Each operator is provided with an example. The descriptions were translated by me from the standard Help "a VB5.

Arithmetic operators

operator ^ (exponentiation)

This operator is for raising a number to a power.

Syntax:

result = number^ power

Options:

result
number
power - is required; any numeric expression

Remarks:

number can be negative only if power is an integer. If multiple ^ operators are used in one expression, then the evaluation occurs from left to right. Typically, the result type is Double. However, if or power, or number - Null expression, then result also Null.

Example:

Dim MyValue
MyValue \u003d 2 ^ 2 "Will return 4.
MyValue \u003d 2 ^ 3 ^ 3 " Will refund 512 (2^3=8, 8^3=512)
MyValue \u003d (-5) ^ 3 " Will refund -125.

Advice:

If you need to raise a number to a constant power, then it is better to use several operators - multiplications than one - exponentiation, judge for yourself, a loop of a million passes with the calculation of the expression went through:

test1 \u003d 2 ^ 8 "893ms
test1 \u003d 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 "130ms!

operator * (multiplication)

Used to multiply two numbers.

Syntax:

result = number1* number2

Options:

result - required; any numeric variable
number1 - required; any numeric expression
number2 - required; any numeric expression

Notes:

The data type of the result is usually the same as the most precise type of two numbers. The order of precision, from highest to lowest - Byte, Integer, Long, Single, Currency, Double, Decimal. True, there are exceptions:

  • If Single and Long are multiplied, then the result is Double
  • If data type result - Long Single or Date, which does not fit the result of the expression itself, then the result is converted to a Variant containing Double.

If a number1 or number2

The order of precision in multiplication is different from that used in addition and subtraction.

Example:

Dim MyValue
MyValue \u003d 2 * 2 "Will return 4.
MyValue \u003d 459.35 * MyValue "(! LANG: Returns 495.35 * 4

operator / (division)

Used to divide two numbers and get a floating point result.

Syntax:

result = number1 / number2

Options:

result - required; any numeric variable
number1 - required; any numeric expression
number2 - required; any numeric expression

Notes:

Resultusually of type Double. True, there are exceptions:

  • If both expression expressions are of type Byte, Integer, Single, then the result is Single. However, if the dimensions of the expression do not fit within the Single frame, an error occurs.
  • If both expressions of the expression are of type Variant containing Byte, Integer, or Single, then the result is a Single Variant. However, if the dimensions of the expression do not fit within the Single, then Double Variant.
  • If one of the numbers is of type Decimal, then the result is also Decimal.

If a number1 or number2 - Null, then it is simply interpreted as a normal 0.

Examples:

Dim MyValue
MyValue \u003d 10/4" !} Will refund 2.5.
MyValue \u003d 10/3 " Will refund 3.333333.

operator \\ (integer division)

Used to divide two numbers and get an integer result.

Syntax:

result = number1 \ number2

Options:

result - required; any numeric variable
number1 - required; any numeric expression
number2 - required; any numeric expression

Notes:

Before such division occurs, expressions are rounded to Byte, Integer, or Long expressions. Typically, the result data type is Byte, Byte variant, Integer, Integer variant, Long, or Long variant. Any fractional part is truncated.

However, if any of the expressions are Null, then the result is Null. Any expression containing Empty is interpreted as 0.

Examples:

Dim MyValue
MyValue \u003d 11 \\ 4 " Will refund 2.
MyValue \u003d 9 \\ 3 " Will refund 3.
MyValue \u003d 100 \\ 3 " Will refund 33.

operator Mod (remainder of division)

Used to divide two numbers and get the remainder of their division.

Syntax:

result = number1 Mod number2

Options:

result - required; any numeric variable
number1 - required; any numeric expression
number2 - required; any numeric expression

Notes:

When dividing, numbers with the right dot are rounded. For example, the result of the following expression is 5:

A \u003d 19 Mod 6.7

What's going on here? First, the number 6.7 is rounded to 7. Then the division takes place, we get 2.7 .... The remainder of the division \u003d 5. (2 * 7 \u003d 14, 19 - 14 \u003d 5).

The result is usually of type Byte, Byte variant, Integer, Integer variant, Long, or a Variant containing a Long.

If any of the expressions are Null, then the result is Null. Any expression containing Empty is interpreted as 0.

Examples:

Dim MyResult
MyResult \u003d 10 Mod 5 " Will refund 0.
MyResult \u003d 10 Mod 3 " Will refund 1.
MyResult \u003d 12 Mod 4.3 " Will refund 0.
MyResult \u003d 12.6 Mod 5 " Will refund 3.

operator + (addition)

Used to add two numbers.

Syntax:

result = expression1+ expression2

Options:

result - required; any numeric variable
expression1
expression2 - required; any expression

Notes:

When you use the + operator, you cannot determine what happens, addition or string concatenation. Use the & operator for concatenation to avoid confusion and make your code more readable.

If one of the expressions is not Variant, then the following rules apply:

  • If both expressions are of a numeric type (Byte, Boolean, Integer, Long, Single, Double, Date, Currency, or Decimal), then they are added.
  • If both expressions are string - concatenation.
  • If one of the expressions is of a numeric type and the other is any Variant value, including Null, then addition occurs.
  • If one of the expressions is a string and the other is any Variant value, then concatenation occurs.
  • If one of the expressions contains Empty, then the second, unchanged expression is returned.
  • If one of the expressions is of a numeric type and the other is a string, a Type mismatch error occurs.
  • If any expression is Null, then the result is also Null.

If both expressions are Variant, then the following rules apply:

  • If both expressions are numbers, then they are added.
  • If both expressions are strings, then they are concatenated.
  • If one of the expressions is a number and the other is a string, addition occurs.

For normal addition, the data type of the result is usually the same as the most precise type of two numbers. The order of precision is Byte, Integer, Long, Single, Double, Currency, and Decimal. There are exceptions:

  • If Single and Long are added, then the result is Double
  • If an expression with the Date type is added, with any other expression, then the result is Date.

If one or both expressions are Null, then the result is also Null. If both of the expressions contain Empty, the result is Integer. If only one, then the unchanged second expression is returned as the result.

Examples:

Dim MyNumber, Var1, Var2
MyNumber \u003d 2 + 2 " Will refund 4.
MyNumber \u003d 4257.04 + 98112 " Will refund 102369.04.

Var1 \u003d "34"
Var2 \u003d 6 "Initializing mixed variables
MyNumber \u003d Var1 + Var2 " Will refund 40.

Var1 \u003d "34"
Var2 \u003d "6" "Initializing variables with strings
MyNumber \u003d Var1 + Var2 " Will refund "346" (occurred
"concatenation, not addition!).

Advice:

The addition operator (+) can be used to add dates, i.e. variables of type Date:

Dim d As Date
d \u003d DateSerial (2002, 8, 15) "date initialization 08/15/2002
d \u003d d + 15 "now d contains the date 30.08.2002
"ie we have added 15 days

operator - (subtraction, sign change)

Used to find the difference between two numbers, or also to change the sign of an expression.

Syntax:

result = expression1- expression2

-expression

Options:

result - required; any numeric variable
expression - required; any expression
expression1 - required; any expression
expression2 - required; any expression

Remarks:

In the first syntax, the "-" operator is needed to find the difference between two numbers. In the second syntax, "-" is used to change the sign of expressions.

The data type of the result is usually the same as the most precise type of two numbers. The order of precision is Byte, Integer, Long, Single, Double, Currency, and Decimal. There are exceptions:

  • If the types Single and Long are involved in the subtraction, then the result is Double
  • If a Date expression is used in the subtraction,
    then the result is Date.
  • Subtracting two dates results in Double.

If one or both expressions are Null, then the result is also Null. If one of the expressions is Empty, then it is interpreted as 0.

The order of precision in addition and subtraction is different from those used in multiplication.

Examples:

Dim MyResult
MyResult \u003d 4 - 2 " Will refund 2.
MyResult \u003d 459.35 - 334.90 " Will refund 124.45.

Advice:

Like the addition operator, the subtraction operator can be used to calculate the difference (in days) between two dates:

Dim d1 As Date
Dim d2 As Date
Dim razn As Long
d1 \u003d DateSerial (1983, 10, 14)
d2 \u003d DateSerial (2002, 8, 15)
razn \u003d d2 - d1 "difference in days (6880).

Comparison Operators

Used to compare some expressions. They have 3 syntaxes:

Syntax:

result = expression1 comparison operator expression2
result = object1 Is object2
result = line Like sample

Options:

result required; any numeric variable
expression required; any expression
comparison operator required; any comparison operator
an object required; the name of any object
line required; any string expression.
sample required; any string expression, or a range of letters and numbers

Notes:

The following table contains a list of comparison operators and conditions that determine the result of an expression (True or False).

Operator

True if

False if

Null if

< (меньше чем) expression1 <
expression2
expression1 >=
expression2

one thing
of
expressions contains Null

<= (меньше или равно) expression1 <=
expression2
expression1 >
expression2
\u003e (greater than) expression1 >
expression2
expression1 <=
expression2
\u003e \u003d (greater or equal) expression1 >=
expression2
expression1 <
expression2
\u003d (equal) expression1 =
expression2
expression1 <>
expression2
<> (not equal) expression1 <>
expression2
expression1 =
expression2

The Is and Like operators perform specific functions, and their comparison table is different from the one shown below (we will discuss them below).

When comparing two expressions, it is not always possible to determine whether the comparison will be numbers or strings. The following is how the result will be evaluated if both expressions are of a type other than Variant:

  • If both expressions are numbers (Byte, Boolean, Integer, Long, Single, Double, Date, Currency, or Decimal), then the numbers are compared.
  • If both expressions are strings, then the strings are compared. (the smaller line is the one, the first and subsequent letters of which have a smaller ascii code).
  • If one of the expressions is a number and the other is Variant, which can be interpreted as a number, then the numbers are compared.
  • If one of the expressions is a number and the other is a Variant string that cannot be interpreted as a number, an error occurs (Type mismatch).
  • If one of the expressions is a string and the other is any Variant value (even Null), then a string comparison occurs.
  • If one of the expressions is Empty and the other is a number, then a number comparison is performed, where Empty is treated as 0.
  • If one of the expressions is Empty and the other is a string, then a string comparison is performed, where Empty is treated as an empty string "".

If both the first expression and the second are of type Variant, then the expressions are compared according to the data types that the Variant contains:

  • If both Variant expressions contain numbers, then the numbers are compared.
  • If both Variant expressions contain strings, then the strings are compared.
  • If one of the Variant expressions contains a number and the other a string, then the numeric expression is less than the string expression.
  • If one of the Variant expressions is Empty and the other is a number, then Empty is treated as 0.
  • If one of the Variant expressions is Empty and the other is a string, then Empty is treated as the empty string "".
  • If both expressions are Empty, then they are considered equal.

When a Single variable is compared to a Double, the Double is rounded to the precision of a Single.

If Currency is compared to Single or Double, then Single or Double is converted to Currency. Likewise, when comparing Decimal with Single or Double, then Single or Double are converted to Decimal. For Currency, any fractional part less than .0001 may be lost. For Decimal, the value is 1E-28, or an error may occur. Thus, with the loss of the fractional part, the expressions can be interpreted as equal, although in fact, one will differ from the other. (albeit a small value).

Examples:

Dim MyResult, Var1, Var2
MyResult \u003d (45< 35) " Will refund False.
MyResult \u003d (45 \u003d 45) " Will refund True.
MyResult \u003d (4<> 3) " Will refund True.
MyResult \u003d ("5"\u003e "4") " Will refund True.

Var1 \u003d "5": Var2 \u003d 4 "in VB you can use a colon,
"to separate operators.

MyResult \u003d (Var1\u003e Var2) " Will refund True.

Var1 \u003d 5: Var2 \u003d Empty
MyResult \u003d (Var1\u003e Var2) " Will refund True.

Var1 \u003d 0: Var2 \u003d Empty
MyResult \u003d (Var1 \u003d Var2) " Will refund True.

comparison operator Is

This operator is used to compare object variables.

The syntax for this operator is shown above.

Notes:

If object1 and object1 refer to the same object, then the result is True, if not, then False. Two variables can refer to the same object in multiple ways. In the following example, A refers to the same object as B:

Set A \u003d B

The following example makes it so that the variables A and B refer to the same object - C:

Set A \u003d C
Set B \u003d C

Examples:

Dim MyObject, YourObject, ThisObject, _
OtherObject, ThatObject, MyCheck

Set YourObject \u003d MyObject "create links to objects
Set ThisObject \u003d MyObject
Set ThatObject \u003d OtherObject
MyCheck \u003d YourObject Is ThisObject "Will return True.
MyCheck \u003d ThatObject Is ThisObject "Will return False.
"We assume that MyObject<> OtherObject
MyCheck \u003d MyObject Is ThatObject "Will return False.

string comparison operator - Like

The Like string comparison operator is used to compare strings.

The syntax for this operator has already been discussed above.

Comment:

This operator can be used to test String against Pattern. It is a very powerful operator, almost analogous to regular expressions in Perl.

So, this operator works as follows. If the string matches the mask, then the result is True. If not, False. If one of the expressions is Null, the result is also Null.

The behavior of the Like operator depends on the default string comparison type. (Option Compare statement).

If the Binary type is set (i.e. binary comparison), then the strings are compared according to their Ascii codes (in different encodings it is different). This sequence is usually used:

A< B < E < Z < a < b < e < z < А < К < Я < а < к < я

If the type is set to Text (text comparison). With this comparison, the sequence differs from the previous one, here the upper and lower letters are equal:

(A \u003d a)< (А=а) < (B=b) < (E=e) < (К=к) < (Z=z) < (Я=я)

The most important function of the Like operator is to check if a string belongs to a mask. The following specials can be used in the mask. symbols:

Any single character
* Zero or more characters
# Any digit (0-9).
Any single character that appears in the charlist
[! charlist] Any single character not included in the charlist

Here's a quick note. In order to check the belonging of a line on a mask containing special. symbols (ie check, for example, if there are symbols [,?, #,], * in the string), then you need to enclose them in square brackets. You can't just put a separate parenthesis [or].

When specifying a list of characters, you can use a dash (-). For example, to specify a sequence from A to Z, you would use a mask. Everything in the brackets must not contain any separators (spaces, commas, etc.), otherwise they will also be included in the sequence.

There are other important rules for mask checking:

  • (!) sign at the beginning of the list of symbols indicates that you need to search for symbols that are not included in this list. If you need to find the! Sign itself, you need to put brackets [!].
  • (-) is used to specify a range of characters.
  • When a range of characters is specified, it must be ASCII ascending. Those. correct mask, not.
  • The sequence is interpreted as an empty string "".

Examples:

Dim MyCheck
MyCheck \u003d "aBBBa" Like "a * a" "Will return True.
MyCheck \u003d "F" Like "" "Will return True.
MyCheck \u003d "F" Like "[! A-Z]" "Will return False.
MyCheck \u003d "a2a" Like "a # a" "Will return True.
MyCheck \u003d "aM5b" Like "a # [! C-e]" "Will return True.
MyCheck \u003d "BAT123khg" Like "B? T *" "Will return True.
MyCheck \u003d "CAT123khg" Like "B? T *" "Will return False.

myString \u003d "312T-87GD-8922"

If myString Like "### - ## - ####" Then ...

String Concatenation Operators

Actually, you can use only 2 operators to concatenate strings in Visual Basic. These are & and +. The + operator is described above. Let's talk about the & operator.

string concatenation operator - &

Used to concatenate two expressions.

Syntax:

result = expression1 & expression2

result required; Any String or Variant variable
expression1 required; Any expression
expression2 required; Any expression

Notes:

If the expression is not a string, then it is converted to a String Variant. The result data type is String only when both expressions are of type String. Otherwise, the result is a String Variant. If both expressions are Null, then the result is also Null. However, if only one of the expressions contains a Null value, then it is interpreted as an empty string "". Empty is also interpreted as the empty string "".

Examples:

Dim MyStr
MyStr \u003d "Hello" & "World"
" Will refund the string "Hello World".
MyStr \u003d "CHECK" & 123 & "CHECK"
"Will return the string" CHECK 123 CHECK ".

Logical operators

This is the most interesting group by the operator. When programming, you must know how they work and how they are used (and not only in Visual Basic).

There are 6 of them in Visual Basic. Let's consider each operator in detail.

And operator

Used to perform logical multiplication on two expressions.

Syntax:

result = expression1 And expression2

result
expression1 required; Any expression
expression2 required; Any expression

Notes:

The following table shows how the And operator works:

If expression1 \u003d

, and expression2 \u003d

That result \u003d

True True True
True False False
True Null Null
False True False
False False False
False Null False
Null True Null
Null False False
Null Null Null

The And operator is also used to test the bits of a number. For bits, the And operator works as follows (look from left to right)

0 0 0
0 1 0
1 0 0
1 1 1

Examples:

Dim A, B, C, D, MyCheck
MyCheck \u003d A\u003e B And B\u003e C " Will refund True.
MyCheck \u003d B\u003e A And B\u003e C " Will refund False.
MyCheck \u003d A\u003e B And B\u003e D " Will refund Null.
MyCheck \u003d A And B " Will refund 8 (bitwise comparison).

Let's take a closer look at the last example. The number 10 is represented as bits as follows (as a tetrad, i.e. 4 bits):

And the number 8 is like this:

As a result of the And operator, according to the above table, we get:

Those. 8. Why did we do it? We did this in order to check if the fourth bit of the number A is set? When we got B, we made sure that this bit is set.

Or operator

Used to perform a logical addition of two expressions.

Syntax:

result = expression1 Or expression2

result required; Any numeric (including Boolean) variable
expression1 required; Any expression
expression2 required; Any expression

Notes:

The following table shows how the Or operator works:

If expression1 \u003d

, and expression2 \u003d

That result \u003d

True True True
True False True
True Null True
False True True
False False False
False Null Null
Null True True
Null False Null
Null Null Null

The Or operator is used to set specific bits of a number. For bits, the Or operator works as follows (look from left to right)

0 0 0
0 1 1
1 0 1
1 1 1

The result is in bold.

Examples:

Dim A, B, C, D, MyCheck
MyCheck \u003d A\u003e B Or B\u003e C " Will refund True.
MyCheck \u003d B\u003e A Or B\u003e C " Will refund True.
MyCheck \u003d A\u003e B Or B\u003e D " Will refund True.
MyCheck \u003d B\u003e D Or B\u003e A " Will refund Null.
MyCheck \u003d A Or 5 " Will refund 15:

Let's take a closer look at the last example. The number 10 is represented as bits in the following way (as a tetrad, i.e. 4 bits):

And number 5:

As a result of the operation of the Or operator, according to the above table, we get:

Those. 15. As you can see, the Or operator is very easy and convenient to use not only in comparison expressions, but also to set certain bits of a number.

Xor operator

Used to perform logical negation of two expressions.

Syntax:

result = expression1 Xor expression2

result required; Any numeric (including Boolean) variable
expression1 required; Any expression
expression2 required; Any expression

Notes:

The following table shows how the Xor operator works:

The Xor operator is used to invert certain bits of a number. For bits, the Xor operator works as follows (look from left to right)

0 0 0
0 1 1
1 0 1
1 1 0

The result is in bold. Xor differs from Or, only in that when both bits are ones, Xor produces 0. The Xor operator is interesting for its property, but when it is used twice, it produces the same number. This is often used in cryptography.

Examples:

Dim A, B, C, D, MyCheck
A \u003d 10: B \u003d 8: C \u003d 6: D \u003d Null
MyCheck \u003d A\u003e B Xor B\u003e C " Will refund False.
MyCheck \u003d B\u003e A Xor B\u003e C " Will refund True.
MyCheck \u003d B\u003e A Xor C\u003e B " Will refund False.
MyCheck \u003d B\u003e D Xor A\u003e B " Will refund Null.
MyCheck \u003d A Xor B " Will refund 2

An interesting example of the use of the Xor operator is the exchange of values \u200b\u200bof two numeric variables:

Dim a As Long, b As Long
a \u003d 4
b \u003d 7
a \u003d a Xor b
b \u003d a Xor b
a \u003d a Xor b

Now variable a contains the value of variable b, and vice versa.

operator Not

Used to perform logical inversion of two expressions.

Syntax:

result \u003d Not expression

result required; Any numeric (including Boolean) variable
expression required; Any expression

Notes:

The following table shows how the Not operator works:

The Not operator inverts all bits expressions.For bits, the Not operator works as follows (looking from left to right):

0 1
1 0

The result is in bold.

Examples:

Dim A, B, C, D, MyCheck
A \u003d 10: B \u003d 8: C \u003d 6: D \u003d Null
MyCheck \u003d Not (A\u003e B) " Will refund False.
MyCheck \u003d Not (B\u003e A) " Will refund True.
MyCheck \u003d Not (C\u003e D) " Will refund Null.
MyCheck \u003d Not A " Will refund -11 (all bits are inverted)

Let's take a closer look at the last example. The number 10 is represented as bits as follows (as a byte, i.e. 8 bits):

After inverting all the bits, we get:

And this is -11.

operator Eqv

Used to perform logical equivalence of two expressions.

Syntax:

result = expression1 Eqv expression2

result required; Any numeric (including Boolean) variable
expression1 required; Any expression
expression2 required; Any expression

Notes:

The following table shows how the Eqv operator works:

For bits, the Eqv operator works as follows (look from left to right)

0 0 1
0 1 0
1 0 0
1 1 1

Examples:

Dim A, B, C, D, MyCheck
A \u003d 10: B \u003d 8: C \u003d 6: D \u003d Null
MyCheck \u003d A\u003e B Eqv B\u003e C " Will refund True.
MyCheck \u003d B\u003e A Eqv B\u003e C " Will refund False.
MyCheck \u003d A\u003e B Eqv B\u003e D " Will refund Null.
MyCheck \u003d A Eqv B " Will refund -3

Imp operator

Used to make a logical implication of two expressions.

Syntax:

result = expression1 Imp expression2

result required; Any numeric (including Boolean) variable
expression1 required; Any expression
expression2 required; Any expression

Notes:

The following table shows how the Imp operator works:

If expression1 \u003d

, and expression2 \u003d

That result \u003d

True True True
True False False
True Null Null
False True True
False False True
False Null True
Null True True
Null False Null
Null Null Null

For bits, the Imp operator works as follows (look from left to right)

0 0 1
0 1 1
1 0 0
1 1 1

Examples:

Dim A, B, C, D, MyCheck
A \u003d 10: B \u003d 8: C \u003d 6: D \u003d Null
MyCheck \u003d A\u003e B Imp B\u003e C " Will refund True.
MyCheck \u003d A\u003e B Imp C\u003e B " Will refund False.
MyCheck \u003d B\u003e A Imp C\u003e B " Will refund True.
MyCheck \u003d B\u003e A Imp C\u003e D " Will refund True.
MyCheck \u003d C\u003e D Imp B\u003e A " Will refund Null.
MyCheck \u003d B Imp A " Returns -1

A VBA program is a sequence of statements.

A number of conventions should be followed when programming. Thus, several operators can be placed on one line. A colon is placed between operators on the same line.

Any line can be split into two by placing at the end of the first character characters "Space" + "Underscore" (_), in this case the second line will be considered a continuation of the first.

Comments are used to make the program readable. There are two ways to enter comments in VBA: using the apostrophe ('), which can be placed anywhere on the line, and the reserved word Rem instead of the apostrophe.

1. Dim statementis intended to declare the types of variables.

Dim A As Integer - variable A is declared as an integer, i.e. only integer values \u200b\u200bwill be stored in it .

Dim D As Date - a variable D is declared to store dates.

Dim Surname, Name As String - variables are declared. Surname and Name intended for storing text.

Dim B (12) As Integer - a one-dimensional array (vector) is declared, consisting of 12 integers, and by default the first element of the array will be B (0), and the last B (12).

Dim B (3,3) As Single - declared a two-dimensional array 3x3 (matrix), consisting of real numbers.

If the type of the variable is not specified, then the Variant type is used by default. However, specifying the specific type of a variable makes the program more reliable and speeds up its work, since VBA does not need to spend time recognizing an undeclared variable every time it is accessed.

If the size of the array M is not known in advance and is determined during the program, then when describing the array, the number of elements is not specified, and the array is defined as follows:

Dim M () As Integer

After determining the number of array elements, for example, N, you need to write the operator

2. Assignment operator is designed to assign a value to a variable.

Syntax:

Variable (or object property) \u003d expression.

A \u003d 5 - variable A to assign the value 5 ;

· B \u003d "Manager" - variable b assign meaning "Manager";

· Address \u003d Sheets ("Organization"). Cells (2,2) - to the variable Address assign the contents of cell B2, which is located on the Organization sheet in the current workbook;

Last name \u003d UserForm1.TextBox1.Text - variable The last name is to assign the contents of the TextBox1 to the custom form UserForm1.

3. With / End with statementsaves the programmer from a lot of repetitions of the name of the same object.

Syntax:

With object

operator1

operator2



operatorN

For example, instead of a sequence of statements

UserForm1.TextBox1.Text \u003d Date

UserForm1.TextBox2.Text \u003d ““

UserForm1.ComboBox1.Text \u003d ““

can be written like this

TextBox1.Text \u003d Date

. TextBox2.Text \u003d ““

. ComboBox1.Text \u003d ““

REM On sheet Sheet1 in column A, starting with the second line, ‘employee rates. Fill in the combo box ComboBox1 in the ‘custom form UserForm1

'The first line of the program - on sheet Sheet1 in column A' the number of filled cells is counted, the result is assigned to the variable N

N \u003d Application.CountA (Sheets (“Sheet1”). Range (“A: A”)).

D \u003d ”A2: A” & Cint (N)

Sheets ("Sheet1"). Range (D) .Name \u003d "Rates"

TextBox1.Text \u003d Date

. TextBox2.Text \u003d ““

. ComboBox1.Text \u003d ““

. ComboBox1.Rowsource \u003d “Rates“

4. If / Then / Else Conditional Statement - allows you to check a certain condition and, depending on the results of the check, perform one or another action

Syntax:

If condition Then operators1 [ Else operators2]

If the condition is true, then statements1 are executed, otherwise statements2 are executed.

It is also allowed to use a complex conditional operator, which is written as a block:

If condition1 Then

operators1

ElseIfcondition2 Then

Parameter name Value
Topic of the article: VBA statements
Category (thematic category) Programming

VBA is an operator language. This means that its programs (procedures or functions) represent sequences of statements.

In the VBA language, the following groups of operators can be distinguished:

1. declarative operators designed to describe the objects with which the program works (types of variables, constants and arrays, etc.),

2.operators-comments,

3. operators of assignment and change of values \u200b\u200bof objects,

4. operators that control the course of calculations (conditional, cyclic, transition).

In this course, the basic operators will be considered, and some, for example, cyclic, will be presented in three types, but students should only master one, as the most understandable for use.

Comment operator

Comments do not affect the execution of the program, but are necessary to understand the algorithm. Since programs are being updated repeatedly, it is extremely important to use comments to remember the algorithm and change it correctly.

Any line of program text can end with a comment. A comment in VBA begins with an apostrophe (") and includes any text to the right of the line.

For instance,

Weight \u003d weight + z "Weight gain value \u003d weight * price" New cost

Assignment operator

Assignment operators are the main means of changing program state (variable values). It is a construction that connects a variable (left side) and an expression (right side) with the sign \u003d. An expression consists of operands (variable names, constants, standard function names) and operation signs (arithmetic, logical, string, comparison). The meaning of this operator is that the left side is assigned the value of the right side.

Control Operators

The VBA set of control statements conforms to a structured programming language. This set includes conditional and loop statements, which allows you to organize the computation process reliably and efficiently.

Conditional statement If Then Else End If

This is a common computation control operator in programming languages \u200b\u200bthat allows you to select and perform actions based on the truth of a certain condition.

VBA operators - concept and types. Classification and features of the "VBA Operators" category 2017, 2018.

  • - Operators of life and conservation of Quantity

    This category of Dial in TRIZ is presented as “principle No. 34, discarding and regenerating parts: a) The part of an object that has fulfilled its purpose or has become unnecessary must be discarded (dissolved, evaporated, etc.) or modified directly in the course of work. b) ....


  • - VBA language elements

    Figure: 12.1 Objects located on the form 6. Activating each object on the form separately, set its property using the properties window (Properties Fig. 12.2). Thus, the interface is created (Figure 12.3). Figure: 12.3. Project interface (with ....


  • - Conditional operators

    Compound operator. Structured operators Procedure call operator Unconditional jump operator The unconditional jump operator provides the ability to change the execution order ....


  • - Compound conditional statements

    Sometimes, when solving problems, a situation arises when other operators of the condition can, in turn, be the operators contained in the branches. They are called compound operators. Moreover, the number of investments, or, as they say, levels of investments can be ....


  • - VBA Basics

    Visual Basic For Application (VBA) is a combination of one of the simplest programming languages \u200b\u200band all the computational power of an Excel spreadsheet processor. With VBA, you can quickly and easily create a variety of applications, even if you are not an expert in the field ...

  • Comparison operations are typically used in looping statements to make some sort of decision about the further progress of operations.

    The result of any comparison operation is a Boolean value: True, False.

    If both operands in a comparison expression are of the same data type, VBA performs a simple comparison for that type.

    If both operands in a comparison expression are of specific types and the types are not compatible, VBA issues a type mismatch error.

    If one or both operands in the comparison expression are Variant, VBA attempts to convert the Variant to some compatible type.

    String comparison

    When comparing strings with relational operators, VBA compares each string from left to right character by character.

    In VBA, one string is equal to the other only when both strings contain exactly the same characters in exactly the same order and both strings are the same length. For example, the strings "johns" "johns" "johns" are not equal to each other, because VBA does not ignore leading or trailing whitespace when comparing strings.

    Care should be taken when comparing variable-length strings.

    Binary and text string comparison

    To store text, the computer uses a pattern in which each displayed character is assigned a unique number. All letters of the alphabet have their own unique numbers. Typically, uppercase letters are numbered lower than lowercase letters. The number corresponding to a specific letter or symbol is called character code.

    When performing a binary comparison on string information, VBA uses the binary equivalent of a number for each character. This comparison method is called binary or binary and is the default comparison method.

    Because uppercase letters have lower binary numbers, uppercase letters appear in alphabetical order before lowercase letters. Therefore, when comparing binary strings, the string "ABC" will be less than the string "abc".


    In textual string comparisons, VBA does not use the binary equivalent of characters, and does not "distinguish" between upper and lower case. In a text comparison, the string "abc" is equal to the string "ABC".

    To select a method for comparing strings, use the Option Compare directive


    Option Compare


    This directive must be in the module declaration area.

    String concatenation

    Joining one string to another is called string concatenation.

    String concatenation is commonly used to generate strings from various sources in a procedure to create a message for display. VBA has two operators for string concatenation.

    Concatenation Operator (&)

    The operator (&) in VBA is only used for string concatenation.

    Syntax


    Operand_1 & Operand_2 [& Operand_3 ..]


    Operand_N is any valid string or numeric expression (which is converted to string).

    The data type of the result of string concatenation is String.

    If an operand in a string concatenation expression is Empty or Null, VBA interprets that operand as a zero-length string (no character string).

    Note! The symbol (&) of the concatenation operation must be separated by a space from the variable name, since otherwise, VBA can interpret the character as a Long type inference character.

    The addition operator in string concatenation

    You can also use the (+) operator to concatenate strings.

    This operator has the same syntax and requirements as the (&) operator. However, it should be understood that in VBA the main purpose of the (+) operator is arithmetic addition. Therefore, in order to avoid ambiguity in reading the program code, it is strongly recommended to use the operator (&) for string concatenation.

    Operation Priorities

    Many of the expressions in the program code are complex (compound), i.e. consist of two or more expressions.

    When evaluating complex expressions, VBA follows these rules:

    • Portions of an expression enclosed in parentheses are always evaluated first;
    • Specific operations are performed depending on the operator hierarchy (table below);
    • If the operator hierarchy is equal, they are evaluated from left to right.
    Operator
    Did you like the article? To share with friends: