Distribute a quantity value in a cell into other cells (is it even possible?)

edge37

Board Regular
Joined
Sep 1, 2016
Messages
102
Office Version
  1. 365
Platform
  1. Windows
Hello, is it possible to distribute a value in a cell (in my case they are extra points) between other cells that already have a note (but not going above the maximum value assigned)? In my gradebook I have a cell with extra points, and I want to distribute them between the assignments that I already graded. As you can see in the example I sent, I have 5 and 3 extra points for each student, HW1 max value in John's grades is 7 points, but he got only 6, so the macro would add 1 point more so the value would reach 7. Then 4 extra points are remaining, so the same would be with other cells until those 5 extra points are distributed. If a cell has the maximun number of points then nothing would happen there, if the cell value is below the max then the extras could be distributed there. If there is no room for more points then the value of extras wouldn't change, if all extra points are distributed the Extra Point cell will show a 0.

I've never heard of this before, but, if possible, can you help me with a macro to do this for all students?

example.xlsx
ABCDEFGHIJ
1NameHW 1HW 2HW 3HW 4HW 5HW 6HW 7XTRA
27 points5 points5 points5 points6 points7 points8 points
3John63436085
4Mary75216783
Sheet1
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
I see what you are trying to achieve, but which students get the extra point, as there are 7 students and only 5 points ??
 
Upvote 0
Why wouldn't all 5 of John's extra points go to HW6? Seems that would be best for John.
 
Upvote 0
See if this does what you want. Test with a copy of your worksheet.

VBA Code:
Sub Distribute_Extras()
  Dim p As Variant, Maxp As Variant, x As Variant
  Dim i As Long, j As Long, ubp2 As Long
  
  With Range("B3", Range("B3").End(xlToRight)).Resize(Range("A" & Rows.Count).End(xlUp).Row - 2)
    p = .Value
    ubp2 = UBound(p, 2)
    Maxp = .Rows(0).Value
    For j = 1 To ubp2
      Maxp(1, j) = Val(Split(Maxp(1, j))(0))
    Next j
    x = .Offset(, .Columns.Count + 1).Resize(, 1).Value
    For i = 1 To UBound(p)
    j = 1
      Do While x(i, 1) > 0 And j <= ubp2
        If p(i, j) < Maxp(1, j) Then
          p(i, j) = p(i, j) + 1
          x(i, 1) = x(i, 1) - 1
        End If
        If p(i, j) = Maxp(1, j) Then j = j + 1
      Loop
    Next i
    .Value = p
    .Offset(, .Columns.Count + 1).Resize(, 1).Value = x
  End With
End Sub

Here is my sheet after the code has been run

edge37.xlsm
ABCDEFGHIJ
1NameHW 1HW 2HW 3HW 4HW 5HW 6HW 7XTRA
27 points5 points5 points5 points6 points7 points8 points
3John75546080
4Mary75516780
5
Sheet1
 
Upvote 0
I am not sure what is possible with your data, but here I have added some other scenarios, including where not all the extra points can be used - as I understand it.
Same code as above.
Before:

edge37.xlsm
ABCDEFGHIJ
1NameHW 1HW 2HW 3HW 4HW 5HW 6HW 7XTRA
27 points5 points5 points5 points6 points7 points8 points
3John63436085
4Mary75216783
5Deb655430720
6Kim33330330
7Tom75556785
Sheet3


After:

edge37.xlsm
ABCDEFGHIJ
1NameHW 1HW 2HW 3HW 4HW 5HW 6HW 7XTRA
27 points5 points5 points5 points6 points7 points8 points
3John75546080
4Mary75516780
5Deb75556787
6Kim33330330
7Tom75556785
Sheet3
 
Upvote 0
Try.
VBA Code:
Sub DistributeExttra()
Dim D, X, MaxPts
Dim Tr&, Tc&, Lr&

D = Range("B3:H4"): X = Range("J3:J4"): MaxPts = Range("B2:H2")
Lr = Range("A" & Rows.Count).End(xlUp).Row
For Tr = 1 To UBound(D, 1)
    For Tc = 1 To UBound(D, 2)
    If X(Tr, 1) = 0 Then Exit For
    If D(Tr, Tc) < MaxPts(1, Tc) Then
    D(Tr, Tc) = D(Tr, Tc) + 1
    X(Tr, 1) = X(Tr, 1) - 1
    End If
    Next Tc
If X(Tr, 1) > 0 Then Tr = Tr - 1
Next Tr
Range("B3:H4") = D: Range("J3:J4") = X
End Sub
 
