I'm trying to distribute a known number evenly 1 by 1 across a range where the known number is given by "TV Comodin" Row (in color Red), the data set is as follow:
| TV Comodín | L | M | SEGMENTO |
|---|---|---|---|
| Second | 20 | 30 | CD |
| First | 10 | 30 | AB |
| Second | 80 | 30 | AB |
| TV Comodín | 500 | 500 | COMODIN |
The cell has a limit depending on its category given by the column I, "SEGMENTO".
AB category must to be <= 100 and CD category must to be <= 120.
Sub prueba()
Dim f As Range, ws As Worksheet, comodin As Long, rng As Range, m
Set ws = ActiveSheet
Set rng = ws.Range("A2", ws.Range("A2").End(xlDown)).Offset(0, 1)
Set f = ws.Columns("A").Find(What:="TV Comodín", LookIn:=xlFormulas, _
LookAt:=xlWhole, MatchCase:=False)
lastRow = Range("I" & Rows.Count).End(xlUp).Row
For i = i + 1 To lastRow
If Range("I" & i) = "AB" Then
If Not f Is Nothing Then
rng.Value = ws.Evaluate("=" & rng.Address() & "*1") 'fill empty cells with zeros
comodin = f.Offset(0, 1).Value
Do While comodin > 0
mn = Application.Min(rng)
If mn >= 100 Then Exit Do ' exit when no values are <100
m = Application.Match(mn, rng, 0)
rng.Cells(m).Value = rng.Cells(m).Value + 1
comodin = comodin - 1
f.Offset(0, 1).Value = comodin
Loop
Else
MsgBox "No found"
End If
End If
Next i
End Sub
It works with the first condition (AB <= 100).
I tried to add the other condition (CD <= 120) by using Elseif inside the loop.
Desired output
| TV Comodín | L | M | SEGMENTO |
|---|---|---|---|
| Second | 120 | 30 | CD |
| First | 100 | 30 | AB |
| Second | 100 | 30 | AB |
| TV Comodín | 200 | 500 | COMODIN |