Num = WorksheetFunction.RandBetween( 1, 10)Įxplanation: In the above code we have used the RandBetween function of Excel to generate any random number from 1 – 10. So, we will try to write a VBA program as: Objective: Here we will generate a random number between 1-10 and then our task is to identify if the generated number is less than 5, equal to 5 or greater than 5. Now let’s understand how to use the IF Statement in VBA.īefore preceding let’s make our objective very clear. Note: Remember that out of IF, ELSEIF’s, and ELSE code blocks only a single code block will be executed at a time based on the condition. Finally, if all the IF and ELSEIF’s evaluate to FALSE then the ELSE block will be executed. However, in case, it also evaluates to FALSE then the next ELSEIF statement will be evaluated and so on. If it evaluates to TRUE then the instructions inside the ELSEIF Block will be sequentially executed and the rest of the blocks (ELSEIF’s and ELSE blocks) will be skipped. This means, first of all, the IF Statement with ‘ condition_1’ will be evaluated, if it evaluates to TRUE then statements inside the first IF block will be executed and the rest of the blocks (ELSEIF’s and ELSE blocks) will be skipped.īut, if the First IF Statement evaluates to FALSE then the ELSEIF statement following it will be evaluated. The conditions along with the IF Statements will be evaluated sequentially. IF function usually ends with an ‘ END IF’ statement which tells the application that it is the last line of the IF function. The ‘ THEN’ keyword is basically a directive signifying that the instructions immediately following the IF Statement are to be executed if the condition evaluates to TRUE. 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. Comparison operators come in handy in such situations.IF is one of the most popular and frequently used statements in VBA. This operator will check against the negative numbers to ensure that the amount paid does not exceed the billed amount. In such cases, you can use comparison operators. In this application, you want to validate the values entered before you post. Let’s say you are developing a simple point-of-sale application. Comparison operators include equal to, less than, greater than, and not equal toĬomparison operators are used to comparing values for validation purposes. These are operators that are used to compare values. For example, the following statement would compare two values and return TRUE if the first value was less than or equal to the second: These symbols can be used in conjunction with each other to create more complex comparisons. Less than or equal to is represented by the symbol =. The Less Than or Equal to operator is represented by the. The Not Equal operator is represented by the symbols and returns TRUE if the two values being compared are not equal. There are two comparison operators in VBA that are not equal to each other: the Not Equal operator and the Less Than or Equal to the operator.
0 Comments
Leave a Reply. |