PDA

View Full Version : How can I add a condition in my loop that does not work?



xxkid
01-09-2021, 03:38 PM
Hey folks!

I've a problem with my code, basically this loop distribute a known number evenly 1 by 1 across a range where the known number is given by "TV Comodin" Row (in color Red) until the rows reach to 100 (limit) or the known number reach to 0 , the data set is as follow:

27701

My code works, it identify the minimum value and then distribute the known number evenly 1 by 1 across the range, and once the known number reach to 100 or the known number gets to 0 it stops, the problem here is that the loop just should be adding the number 1 by 1 and stopping when it finish in the rows with the AB Category (given by the "SEGMENT" column ") . When I try to add this rule, it just not works and keeps adding the number in all the rows along the range, here is my try:


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
myvar = 100


For j = j + 1 To Lastrow
If Range("I" & j) = "CD" And Range("B" & j) <= myvar Then
For i = i + 1 To Lastrow
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 And Range("I" & j) = "CD"
mn = Application.Min(rng)
If mn >= myvar 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
Next i
End If
Next j



the desired out put would be:

27702

any idea? hope you can help me guys, thanks ( I attached the data set, )