Option Explicit
Sub Organizza()
Dim i As Long, o As Long, z As Long
Dim NcXt As Long, TotaT As Long, Npre As Long
Dim tavoli As Long
Dim Nscala As Long
Range("C3:ZZ1000").ClearContents
tavoli = Cells(3, 2)
NcXt = Cells(1, 2).Value
Npre = Cells(Rows.Count, 1).End(xlUp).Row
For i = 4 To 3 + tavoli
Cells(4, i) = "Tav. " & i - 3
Next i
''------------
For o = 5 To Npre
If Cells(o, 2) > NcXt Then
For i = 4 To 3 + tavoli
TotaT = Application.Sum(Range(Cells(5, i), Cells(Npre, i)))
If TotaT = 0 Then
Cells(o, i) = NcXt
Nscala = Cells(o, 2) - NcXt
For z = 1 To Fix(Cells(o, 2) / NcXt)
If Nscala >= NcXt Then
Cells(o, i + z) = NcXt
Nscala = Nscala - NcXt
Else
Cells(o, i + z) = Nscala
End If
Next z
Exit For
End If
Next i
End If
Next o
''--------------
For i = 4 To 3 + tavoli
TotaT = Application.Sum(Range(Cells(5, i), Cells(Npre, i)))
For o = 5 To Npre
If Cells(o, 3) = "" Then
If TotaT + Cells(o, 2) <= NcXt Then
Cells(o, i) = Cells(o, 2)
Cells(o, 3) = "X"
TotaT = TotaT + Cells(o, 2)
End If
End If
Next o
Cells(3, i) = Application.Sum(Range(Cells(5, i), Cells(Npre, i)))
Next i
End Sub
|