VBA code identify rows that have exceeded a certain value in a cumulative chain

lgare

New Member
Joined
Dec 17, 2015
Messages
4
Hi,

I have a data set of around 4000 individuals where each individual has multiple rows pertaining to scores in exams. Each row for each student contains an exam grade, and I have a column displaying the amount of credits the exam is worth (‘Exam Credit’). I need to identify the ‘best’ 100 credits worth of exam results for each student. I have sorted the data so that each students’ exam scores are in highest to lowest order. Can someone suggest some VBA code that will cumulatively add each students’ credit up to 100, then mark any subsequent exam rows with a N/A (or a #N/A or leave blank), as is shown in the ‘Cumulative Credit’ column. Any suggestions much appreciated.
Person NumberExam CreditExam ScoreCumulative Credit
XXXXXX001201120
XXXXXX001201040
XXXXXX00140880
XXXXXX001204100
XXXXXX001204N/A
XXXXXX002201520
XXXXXX002601380
XXXXXX002207100
XXXXXX002207N/A
XXXXXX002206N/A
XXXXXX003401340
XXXXXX003201360
XXXXXX0034012100
XXXXXX0032010N/A
XXXXXX004201320
XXXXXX0048013100
XXXXXX004209N/A

<tbody>
</tbody>
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Try:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG17Dec57
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, Temp [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String,[/COLOR] Num [COLOR="Navy"]As[/COLOR] Double
[COLOR="Navy"]Set[/COLOR] Rng = Range(Range("A2"), Range("A" & Rows.Count).End(xlUp))
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
    [COLOR="Navy"]If[/COLOR] Not Temp = Dn.Value [COLOR="Navy"]Then[/COLOR] Num = 0
        Num = Num + Dn.Offset(, 1)
        Dn.Offset(, 3).Value = IIf(Num <= 100, Num, "N/A")
    Temp = Dn.Value
[COLOR="Navy"]Next[/COLOR] Dn
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
MickG,

Nicely done as always.


lgare,

Here is another macro solution for you to consider.

You can change the raw data worksheet name in the macro.

Sample raw data, and, results:


Excel 2007
ABCD
1Person NumberExam CreditExam ScoreCumulative Credit
2XXXXXX001201120
3XXXXXX001201040
4XXXXXX00140880
5XXXXXX001204100
6XXXXXX001204N/A
7XXXXXX002201520
8XXXXXX002601380
9XXXXXX002207100
10XXXXXX002207N/A
11XXXXXX002206N/A
12XXXXXX003401340
13XXXXXX003201360
14XXXXXX0034012100
15XXXXXX0032010N/A
16XXXXXX004201320
17XXXXXX0048013100
18XXXXXX004209N/A
19
Sheet1


Code:
Sub UpdateCumulative()
' hiker95, 12/17/2015, ME909358
Dim lr As Long, r As Long, n As Long
Application.ScreenUpdating = False
With Sheets("Sheet1")   '<-- you can change the sheet name here
  lr = .Cells(Rows.Count, 1).End(xlUp).Row
  For r = 2 To lr
    n = Application.CountIf(.Columns(1), .Cells(r, 1).Value)
    If n = 1 Then
      .Cells(r, 4).Value = .Cells(r, 2).Value
    ElseIf n > 1 Then
      With .Cells(r, 4)
        .FormulaR1C1 = "=RC[-2]"
        .Value = .Value
      End With
      With .Range(.Cells(r + 1, 4), .Cells(r + n - 1, 4))
        .FormulaR1C1 = "=IF(R[-1]C>=100,""N/A"",R[-1]C+RC[-2])"
        .Value = .Value
      End With
    End If
    r = r + n - 1
  Next r
End With
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,131
Messages
6,123,223
Members
449,091
Latest member
jeremy_bp001

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top