Copy visible rows with 2 criteria from one sheet and paste in another on first empty row

PhilRJ

New Member
Joined
Jun 12, 2015
Messages
13
Hi Folks,

Total newbie here with VBA.
Basically, I'm looking to copy rows (that meet 2 criteria under a same column header) from one sheet ("Source") to the first empty row in a second spreadsheet ("Target") in the same workbook. I have added the sample spreadsheet (linked below from dropbox) so you can visualize it. I think it can be done with autofilter and then copying and pasting to the other spreadsheet. But not sure how.

Both sheets have the same headers (Method, Type, Booking Date, Days to Trip, Cost).
However, the source headers are in the range B6 to F6 while the target headers are in the range A1 to E1.

Again, this is just a small sample. The real data set is massive but this example illustrates the problem well. I need a macro to copy rows from Source that have the following criteria: either leisure or business under column 2 (ie, type).

Mind you, the example shows source sheet with only 6 rows beneath the header. In reality, the amount of rows in the source changes daily (ie, dynamic) because it's generated by input formulas (you won't see these formulas in my sample spreadsheet but they're unecessary). Initially, when I tried to copy to the target, I realized Excel didn't actually paste to the NEXT EMPTY ROW because it considered blank cells often times to be NON EMPTY (because they contain formulas that generate zero values, and, as such, cannot be technically considered empty). Hence, as I ran my initial macro, the first paste was fine (ie, next empty row), but the row pastings thereafter (ie, additional executions of the macro) generated huge gaps between the old and new chunks of rows posted to the target sheet because Excel doesn't equate blank cells (resulting from " " results) as empty cells.

The workaround I found was to set up a conditional in the generating of the rows in the source sheet, whereby rows that are empty generate "EMPTY" readings. This allowed me to paste to TARGET the whole set of rows and add a final command to delete all rows that were called EMPTY. This works. The problem is the macro takes time to run and crashes every now and then.

I've spent hours on this to no avail. :(

Much appreciate the help!


Sub CopyPasteRowstoNextEmptyRow()


Dim lastCol As Long, lastRow As Long

With Sheets("Source")
lastCol = .Cells(7, .Columns.Count).End(xlToLeft).Column
lastRow = .Cells(.Rows.Count, "F").End(xlUp).Row
.Range("B6", .Cells(lastRow, lastCol)).Copy
End With


With Sheets("RegistroTotal")
lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
If lastRow = 1 And .Cells(1) = "" Then lastRow = 0
.Cells(lastRow + 1, "A").PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
End With

Application.CutCopyMode = False

Dim ws As Worksheet
Dim rng As Range
Dim LR As Long


Set ws = ActiveWorkbook.Sheets("RegistroTotal")


LR = ws.Range("A" & ws.Rows.Count).End(xlUp).Row


Set rng = ws.Range("A1:A" & LR)


' filter and delete all but header row
With rng
.AutoFilter Field:=1, Criteria1:="EMPTY"
.Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
End With


' turn off the filters
ws.AutoFilterMode = False



End Sub


Heres the example of the spreadsheet.
https://www.dropbox.com/s/jnj34wfbp6sr8dr/Example.xlsx?dl=0


Phil
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.

Forum statistics

Threads
1,214,904
Messages
6,122,169
Members
449,070
Latest member
webster33

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