Copy / Paste Macro only for rows where data exists in a given range

mikestorm

New Member
Joined
Feb 3, 2021
Messages
12
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi all, first off thank you in advance for reading and considering to help. I am looking for assistance on a copy paste macro to copy data to a new sheet called 'Export' (destination tab already exists) but to only copy rows where data exists in columns E through N.

Below is a visual representation of what I mean. Source tab, not all rows will have data populated.
Example.png


After the macro fires, Export tab would look like this. It's important to note on the source tab there are data in columns to the right of Column N (Criteria 8) but only through column N in the row should be copied.
example2.png


That's essentially it. Any guidance would be very much appreciated!
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
try this on a copy of your file:

VBA Code:
Sub Do_it()

Dim rs As Worksheet
Set rs = Worksheets("Export")

lr = Cells(Rows.Count, "A").End(xlUp).Row
For r = 4 To lr
If WorksheetFunction.CountA(Range(Cells(r, "E"), Cells(r, "N"))) > 0 Then

rr = rs.Cells(Rows.Count, "A").End(xlUp).Row + 1
Range("A" & r & ":N" & r).Copy rs.Range("A" & r & ":N" & r)

End If
Next

End Sub

Hth,
-Ross
 
Last edited:
Upvote 0
try this on a copy of your file:

VBA Code:
Sub Do_it()

Dim rs As Worksheet
Set rs = Worksheets("Export")

lr = Cells(Rows.Count, "A").End(xlUp).Row
For r = 4 To lr
If WorksheetFunction.CountA(Range(Cells(r, "E"), Cells(r, "N"))) > 0 Then

rr = rs.Cells(Rows.Count, "A").End(xlUp).Row + 1
Range("A" & r & ":N" & r).Copy rs.Range("A" & r & ":N" & r)

End If
Next

End Sub

Hth,
-Ross

Hi Rob, thank you. This does move the pertinent records to the export tab, but it preserves the relative spacing of the rows with data, as well as the formatting (I tweaked your code to grab the header also on row 3).
results1.png


Was hoping data could be written in an unformatted way starting on A1 without row spaces like the output of my OP:
example2.png
 
Upvote 0
oops

Range("A" & r & ":N" & r).Copy rs.Range("A" & r & ":N" & r)

should be

Range("A" & r & ":N" & r).Copy rs.Range("A" & rr & ":N" & rr)
 
Upvote 0
Solution
Thank you! I tweaked it slightly as it was pasting formats/formulas and also not on row 1 of "Export" but you definitely got me there!

VBA Code:
Sub Do_it()

Dim rs As Worksheet
Set rs = Worksheets("Export")

lr = Cells(Rows.Count, "A").End(xlUp).Row
For r =3 To lr
If WorksheetFunction.CountA(Range(Cells(r, "E"), Cells(r, "N"))) > 0 Then

rr = rs.Cells(Rows.Count, "A").End(xlUp).Row + 1
Range("A" & r & ":N" & r).Copy
rs.Range("A" & rr - 1 & ":N" & rr).PasteSpecial Paste:=xlPasteValues


End If
Next

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,006
Messages
6,122,666
Members
449,091
Latest member
peppernaut

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