Interview Questions

Decisions in VBA

Comparison operators

Computers do more than calculate among other things they also make decisions. VBA macros can select a course of action that depends on the outcome of some decision. We have already encountered one way in which this can be done by using the If Then statements. This chapter will look at VBA decision-making statements in a little more detail.

When we used a statement like If cell. Value > 0 Then in previous chapters,the expression cell. Value has to be greater than 0 before the result will succeed. In other words, the result of the comparison must be true. This is called a comparison operator. Clearly, the comparison operators produce Boolean outcomes, because the outcome of the comparison is either true or false. The mathematical operators, discussed in previous chapters, produce numeric values, whereas the comparison operators produce Boolean results. Table gives some illustrations of comparison operators.

Some comparison operators used in VBA

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.

Examples of comparison operators

Pragna Meter
Next Chapter  
e-University Search
Related Jobs