netrixuser
Board Regular
- Joined
- Jan 21, 2019
- Messages
- 77
- Office Version
- 365
- Platform
- 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:
When I run the code the filter looks like this, row 1 is visible and the filter is starting at row 2?
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
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:
when the filter runs with no "NEW" :
Now the code - I'm certain there is something "obvious" ......
As always, thanks in advance for all who take the time to help me, it is always very much appreciated.
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:
When I run the code the filter looks like this, row 1 is visible and the filter is starting at row 2?
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
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:
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.