Excel VBA Basic Training

Excel VBA Basic Training

If statements

If statements are used to do conditional checks if a statement is TRUE or FALSE. If TRUE then one action is performed and IF FALSE then another action is performed. You can add multiple IF statements within one by using ElseIF.

If condition_1 Then
    result_1
ElseIf condition_2 Then
    result_2
...
ElseIf condition_n Then
    result_n
Else
   result_else
End If

Simple IF Example

Dim score As Integer
Dim grade As String
score = Range("A1").Value

If score > 70 THEN
   grade = "pass"
else
   grade = "fail"
EndIf

ElseIf Example

Dim score As Integer
Dim grade As String
score = Range("A1").Value

IF score < 60 THEN
   grade = "F"
ELSEIF score>60 AND score<70 THEN
   grade = "D"
ELSEIF score>70 AND score<80 THEN
   grade = "C"
ELSEIF score>80 AND score<90 THEN
   grade = "B"
ELSEIF score>90 AND score<100 THEN
   grade = "A"
ELSE
   grade = "NA"
ENDIF

[efb_likebox fanpage_url="https://www.facebook.com/vbamarket/" box_width="840" box_height=340"" responsive="1" show_faces="1" show_stream="1" hide_cover="1" small_header="0" hide_cta="0" locale="en_US"]

Add comment