For loop with nested Do loop, For loop not working

capson

Board Regular
Joined
Jul 9, 2010
Messages
107
Hello,

I am trying to use a Do loop for the first time and having some difficulty

I want to exit the Do loop when
Code:
.Cells(irow, icol) = ""
and advance to the next row

My code only works for the irow = 2, it does not advance i.e. next irow does not seem to be read or executed

The For loop is not working and not sure why

The Do loop is giving me what I want

Thanks for any help on this


Code:
Sub testDoLoop()
Dim ws As Worksheet
Dim cnt As Long, irow As Long, icol As Long, LR As Long, LC As Long


Set ws = ThisWorkbook.Sheets("Elements")
      LC = 149
      LR = 276
    
  cnt = 1
  icol = 13
  With ws
    For irow = 2 To LR


     Do While icol <= LC
       If .Cells(irow, icol) <> "" Then
         .Cells(irow, icol).value = "sm-" & cnt & "|" & .Cells(irow, icol).value
         cnt = cnt + 1
         icol = icol + 9
       Else
       
        Exit Do
      End If
    Loop


  Next irow
End With


End Sub
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
When I run the code the For loop is definitely working.

Have you tried stepping through the code with F8 to see what's actually happening?
 
Upvote 0
Hia, both loops are working fine, the problem is your not re-setting icol after the do loop.
Add this line as shown
Code:
    Loop
   [COLOR=#0000ff] icol = 13
[/COLOR]
  Next irow
Another option is to use For each loops for both like this
Code:
    With ws
        For irow = 2 To LR
            For icol = 13 To LC Step 9
                If .Cells(irow, icol) <> "" Then
                    .Cells(irow, icol).Value = "sm-" & cnt & "|" & .Cells(irow, icol).Value
                    cnt = cnt + 1
                Else
                    Exit For
                End If
            Next icol
        Next irow
    End With
 
Last edited:
Upvote 0
If you want to switch to a nested For Loop, (which resets to 13 as @Fluff suggests), try:
Rich (BB code):
Sub testDoLoop()

    Dim cnt     As Long
    Dim x       As Long
    Dim y       As Long
    Dim LR      As Long: LR = 276
    Dim LC      As Long: LC = 149
    
    Application.ScreenUpdating = False
    
    With Sheets("Elements")
        
        For x = 2 To LR
            For y = 13 To LC Step 9
                If Len(.Cells(x, y)) > 0 Then
                    .Cells(x, y).Value = "sm-" & cnt & "|" & .Cells(x, y).Value
                    cnt = cnt + 1
                End If
            Loop
        Next x
        
    End With
    
    Application.ScreenUpdating = True
    
End Sub
 
Last edited:
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,197
Members
453,021
Latest member
pingpong7117

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top