'If' helps the user to calculate a value if a condition evaluates to TRUE and another value if the condition evaluates to FALSE.
Syntax: IF (logical_test,assign_value_if_true,assign_value_if_false)
For example, you could give the buyer of your goods a discount of 10% if she purchases a quantity greater than or equal to 12 of a product and a discount of 5% if she purchases a quantity less than 12.
The code for the above macro is given below.
Range("D4") = Range("C4") * Range("B4")
If Range("B4") >= 12 Then
Range("E4") = Range("D4") * 0.1
Range("E4") = Range("D4") * 0.05
Range("F4") = Range("D4") - Range("E4")
Range("F4") = Round(Range("F4"), 2)
The if-else function macro first calculates the 'Amount' based on the quantity purchased and the price of the item. The next step determines whether the purchaser bought a quantity greater than or equal to 12. If true the discount of 10% is given else a discount of 5% is given. Next the final amount due from the customer is calculated. The final amount is then formatted to two deicmal places.
Watch the training video below to see the macro in action.
|Excel 2007||Excel 2003||Macros Excel 2007||Macros Excel 2003||Excel 2010|
|About Us||Free Ebook||Offline Training||Blog||Interactive Training|