Compile Error: Loop Without Do - Looping with multiple GoTo statements

SBF12345

Well-known Member
Joined
Jul 26, 2014
Messages
614
Greetings,

I am receiving a loop without Do error at the second "loop". Is it possible to loop to the same initial "Do Until" statement from more than one point? I would like to loop back to the initial point from each GoTo statement.

Code:
k = Range(Cells(Rows.End(xlUp), b))    
    Do Until k = 1
    
    If Range(Cells(k, b)) <> "" Then GoTo First Else GoTo Second
    
First:
    
        Set h = Range(Cells(k, Column.End(xlToRight)))
        k = k - 1
        
        
        Loop
          
          
Second:
    
        h.Copy
        Range(Cells(k, b)).PasteSpecial Paste:=xlPasteValues
        
        k = k - 1
        
        
        Loop
    
    
End If
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
How's this?

Code:
k = Range(Cells(Rows.End(xlUp), b))
Do Until k = 1
    
    If Range(Cells(k, b)) <> "" Then
        Set h = Range(Cells(k, Column.End(xlToRight)))
        k = k - 1
    Else
        h.Copy
        Range(Cells(k, b)).PasteSpecial Paste:=xlPasteValues
        k = k - 1
    End If
   
Loop
 
Last edited:
Upvote 0
HI,

Apologies if this seems critical, but since whenever I first started learning to write code I've been advised (on pain of something unpleasant) to never ever use a goto statement. The solution provided by Mrshl clearly avoids such a construct. The danger with using them is that in anything complicated you end up with a 'string vest' of code that is almost indecipherable.

Just an observation.

Regards
 
Upvote 0

Forum statistics

Threads
1,223,249
Messages
6,171,031
Members
452,374
Latest member
keccles

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