How to use the IF-THEN-ELSE Statement (VBA)

In Visual Basic Editor, we use IF and then Else condition to control output based different condition in MS Excel. We will, now, discuss how to use IF, and then Else to check the multiple condition for the particular output.

Let’s take an example to understand: –

We want to check if Score is equal to or greater than 60, then result should either be passed or failed respectively. To record a macro, follow below given steps: –

  • Insert the Command button from the Developer tab.
  • And then assign the Macro.
  • VBE page will get open.
  • Write the below mentioned code: –

Sub Command()
Dim score As Integer, result As String
score = Range(“A1”).Value
If score >= 60 Then
result = “Pass”
result = “Fail”
End If
Range(“B1”).Value = result
End Sub

image 1

When you will click on Command, you will get the result in cell B1, according to the score mentioned in A1 cell.

Next ChapterPrevious Chapter

Leave a Reply