Upvote 0
Modified Code. Try.
VBA Code:
Sub DistributeExttra()
Dim D, X, MaxPts, A
Dim Tr&, Tc&, Lr&

D = Range("B3:H4"): X = Range("J3:J4"): MaxPts = Range("B2:H2")
Lr = Range("A" & Rows.Count).End(xlUp).Row
For Tr = 1 To UBound(D, 1)
A = True
    For Tc = 1 To UBound(D, 2)
    If X(Tr, 1) = 0 Then Exit For
    If D(Tr, Tc) < MaxPts(1, Tc) Then
    D(Tr, Tc) = D(Tr, Tc) + 1
    X(Tr, 1) = X(Tr, 1) - 1
    If D(Tr, Tc) < MaxPts(1, Tc) Then A = False
    End If
    Next Tc
If X(Tr, 1) > 0 And A = False Then Tr = Tr - 1
Next Tr
Range("B3:H4") = D: Range("J3:J4") = X
End Sub
 
Upvote 0
I see what you are trying to achieve, but which students get the extra point, as there are 7 students and only 5 points ??
In the example, there are 2 students and each have 5 and 3 extra points respectively
 
Upvote 0
I see what you are trying to achieve, but which students get the extra point, as there are 7 students and only 5 points ??
In the example, there are 2 students and each have
See if this does what you want. Test with a copy of your worksheet.

VBA Code:
Sub Distribute_Extras()
  Dim p As Variant, Maxp As Variant, x As Variant
  Dim i As Long, j As Long, ubp2 As Long
 
  With Range("B3", Range("B3").End(xlToRight)).Resize(Range("A" & Rows.Count).End(xlUp).Row - 2)
    p = .Value
    ubp2 = UBound(p, 2)
    Maxp = .Rows(0).Value
    For j = 1 To ubp2
      Maxp(1, j) = Val(Split(Maxp(1, j))(0))
    Next j
    x = .Offset(, .Columns.Count + 1).Resize(, 1).Value
    For i = 1 To UBound(p)
    j = 1
      Do While x(i, 1) > 0 And j <= ubp2
        If p(i, j) < Maxp(1, j) Then
          p(i, j) = p(i, j) + 1
          x(i, 1) = x(i, 1) - 1
        End If
        If p(i, j) = Maxp(1, j) Then j = j + 1
      Loop
    Next i
    .Value = p
    .Offset(, .Columns.Count + 1).Resize(, 1).Value = x
  End With
End Sub

Here is my sheet after the code has been run

edge37.xlsm
ABCDEFGHIJ
1NameHW 1HW 2HW 3HW 4HW 5HW 6HW 7XTRA
27 points5 points5 points5 points6 points7 points8 points
3John75546080
4Mary75516780
5
Sheet1
Fantastic coding Peter, I'm almost there. I want to ask you if you could please see if your code can be modified for the following:

1) I'm including my basic gradebook with sample names and grades with the exact ranges of where any homeworks are going to be (C9:K38), in the image I have HW from C9:J33, but sometimes I could have only 5 HWs or 9 HWs in other periods, also the number of students vary between grades/sections. I need, if possible, if your code could still work defining the whole HW range, eventhough some rows or columns may not be filled with values (I tried your code (in the previous example) and there is a "Runtime error '13': Type mismatch" if I erase Mary's name and grades, or, if I delete HW7 then the code would do nothing. When there are no values in a row or column, the code wouldn't work)

2) Can you please modify your code so it fits directly into my gradebook I am sending you here? Range for all HWs= C9:K38; Range of Maximum Points for each HW: C8:K8; Range of EXTRA POINTS: O9:O38

Thank you very much for all this trouble. I hope I could explain myself here. I appreciate your assistance.
EXAMPLE 2.xlsm
ABCDEFGHIJKLMNOPQ
2
3
4
5
6123456789
7HW1HW2HW3HW4HW5HW6HW7HW8IPIP 2VALEXTRA POINTS
8#NOMBRE DEL ESTUDIANTE555555875510X
91Olivia555555875
102Ethan555455875
113Sophia555555873
124Liam55044586
135Ava54434485
146Mason532050805
157Emma555555874
168Noah555555873
179Isabella555555872
1810Lucas503350801
1911Mia020205805
2012Elijah555555874
2113Charlotte555555873
2214Benjamin555555872
2315Harper55534346
2416Oliver555555874
2517Amelia553444873
2618Henry555555872
2719Grace522433755
2820William555045874
2921Abigail555555873
3022Alexander055555872
3123Ella500054631
3224James555455875
3325Chloe540255865
34
35
36
37
38
10A (1)
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,159
Members
453,021
Latest member
Justyna P

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