• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

macros of if elseif

Dalia

Member
Hi,

I am trying to create a macro which says that if score is more than or equal to 80 then grade it as "A" or else "A-"
A B C
john 75
the below macro I created but its not working
Code:
Sub m()
Dim score As Integer
score = ActiveCell.Value
If score >= 80 Then ActiveCell(1, 2).Value = "A"
ElseIf score < 80 Then ActiveCell(1, 2).Value = "A-"
End If
End sub

Kindly advise
 
Last edited by a moderator:
Hi Daia,

Just a little modification in the code..
Code:
Sub m()
Dim score As Integer
score = ActiveCell.Value
  If score >= 80 Then
  ActiveCell(1, 2).Value = "A"
  Else
  ActiveCell(1, 2).Value = "A-"
  End If
End Sub
 
Hi ,

The problem is if you write an IF statement so that the THEN is followed by an action on the same line , the IF statement is completed and will not take an ELSEIF or an ENDIF. Only if you put the action in the next line , then the IF statement is taken as a compound statement , and needs an ENDIF , and will accept an ELSEIF.

Narayan
 

Hi,

another way :​
Code:
Sub m()
    ActiveCell(1, 2).Value = IIf(ActiveCell.Value < 80, "A-", "A")
End Sub
 
Back
Top