Forum Discussion
Anders_J_Rosen
Jan 02, 2021Copper Contributor
IFS in VBA (Multiple IF statements)
Hey all, I'm trying to define a simple VBA function for quick use in my Excel spreadsheets, but I'm receiving a #VALUE! error. I'm new to Visual Basic and Excel programming, so I'm likely missing som...
HansVogelaar
Jan 02, 2021MVP
Apparently IFS hasn't been added to the WorksheetFunction object. You can use a Select Case block instead:
Function Grade(p As Variant) As String
Select Case p
Case Is >= 90
Grade = "A"
Case Is >= 80
Grade = "B"
Case Is >= 70
Grade = "C"
Case Is >= 60
Grade = "D"
Case Else
Grade = "F"
End Select
End Function
or use the LOOKUP function:
Function Grade(p As Variant) As String
Grade = WorksheetFunction.Lookup(p, Array(0, 60, 70, 80, 90), Array("F", "D", "C", "B", "A"))
End Function