English 中文(简体)
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.

问题回答

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.

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

An easy solution would be to simply split the formula into two cells

=IF(O5>0.895,"A+",IF(O5>0.845,"A",IF(O5>0.795,"A-",<Other cell ref here>)))

Other cell:

=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")))))

While I prefer the Vlookup method (Slaks solution) for numeric parameters for the simplicity and elegance, an alternative which becomes valuable in case of non-numeric parameters is concatenated IFs.

=IF( Case1 , "Label 1", "" ) & 
 IF( Case2 , "Label 2", "" ) & 
 IF( Case3 , "Label 3", "" ) & 

..... and so on

For example, in this case:

= IF( O5 >= 89.5 , "A+" , "" ) & 
  If( AND ( O5 < 89.5 , O5 >= 84.5 ) , "A" , "" ) & 
  If( AND ( O5 < 84.5 , O5 >= 79.5 ) , "B+" , "" ) &

..... and so on for other levels.

The VLOOKUP solution seems the best. To add a VBA script, you could bring up the Visual Basic Toolbar, add a control like a Button and then double-click it. The code for that event opens in the Excel VBA environment. There you could add the code in VB, perhaps something like this.

Private Sub CommandButton1_Click()
  Cells(1, 2) = getValue(Cells(1, 1))
End Sub


Private Function getValue(ByVal argMarks As Double)
  If argMarks > 89.5 And argMarks <= 100 Then
    getValue = "A+"
  If argMarks > 84.5 And argMarks <= 89.49 Then
    getValue = "A"

  .
  .
  and so on...

End Function




相关问题
import of excel in SQL imports NULL lines

I have a stored procedure that imports differently formatted workbooks into a database table, does work on them then drops the table. Here is the populating query. SELECT IDENTITY(INT,1,1) AS ID ...

Connecting to Oracle 10g with ODBC from Excel VBA

The following code works. the connection opens fine but recordset.recordCount always returns -1 when there is data in the table. ANd If I try to call any methods/properties on recordset it crashes ...

Excel date to Unix timestamp

Does anyone know how to convert an Excel date to a correct Unix timestamp?

C# GemBox Excel Import Error

I am trying to import an excel file into a data table using GemBox and I keep getting this error: Invalid data value when extracting to DataTable at SourceRowIndex: 1, and SourceColumnIndex: 1. As ...

Importing from excel "applications" using SSIS

I am looking for any tips or resources on importing from excel into a SQL database, but specifically when the information is NOT in column and row format. I am currently doing some pre-development ...

热门标签