Forum Discussion

Neels13286's avatar
Neels13286
Copper Contributor
Mar 01, 2021

Nested Loop

Hi,  I am trying to do a nested Loop with a Do Until and a While.  See attached.  

when starting the loop it ignores the outside loop but do the inside loop perfectly.   

Outside loop to be done for every name while the inside loop do the name for a specified amount of times.

Please help 

3 Replies

  • JMB17's avatar
    JMB17
    Bronze Contributor

    Neels13286 

     

    If order isn't important, and assuming your names are in B4:B7, number of tickets is in Column H, and you want the list to start in O3 (change ranges as needed), then you could try:

     

    Sub test()
         Dim cell As Range
         Dim dest As Range
         
         Set dest = Range("O3")
         
         For Each cell In Range("B4:B7")
              If Len(dest.Value) > 0 Then
                   Set dest = Cells(Rows.Count, dest.Column).End(xlUp)(2, 1)
              End If
              
              dest.Resize(Intersect(cell.EntireRow, Range("H:H")).Value, 1).Value = cell.Value
              
         Next cell
         
    End Sub

     

    • Neels13286's avatar
      Neels13286
      Copper Contributor

      JMB17 

      Thanks for your help.  It works perfectly.  I just had to remove the "end if" statement.  It presented a compile error " End If without block If " 

       

      In your answer, how did you tell the system to dublicate the name eg 7 times ?  I don't get that part of the makro.

       

      Regards

      • JMB17's avatar
        JMB17
        Bronze Contributor
        By resizing the range: destCell.Resize(rowsize, colsize) where rowsize equals the value that is in Column H. Excel can read/write a block of cells at once - it's Range("O3:O9").Value = "Name" instead of doing one cell at a time.

Resources