+ Reply to Thread
Results 1 to 5 of 5

Find a student's 3 lowest scoring tests

Hybrid View

  1. #1
    Registered User
    Join Date
    04-29-2011
    Location
    Fresno, CA
    MS-Off Ver
    Excel 2003
    Posts
    7

    Find a student's 3 lowest scoring tests

    Hello VBA pro's,

    I have attached a workbook that shows what I would like to do.

    I am a teacher and use excel as my grade book. The grade book will contain 30 to 40 tests (I only did 10ish on the example worksheet) in columns with student names in the corresponding rows. I would like to write a VBA subroutine that scans each student's scores (in the rows) and then reports to me what their 3 lowest tests are (not the scores themselves but the testnames). These testnames will then be returned to a separate range like the one in the middle of the sheet.


    Thank you in advanced for your help.
    Attached Files Attached Files

  2. #2
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Find a student's 3 lowest scoring tests

    Steve_G_2,

    The code I came up with works and returns the desired results, but I wouldn't exactly call it efficient. I just couldn't come up with a better way to do it. Here's the code:

    Sub LowestScores()
    
        Application.ScreenUpdating = False
        Application.Calculation = xlCalculationManual
        
        Dim NameResultCol As String:    NameResultCol = "E"
        Dim NameResultRow As Long:      NameResultRow = 13
        Dim rngStudents As Range:       Set rngStudents = ActiveSheet.Range(Cells(2, 1).Address & ":" & Cells(Rows.Count, 1).End(xlUp).Address)
        Dim TestNameRow As Long:        TestNameRow = 1
        
        Dim Test1 As Long, Test2 As Long, Test3 As Long
        Dim Test1Name As String, Test2Name As String, Test3Name As String
        Dim sCell As Range, rCell As Range, LastTest As Long, CheckTest As Long
        For Each rCell In rngStudents
            LastTest = ActiveSheet.Cells(rCell.Row, Columns.Count).End(xlToLeft).Column
            Test1 = 1000000
            Test2 = 1000000
            Test3 = 1000000
            
            CheckTest = rngStudents.Column + 1
            While CheckTest <= LastTest
                If ActiveSheet.Cells(rCell.Row, CheckTest).Value < Test1 Then
                    
                    Test1 = ActiveSheet.Cells(rCell.Row, CheckTest).Value
                    Test1Name = ActiveSheet.Cells(TestNameRow, CheckTest).Value
                
                End If
                CheckTest = CheckTest + 1
            Wend
            
            CheckTest = rngStudents.Column + 1
            While CheckTest <= LastTest
                If ActiveSheet.Cells(rCell.Row, CheckTest).Value <= Test2 And _
                    ActiveSheet.Cells(TestNameRow, CheckTest).Value <> Test1Name Then
                    
                    Test2 = ActiveSheet.Cells(rCell.Row, CheckTest).Value
                    Test2Name = ActiveSheet.Cells(TestNameRow, CheckTest).Value
                       
                End If
                CheckTest = CheckTest + 1
            Wend
            
            CheckTest = rngStudents.Column + 1
            While CheckTest <= LastTest
                If ActiveSheet.Cells(rCell.Row, CheckTest).Value <= Test3 And _
                    ActiveSheet.Cells(TestNameRow, CheckTest).Value <> Test1Name And _
                    ActiveSheet.Cells(TestNameRow, CheckTest).Value <> Test2Name Then
                    
                    Test3 = ActiveSheet.Cells(rCell.Row, CheckTest).Value
                    Test3Name = ActiveSheet.Cells(TestNameRow, CheckTest).Value
                    
                End If
                CheckTest = CheckTest + 1
            Wend
            
            ActiveSheet.Range(NameResultCol & NameResultRow).Offset(0, 1).Value = Test1Name
            ActiveSheet.Range(NameResultCol & NameResultRow).Offset(0, 2).Value = Test2Name
            ActiveSheet.Range(NameResultCol & NameResultRow).Offset(0, 3).Value = Test3Name
            
            NameResultRow = NameResultRow + 1
            
        Next rCell
        
        Application.Calculation = xlCalculationAutomatic
        Application.ScreenUpdating = True
        
    End Sub


    Notes:
    If, on your real spreadsheet, the following are different, just change the code to the correct values:
    • NameResultCol (This is the column that contains "Bill's 3 Lowest" etc. In the sample you provided, it was column E)
    • NameResultRow (This is the row that "Bill's 3 Lowest" etc. starts on. In the sample you provided, it was row 13)
    • TestNameRow (This is the row that contains the tests' names. In the sample you provided, it was row 1)

    I have attached a modified version of your workbook so you can see how it works

    Hope that helps,
    ~tigeravatar
    Attached Files Attached Files

  3. #3
    Forum Contributor
    Join Date
    10-08-2006
    Location
    Walnut, CA
    MS-Off Ver
    2003,2010, Office 365
    Posts
    114

    Re: Find a student's 3 lowest scoring tests

    Hi,

    Try this, run Macro FindLScores and check the result in Sheets("Result")

    Tony
    Attached Files Attached Files

  4. #4
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Find a student's 3 lowest scoring tests

    I appreciate you explicitly requested a Macro for this but in truth you could achieve the same with formulae - using your sample file:

    G13:
    =INDEX($B$1:$K$1,MATCH(SMALL(INDEX($B2:$K2+COLUMN($B2:$K2)/1000000,0),COLUMNS($G13:G13)),INDEX($B2:$K2+COLUMN($B2:$K2)/1000000,0),0))
    applied to G13:I15
    Obviously, if so desired you could also link the names.

  5. #5
    Registered User
    Join Date
    04-29-2011
    Location
    Fresno, CA
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: Find a student's 3 lowest scoring tests

    Thank you for the help. You are all fabulous.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1