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.

No comments:

Subscribe Now: From your browser

Search:

Learn Office Excel