PDA

View Full Version : [SOLVED:] Loop through inserting row from one sheet to another, at least 2 loops needed?



SteveM99
06-01-2022, 07:03 AM
Need help with coding how to run through a range of rows needed to be inserted in another sheet (same workbook) knowing the number of rows to insert and what row they should be inserted at. Seems not too difficult but I cant understand how to write the loop code. I have attached my very simple start approach but get confused on how to loop through one area to then loop through another area and keep updating the next item in each loop.

SteveM99
06-03-2022, 08:40 AM
Had someone help with this and am posting the answer herein for others....


Sub Loops()
Dim lr As Long
Dim ws As Worksheet, wd As Worksheet
Set ws = Sheets("S1")
Set wd = Sheets("S2")
Dim ls As Long, ld As Long
ls = ws.Range("F" & Rows.Count).End(xlUp).Row
Dim r As Long
For r = 5 To ls
Dim r2 As Long
ld = wd.Range("N" & Rows.Count).End(xlUp).Row
For r2 = ld To 1 Step -1
If wd.Range("N" & r2).Value = ws.Range("F" & r).Value Then
If ws.Range("I" & r).Value <> "" Then
Dim c As Long
For c = 1 To ws.Range("I" & r).Value
wd.Rows(r2 & ":" & r2).Insert Shift:=xlDown
Next c
Exit For
End If
End If
Next r2
Next
End Sub

Aussiebear
06-03-2022, 02:26 PM
Thank you for posting your solution

snb
06-05-2022, 07:40 AM
Sufficient:


Sub M_snb()
For Each it In Sheet2.Columns(1).SpecialCells(2).Areas
it.CurrentRegion.Copy Sheet2.Cells(1 + 9 * n, 10)
n = n + 1
Next
End Sub