Looping code pasting onto same line

mramos17

New Member
Joined
Oct 14, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
New to excel/vba, not too familiar with coding, & many other generic openers you've seen.
I'm using a loop to copy an entire row to another sheet if a criteria is met but my code keeps pasting the data into the same row, overwriting the previous entry. Not sure if the EntireRow.Delete argument is necessary, I was following a tutorial and they had said that looping from the bottom up is preferred & deleting rows as the data is copied is necessary for this loop to work.

____________________________________________________________________
Context:
Sheet = Report - Has data
Sheet = final - Destination for the copied rows
____________________________________________________________________


Sub Loop_Macro()

'Find last row
LR = Sheets("report").Range("B" & Rows.Count).End(xlUp).Row

'step 2 - sets spacing between rows read
For I = LR To 100 Step -1

'Find last row on destination sheet
Sheet1_LR = Sheets("final").Cells(Rows.Count, 1).End(xlUp).Row

'Logic statements for copying
If Sheets("report").Cells(I, 2).Value = "Pay Type" Then
Sheets("report").Cells(I, 1).EntireRow.Copy Sheets("final").Cells(Sheet1_LR + 1, 1).Offset(-1, 0)
Sheets("report").Cells(i, 1).EntireRow.Delete
End If

Next I
End Sub
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
To start with, replace this
VBA Code:
If Sheets("report").Cells(I, 2).Value = "Pay Type" Then
with this
VBA Code:
 If Sheets("report").Cells(1, 2).Value = "Pay Type" Then
and this
VBA Code:
Sheets("report").Cells(I, 1).EntireRow.Copy Sheets("final").Cells(Sheet1_LR + 1, 1).Offset(-1, 0)
with this
VBA Code:
Sheets("report").Cells(I, 1).EntireRow.Copy Sheets("final").Cells(Sheet1_LR + 1, 1)

and reassess.

(Tip: For future posts , you should try to use code tags like I did above when posting your code. It makes it easier to read.)

 
Upvote 0
Solution
Wonderful, I appreciate it. Can you explain the reasoning behind changing the I to a 1 within the first If statement? I'm trying to get away from copy/pasting code & have an understanding of what I'm doing.
 
Upvote 0
Wonderful, I appreciate it. Can you explain the reasoning behind changing the I to a 1 within the first If statement? I'm trying to get away from copy/pasting code & have an understanding of what I'm doing.

Your code indicates you only want to do the copy if the column header in column 2 (Col B) is "Pay Type". That information is ALWAYS going to be in row #1 so the statement checking to see what the column header is must be Cells(1,2), not Cells(I,2) since I is the looping variable and thus will change.
 
Upvote 0

Forum statistics

Threads
1,214,929
Messages
6,122,315
Members
449,081
Latest member
tanurai

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