I've been working on a VBA macro to copy data that matches certain criteria from one worksheet to another worksheet without altering the original worksheet.
I'm locating the last row from worksheet "Prospects" and selecting the criteria that I need and it copies over to the other worksheet "Results", but both worksheets look identical.
So any rows that don't meet the filter criteria are removed from the original worksheet "Prospects".
I need the original worksheet to remain unaltered. I'm also just capturing certain columns, thus hiding the columns that I don't need on the "Results" worksheet.
Sub ProspectList()
Dim r As Range
Dim ws As Worksheet
Set ws = ActiveSheet
ws.Range("A1").AutoFilter
LastRow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
LastCol = ActiveSheet.Cells(1, Columns.Count).End(xlUp).Column
With Sheets("Prospect List").Range([A2], [A2].SpecialCells(xlCellTypeLastCell))
ws.Range("A1").AutoFilter field:=13, Criteria1:="Pipeline"
[B:B].EntireColumn.Hidden = True
.Copy
[C:C].EntireColumn.Hidden = True
.Copy
[E:E].EntireColumn.Hidden = True
.Copy
[H:H].EntireColumn.Hidden = True
.Copy
[I:I].EntireColumn.Hidden = True
.Copy
[K:K].EntireColumn.Hidden = True
.Copy
[L:L].EntireColumn.Hidden = True
.Copy
[B:B].EntireColumn.Hidden = False
[C:C].EntireColumn.Hidden = False
[E:E].EntireColumn.Hidden = False
[H:H].EntireColumn.Hidden = False
[I:I].EntireColumn.Hidden = False
[K:K].EntireColumn.Hidden = False
[L:L].EntireColumn.Hidden = False
End With
With Sheets("Results")
If .Cells(Sheets(1).Rows.Count, 1).End(xlUp) = "" Then 'it's a clean sheet
.Cells(Sheets(1).Rows.Count, 1).End(xlUp).PasteSpecial Paste:=xlPasteValues
Else
.Cells(Sheets(1).Rows.Count, 1).End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteValues
End If
End With
Application.CutCopyMode = False
End Sub
ActiveSheet.Cells(1, Columns.Count).End(xlUp).Column
. I think you should be usingxlToLeft
– CallumDACopy
command. Therefore it isn't apparent what you paste to the 'Result' sheet. But if your intention is to omit certain columns in the latter, hiding or deleting them should be done after thePaste
and not on the original sheet's data. – Variatus