Filtering a range, copying filtered cells to new sheet in same workbook

netrixuser

Board Regular
Joined
Jan 21, 2019
Messages
77
Office Version
  1. 365
Platform
  1. Windows
Hi all,

My goal is to filter a list of alerts [in worksheet "Scratch Sheet"], on column A - for the word NEW - and paste the filtered results to worksheet "Current Alerts" (same workbook)
I have the code below but odd things are happening.
Firstly
As it stands the code throws an error at the line for the copy/paste - but the code does actually paste the data.

[If I insert an "on error resume next" above that line the code runs]

The top row of the source data (on Scratch Sheet is getting copied irrespective of whether is says NEW or not.

To test I have 4 rows of data in Scratch Sheet:
Source.png


When I run the code the filter looks like this, row 1 is visible and the filter is starting at row 2?

Source with code.png


And when the code executes I get this on the Current Alerts sheet - row 1 has also been copied (confirmed by the "1" taken from Column B)
I'm assuming because of the Current Region / visible cells piece in the code

Destination.png


If there are no instances of NEW the filter still "filters" - I would need some sort of error checking to account for this - with four rows all set to CLOSED:

Source 4 Closed.png
when the filter runs with no "NEW" :
Source no new filtered.png


Now the code - I'm certain there is something "obvious" ......

VBA Code:
Sub CopyNew()
Dim ws As Worksheet
Set ws = ActiveWorkbook.Sheets("Scratch Sheet")
Dim ws1 As Worksheet
Set ws1 = ActiveWorkbook.Sheets("Current Alerts")

Dim lastRow As Long
Dim lastRow1 As Long

 
lastRow = ws.Range("A" & ws.Rows.Count).End(xlUp).Row
lastRow1 = ws1.Range("A" & ws1.Rows.Count).End(xlUp).Row

On Error Resume Next

ws.Range("A1").AutoFilter field:=1, Criteria1:="NEW"
ws.Range("A1").CurrentRegion.SpecialCells(xlCellTypeVisible).Copy ws1.Range("A" & (lastRow1))
'Sheets.Add
'ActiveSheet.Paste

 

ws.ShowAllData
Set ws = Nothing
Set ws1 = Nothing

End Sub

As always, thanks in advance for all who take the time to help me, it is always very much appreciated.
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Autofilter will always assume that the first row of your range is headers and you cannot alter that.
 
Upvote 0
Autofilter will always assume that the first row of your range is headers and you cannot alter that.
Thank you Rory - I suppose I'll either have to add an offset of some sort to the copy / paste section or scrap filters and use a loop of some sort for when active cell.value = NEW ?
 
Upvote 0
If your first row is really data, then you will have to insert an actual header row to use autofilter.
 
Upvote 0
If your first row is really data, then you will have to insert an actual header row to use autofilter.
Thanks RoryA,
New problem though - if I insert a header row before filtering, the copy command then includes the header row - how would I get around that ?
 
Upvote 0
Something like this should do it:

VBA Code:
Dim copyRange as range
set copyrange = ws.Range("A1").CurrentRegion
if copyrange.columns(1).specialcells(xlcelltypevisible).count > 1 then ' only copy if there is more than the header row visible
set copyrange = copyrange.resize(copyrange.rows.count - 1).offset(1) ' offset by one and reduce row count by one to skip header row
copyrange.copy destination:=ws1.Cells(lastRow1, "A")
end if
 
Upvote 0
Something like this should do it:

VBA Code:
Dim copyRange as range
set copyrange = ws.Range("A1").CurrentRegion
if copyrange.columns(1).specialcells(xlcelltypevisible).count > 1 then ' only copy if there is more than the header row visible
set copyrange = copyrange.resize(copyrange.rows.count - 1).offset(1) ' offset by one and reduce row count by one to skip header row
copyrange.copy destination:=ws1.Cells(lastRow1, "A")
end if
It certainly does - and also helps me with other sections of my overall code that I was struggling with - thank you RoryA !
 
Upvote 0

Forum statistics

Threads
1,215,071
Messages
6,122,963
Members
449,094
Latest member
Anshu121

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