Looping within a loop

shelim481

New Member
Joined
Aug 30, 2018
Messages
28
Hi need some help ...

Say i have loop as below, but what i want is to run another loop within this loop so that every time the original loop reaches 10 cycles it wold run the second loop, but on the final time the original loop may not run 10 cycles so i would want the second loop to run even if 10 cycles has not been completed just for the last one..
maybe use a count etc?

Code:
   For Each d1 In rng1        For Each d2 In rng2
            On Error Resume Next
			
			
                ActiveWorkbook.Sheets.Add After:=Worksheets(Worksheets.Count)
                Worksheets(Worksheets.Count).Name = d2 & "-" & d1
                oldD = d1.Offset(0, 1)
                oldU = d2.Offset(0, 1).Value
                Sheets("Temp").Select
                ActiveSheet.UsedRange.Copy
                Worksheets(Worksheets.Count).Select
                Range("B1").Select
                Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                :=False, Transpose:=False
                Cells.Replace What:= c1.Offset(0, 1).Value, Replacement:=& c1.Value, LookAt:=xlPart, _
                    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
                    ReplaceFormat:=False
                Cells.Replace What:= & c2.Offset(0, 1).Value, Replacement:= & c2.Value, LookAt:=xlPart, _
                   SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
                    ReplaceFormat:=False
				Sheets.Add After:=Sheets(Sheets.Count)
                Worksheets(Worksheets.Count).Select
				
				'new loop here
				
				
				
				
				
	




        Next d2
    Next d1
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
You can add a loop counter, i.e.
Code:
Dim ctr as Long
For Each d1 In rng1
'   Increment loop counter
    ctr = ctr + 1
'   Do first loop stuff here
'   ...
'   Check to see if loop counter is a multiple of 10
    If ctr Mod 10 = 0 Then
'       Run second loop
        For each d2 in rng2
'           Do second loop stuff here
'           ...
        Next d2
    End If
Next d1
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,286
Members
452,631
Latest member
a_potato

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