Results 1 to 5 of 5

How can I add a condition in my loop that does not work?

Threaded View

  1. #1
    Registered User
    Join Date
    01-08-2021
    Location
    Mexico
    MS-Off Ver
    2013
    Posts
    7

    How can I add a condition in my loop that does not work?

    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:

    Captura.PNG

    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:

    Sin título.png

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

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Add a blank row after the loop done the work and run the loop from the next row
    By IonutC in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-16-2016, 04:31 AM
  2. Skip in loop if condition in loop is met
    By jakopak in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-17-2015, 10:41 AM
  3. VBA Code for populating cells from one Work Book A to another Work Book with condition
    By ray.kanata in forum Excel Programming / VBA / Macros
    Replies: 20
    Last Post: 06-22-2015, 03:46 PM
  4. Multiple Condition Do Loop
    By redpanda in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-01-2011, 02:29 PM
  5. Loop until condition is met
    By Toimia in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 03-08-2011, 08:39 PM
  6. Add a second condition to loop
    By Casey in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-06-2006, 07:00 PM
  7. Using For loop to condition
    By bundyloco in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-19-2005, 03:05 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1