String comparisons

When you compare strings, VBA uses the American Standard Code for Information Interchange (ASCII) codes, to determine how to compare the characters. For example, the ASCII code for the uppercase letter A - whose ASCII code is 65 - is less than the uppercase letter B (ASCII code 66). The ASCII codes for the lower case characters are the same for the Uppercase characters plus 32. Hence, the ASCII code for lowercase 'a' is 65 + 32 = 97, the ASCII code for lowercase 'b' is 66 + 32 = 98, and so on. To use comparison operators in your programs, you must understand how they get their true or false results. The If statement, introduced in the next section, explains how you can use these results to make decisions in your program. Before you read the next section, make sure that you understand how these operators compare values.

Table 6.2 Examples of comparison operators

Relation

Result

4 > 1

True

4 < 1

False

4 < 8

True

"straw" <= "wood"

False

"Keith Darlington" < "keith darlington"

True

0 >= 0

True

0 <= 0

True

1 <> "one"

True

2 >= 3

False

0 0

Post a comment