Loop code with "Do Until" loops in it

Beachson

Active Member
Joined
Oct 28, 2009
Messages
468
I am trying to loop the long code below until ...
Code:
ActiveCell.Offset(1, -11) = ""
If I cannot loop the code using the condition then looping the code for a specified number of times will suffice ... I am a novice with VBA so forgive me if I am going at this wrong to begin with but my desired result can be achieved by simply looping the code below.

Thank you so much for any help

Code:
Let X = 0
 
 Do Until ActiveCell.Offset(0, -11) <> ActiveCell.Offset(1, -11)
 
 If ActiveCell.Offset(0, -11) = ActiveCell.Offset(1, -11) Then
          
 NextFree = Range("AD2:AD" & Rows.Count).Cells.SpecialCells(xlCellTypeBlanks).Row
 Range("AD" & NextFree).Select
    
 ActiveCell.Offset(0, -1).Select
 Selection.FormulaR1C1 = "=IF(RC[-2]>0,RC[-2],0)"
 ActiveCell.Offset(0, 1).Select
 Selection.FormulaR1C1 = _
 "=IF(AND(RC[-3]>0,RC[2]>-1,RC[3]>-1,RC[4]>-1,RC[5]>-1),RC[-3],0)"
 Selection.Copy
 Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
 :=False, Transpose:=False
 Application.CutCopyMode = False
 ActiveCell.Offset(0, -1).Select
 Selection.ClearContents
          
 Else
          
 ActiveCell.Offset(0, -2).Select
 Selection.FormulaR1C1 = _
 "=IF(SUMIF(R2C18:R1000C18,RC[-10],R2C27:R1000C27)=SUMIF(R2C18:R1000C18,RC[-10],R2C30:R1000C30),RC[2],0)"
 Selection.Copy
 Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
 :=False, Transpose:=False
 Application.CutCopyMode = False
 ActiveCell.Offset(0, 1).Select
 Selection.ClearContents
          
 End If
          
 X = X + 1
 
 Loop
          
 ActiveCell.Offset(0, -1).Select
     
 Do Until ActiveCell.Offset(0, -10) <> ActiveCell.Offset(-1, -10)
     
 ActiveCell.Offset(0, -10) = ActiveCell.Offset(-1, -10)
 Selection.FormulaR1C1 = _
 "=IF(SUMIF(R2C18:R1000C18,RC[-10],R2C27:R1000C27)=SUMIF(R2C18:R1000C18,RC[-10],R2C30:R1000C30),RC[2],0)"
 Selection.Copy
 Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
 :=False, Transpose:=False
 Application.CutCopyMode = False
 ActiveCell.Offset(-1, 0).Select
     
 X = X + 1
 
 Loop
        
 Selection.FormulaR1C1 = _
 "=IF(SUMIF(R2C18:R1000C18,RC[-10],R2C27:R1000C27)=SUMIF(R2C18:R1000C18,RC[-10],R2C30:R1000C30),RC[2],0)"
 Selection.Copy
 Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
 :=False, Transpose:=False
 Application.CutCopyMode = False
 ActiveCell.Offset(0, 1).Select
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
You could just add

Do Until ActiveCell.Offset(1, -11) = "" after Let x = 0

and Loop after your first or 2nd loops depending what you are trying to achieve.
 
Upvote 0
You could just add

Do Until ActiveCell.Offset(1, -11) = "" after Let x = 0

and Loop after your first or 2nd loops depending what you are trying to achieve.

Would I just add that to the beginning of the code? If I do that I get a "Compile Error. Expected end of statement" on the "After" part of the code.
 
Upvote 0
May be a misunderstanding here. Apologies for wording that poorly

Add

Code:
[COLOR=#333333][I]Do Until ActiveCell.Offset(1, -11) = "" [/I][/COLOR]

after the line

Code:
[COLOR=#333333][I]Let x = 0[/I][/COLOR]

and then

Code:
Loop

after one of your other loops
 
Last edited:
Upvote 0
May be a misunderstanding here. Apologies for wording that poorly

Add

Code:
[COLOR=#333333][I]Do Until ActiveCell.Offset(1, -11) = "" [/I][/COLOR]

after the line

Code:
[COLOR=#333333][I]Let x = 0[/I][/COLOR]

and then

Code:
Loop

after one of your other loops

That looped my code properly. Thanks you so much for the help!
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,633
Latest member
DougMo

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