My task is to create a tool that, based on (specific) input data, will generate a 'grid' of employees' working hours and spit out specific shifts 6-14; 7-15; 10-20 etc etc.
The general operation of such a macro, let's say I have one, the problem begins when the macro must have a requirement that in a given row (the row is one employee) the sum cannot be greater than 10.
Below I paste the code I managed to create, which fills in the values accordingly based on the input data, but without this requirement (row sum not greater than 10).
Sub AutoFill()
Dim ws As Worksheet
Dim col As Integer
Dim row As Integer
Dim suma As Integer
Dim liczba_osob As Integer
' Ustaw arkusz, na którym chcesz działać
Set ws = ThisWorkbook.Sheets("01")
' Wyczyść zakres G5:AD36
ws.Range("G5:AD36").ClearContents
' Iteruj przez kolumny od G do Y
For col = 7 To 30
' Pobierz liczbę osób w pracy w danej godzinie
liczba_osob = ws.Cells(2, col).Value
' Ustaw sumę kolumny na 0
suma = 0
' Uzupełnij komórki wartością 1 do momentu, gdy suma wiersza będzie równa liczbie osób w pracy
For row = 5 To 36
' Sprawdź, czy suma nie przekracza liczby osób w pracy
If suma < liczba_osob Then
ws.Cells(row, col).Value = 1
suma = suma + 1
Else
' Jeśli suma przekroczyła liczbę osób w pracy, zostaw komórkę pustą
If Not IsEmpty(ws.Cells(row, col)) Then
ws.Cells(row, col).ClearContents
End If
End If
Next row
Next col
End Sub
There are also screenshots below in the attachments - in one there is the addition of "1" by the macro, in the other the addition of "1" by me in the way the macro should be supplemented.
Yes, as part of the implementation, the number of employees in each hour is given in the G2:AD2 range, and the hours are listed in G1:AD1.
Can anyone help me or guide me somehow to make it work?
Unless it is possible to completely omit all these "1"s and directly generate individual changes 6-14, 7-15, etc. based on the demand given in G2:AD2...
I need solution for filling like in screenshot.



it seems to me you could achieve your goal by means of formulas
if you insert one column and one row you could use this formula
or you can keep your original rows and columns layout and adopt this other formula:
Finally here's the corresponding amendment of your code (forgive the polish):
and here's a refactoring of that code: