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
 
See if this is what you want then.

VBA Code:
Sub Distribute_Extras_v2()
  Dim p As Variant, Maxp As Variant, x As Variant
  Dim i As Long, j As Long, ubp2 As Long
  
  With Range("C9:K38")
    p = .Value
    ubp2 = UBound(p, 2)
    Maxp = .Rows(0).Value
    x = Intersect(.EntireRow, Columns("O")).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
    Intersect(.EntireRow, Columns("O")).Value = x
  End With
End Sub

This is the result of the above code using the data from post 10.

edge37.xlsm
ABCDEFGHIJKLMNO
7HW1HW2HW3HW4HW5HW6HW7HW8IPIP 2VALEXTRA POINTS
8#NOMBRE DEL ESTUDIANTE555555875510X
91Olivia555555875
102Ethan555555874
113Sophia555555873
124Liam55044586
135Ava54434485
146Mason555050800
157Emma555555874
168Noah555555873
179Isabella555555872
1810Lucas513350800
1911Mia520205800
2012Elijah555555874
2113Charlotte555555873
2214Benjamin555555872
2315Harper55534346
2416Oliver555555874
2517Amelia555544870
2618Henry555555872
2719Grace554433750
2820William555445870
2921Abigail555555873
3022Alexander255555870
3123Ella510054630
3224James555555874
3325Chloe554255860
34
35
36
37
38
Post 10 (2)
 
Upvote 0
Solution

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Wow, fantastic! Works exactly like I need it to. I appreciate your assistance very much.

Thank you again, Peter_SSs

By the way, I'm trying to mark your post as the solution, I confirm such action and the post doesn't get marked. I'll keep trying
 
Last edited:
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
Thank you for your work, kvsrinivasamurthy
I tried both your codes and I noticed that if I increase the extra points when the code distributes them, they exceed the maximum points for the HW, also when when deleting one student's grades, or when deleting one HW the code do not work. Could this be fixed?
 
Upvote 0
In B2:H2 enter only numbers. Avoid text.
Then run the code.
1734186622622.png
 
Upvote 0
Wow, fantastic! Works exactly like I need it to. I appreciate your assistance very much.

Thank you again, @Peter_SSs
You are welcome. Thanks for the follow-up.


By the way, I'm trying to mark your post as the solution, I confirm such action and the post doesn't get marked. I'll keep trying
We have seen that happen a couple of times and are investigating. On this occasion I have marked the post for you.
 
Upvote 0

Forum statistics

Threads
1,224,819
Messages
6,181,153
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