Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested IF statements in Excel [Over the 7 allowed limit]

I am trying to create a spreadsheet which automagically gives a grade to a student based on their marks they got.

I've apparently hit Excel's nested IF statement limit which is 7.

Here's my if statement:

=IF(O5>0.895,"A+",IF(O5>0.845,"A",IF(O5>0.795,"A-",IF(O5>0.745,"B+",IF(O5>0.695,"B",IF(O5>0.645,"B-",IF(O5>0.595,"C+",IF(O5>0.545,"C","D"))))))))

I was reading online that I could create a VBA script and assign it that, but I dont know anything about VBA....so if someone could help me write a VBA for this, would be awesome.

Its still missing the C- grade and anything lower should be awarded a D mark.

This is the grading scheme I am trying to create...:

A+ 89.500 - 100.000 Pass with Distinction

A 84.500 - 89.490 Pass with Distinction

A- 79.500 - 84.490 Pass with Distinction

B+ 74.500 - 79.490 Pass with Merit

B 69.500 - 74.490 Pass with Merit

B- 64.500 - 69.490 Pass with Merit

C+ 59.500 - 64.490 Pass

C 54.500 - 59.490 Pass

C- 49.500 - 54.490 Pass

D 0.000 - 49.490 Specified Fail

I wouldn't mind going down the VBA route, however my understanding of VB language is absolutely minimal (don't like it)...if this gets too tedious, I was thinking to create a small php/mysql application instead.

like image 938
Alks Avatar asked Mar 17 '10 19:03

Alks


People also ask

Can you nest more than 7 if statements in Excel?

Excel allows a max of 7 nested if statements. If we wanted to expand our list of possible statuses, we could add only one more condition and one more status. But fortunately we can add more using a different function.

Is there a limit to how many nested if statements in Excel?

Nested IF limitsIn Excel 2007 - Excel 365, you can nest up to 64 IF functions. In older versions of Excel 2003 and lower, up to 7 nested IF functions can be used.

Is there a limit to IFS statements in Excel?

Simple syntax Please note that the IFS function allows you to test up to 127 different conditions. However, we don't recommend nesting too many conditions with IF or IFS statements. This is because multiple conditions need to be entered in the correct order, and can be very difficult to build, test and update.


2 Answers

You can do this much more elegantly with the VLOOKUP formula by making separate table mapping lower bounds to letters. The mapping table must be sorted by grade number ascending.

For example:

Table

A     B
0     D
49.5  C-
54    C
59.5  C+
...   ...

Formula:

=VLOOKUP(SomeCell, $A$1:$B$9, 2, TRUE)

Where $A$1:$B$9 is the range with the grade table. (The $ signs tell Excel not to move the reference if you copy the formula).
Passing TRUE as the last argument will cause Excel to do a binary search to find the value, which (as long as the data is sorted) is exactly what you want it to do.

like image 186
SLaks Avatar answered Oct 31 '22 15:10

SLaks


Go to the Visual Basic Editor, and insert this code. I don't know what version of Excel you're using, but for versions before 2007, go to tools, Macros, Visual Basic Editor. For Version 2007 and newer , it is on the Development Tab which is not enabled by default.

Depending on how you want to link it, you could add a button to the page, or call it from the Worksheet_Calculate event.

This assumes that you have the student's total grade in cell A2, and will put the results in A2 and B2.

Sub Calculate
    dim LetterGrade as string
    dim Superlative as string
    Select Case Cells(1,2)
        Case  >= 89.500
            LetterGrade="A+"
            Superlative ="Pass with Distinction"

        Case  84.500 to 89.490 
            LetterGrade="A"
            Superlative ="Pass with Distinction"

        Case 79.500 to 84.490 
            LetterGrade="A-"
            Superlative ="Pass with Distinction"

        Case  74.500 to 79.490 
            LetterGrade="B+"
            Superlative ="Pass with Merit"

        Case 69.500 to 74.490
            LetterGrade="B"
            Superlative ="Pass with Merit"

        Case 64.500 to 69.490 
            LetterGrade="B-"
            Superlative ="Pass with Merit"

       case 59.500 to 64.490 
            LetterGrade="C+"
            Superlative ="Pass"

        Case 54.500 to 59.490
            LetterGrade="C"
            Superlative ="Pass"

        Case 49.500 to 54.490
            LetterGrade="C-"
            Superlative ="Pass"
        Case <=  49.490 
            LetterGrade="F"
            Superlative ="Specified Fail"

 End Select
        Cells(2, 1) = LetterGrade
        Cells(2, 2) = Superlative


End Sub
like image 28
Andrew Neely Avatar answered Oct 31 '22 15:10

Andrew Neely