Showing posts with label If Statement. Show all posts
Showing posts with label If Statement. Show all posts

Tuesday, June 28, 2011

If function statement

The IF function, one of Excel's logical functions, tests to see if a certain condition in a spreadsheet is true or false.


The syntax for the IF function is:

=IF ( logical_test, value_if_true, value_if_false )


logical_test - a value or expression that is tested to see if it is true or false.
value_if_true - the value that is displayed if logical_test is true.
value_if_false - the value that is displayed if logical_test is false.

Example Using Excel  IF Function:


This example will test to see if the value in cell D1 is less than 26. If it is, the IF function will place the number 50 in cell D2. If the value in D1 is not less than 20, the IF function will place the number 100 in cell D2.

  1. Enter 30 into cell D1.

  2. Click on cell E1 - the location where the results will be displayed.

  3. Click on the Formulas tab.

  4. Choose Logical Functions from the ribbon to open the drop down list.

  5. Click on IF in the list to bring up the function's dialog box.

  6. On the Logical_test line in the dialog box, click on cell D1. After this type the less than symbol ( < ) and then the number 20.

  7. On the Value_if_true line of the dialog box, type 50.

  8. On the Value_if_false line of the dialog box, type 100.

  9. Click OK.

  10. The value 100 should appear in cell E1, since the value in D1 is greater than 20.

  11. To change the result in cell E1, change the number in cell D1 to 10 and press the Enter key.

  12. The value 50 should now be present in cell E1 since the value in D1 is now less than 20.

  13. If you click on cell E1, the complete function = IF ( D1 < 20 , 50 , 100 ) appears in the formula bar above the worksheet.

Thursday, December 17, 2009

IF statement in VBA

If statement is basically a single condition base statement. If statement is basically known as
If....then statement

Syntax:
If condition Then statement1 [Else statement2]

If the condition is satisfied (true then statement1 is carried out otherwise control passes to statement2.

Usually you will need to use the block statement. If.....then ....else which does not restrict you to one line of code. This has the form.

Example:
This macro looks at the value in A1. If it is 100 it enters the text Full marks in B1. If the value in A1 is not 100, the macro enters No in B1.

Sub fullmarks()
sheets("sheet1").select
cells(1,1).select
If activecell = 100 Then
cells(1,2).value = "No"
End if
end sub

Note: You could omit the line Cells(1,1).Select and change the following line to if cells(1,1) = 100 then.

Subscribe Now: From your browser

Search:

Learn Office Excel