

either it should be TRUE or it should be FALSE). Here, ‘ condition_1’ to ‘ condition_n’ refers to the expression that must evaluate to a Boolean value (i.e. Recommended Reading: Nested IF’s in Excel In such a case, if the condition inside IF statement evaluates to FALSE then the program control just moves to the next instruction (the instruction after the IF Block) and starts executing them sequentially. However, if the condition evaluates to FALSE then it takes the program control to the statements inside the Else Block.Īlthough, it is not mandatory to have an Else Block with every IF statement. If the condition evaluates to TRUE then, it simply takes the program control to the instructions inside the IF block and starts executing them sequentially. On the other hand, VBA IF Statement checks a condition but it doesn’t return any value. The IF Function in Excel and the IF Statement in VBA are basically designed to do the same tasks, but the way they work is slightly different from each other.Įxcel IF function checks a particular condition and if the condition is TRUE, it returns one value otherwise it returns the second value. If you have followed my earlier posts, then you would remember that we discussed If Function in Excel. The task of the IF Statement is to check if a particular condition is met or not. IF statement in VBA is sometimes also called as IF THEN ELSE Statement.

Have questions or feedback about Office VBA or this documentation? Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.IF is one of the most popular and frequently used statements in VBA. Var1 = "5": Var2 = 4 ' Initialize variables. This example shows various uses of comparison operators, which you use to compare expressions. Such fractional value loss may cause two values to compare as equal when they are not. 0001 may be lost for Decimal, any fractional value less than 1E-28 may be lost, or an overflow error can occur. For Currency, any fractional value less than. Similarly, when a Decimal is compared with a Single or Double, the Single or Double is converted to a Decimal. If a Currency is compared with a Single or Double, the Single or Double is converted to a Currency. When a Single is compared to a Double, the Double is rounded to the precision of the Single. One Variant expression is Empty and the other is a string One Variant expression is Empty and the other is numeric The numeric expression is less than the string expression. One Variant expression is numeric and the other is a string The following table shows how the expressions are compared or the result from the comparison, depending on the underlying type of the Variant. If expression1 and expression2 are both Variant expressions, their underlying type determines how they are compared. Perform a string comparison, using a zero-length string ("") as the Empty expression. One expression is Empty and the other is a String Perform a numeric comparison, using 0 as the Empty expression. One expression is Empty and the other is a numeric data type One expression is a String and the other is any Variant except a Null One expression is a numeric data type and the other is a string Variant that can't be converted to a number One expression is a numeric data type and the other is a Variant that is, or can be, a number Ifīoth expressions are numeric data types ( Byte, Boolean, Integer, Long, Single, Double, Date, Currency, or Decimal) The following table shows how the expressions are compared or the result when either expression is not a Variant. When comparing two expressions, you may not be able to easily determine whether the expressions are being compared as numbers or as strings. The Is and Like operators have specific comparison functionality that differs from the operators in the table.

The following table contains a list of the comparison operators and the conditions that determine whether result is True, False, or Null. Required any string expression or range of characters. Result = expression1 comparisonoperator expression2 result = object1 Is object2 result = string Like patternĬomparison operators have these parts: Part
