寫了段vba程序,可以實現妳要的功能。沒考慮健壯性和擴展性,可能需要自己再改改。
Sub Cal_Click()
Dim hours As Double
Dim initj As Integer
For i = 5 To 20 Step 1
If Len(Sheet2.Cells(i, 1)) = 0 Or Len(Sheet2.Cells(i, 2)) = 0 Then
Exit For
End If
hours = Sheet2.Cells(i, 2)
initj = Sheet2.Cells(i, 1) - 1
Do While hours > 0 '遍歷列
For j = 1 To 5 Step 1
Dim col As Integer
Dim sumcolum As Double
col = initj * 5 + 2 + j
sumcolum = 0
For k = 5 To i - 1 Step 1
sumcolum = sumcolum + Sheet2.Cells(k, col)
Next k
If sumcolum < Sheet2.Cells(4, col) Then
Sheet2.Cells(i, col) = Application.Min(Sheet2.Cells(4, col) - sumcolum, hours)
hours = hours - Sheet2.Cells(i, col)
End If
If hours <= 0 Then Exit For
Next j
initj = initj + 1
If Len(Sheet2.Cells(4, initj * 5 + 2)) = 0 Then Exit For
'MsgBox (initj)
Loop
Next i
End Sub
最終運行結果如下: