Lil Stinker
Board Regular
- Joined
- Feb 16, 2022
- Messages
- 151
- Office Version
- 2019
- Platform
- Windows
Hey folks,
I'm looking for a way to copy the results from an AdvancedFilter run on Sheet1 to Sheet2. The code I currently have works to copy the data over however, it overwrites any existing data on Sheet2. My other issue with this is that for my sample of 196 rows of data (15 columns), it is taking approx. 24 seconds to run which is exceedingly long.
What I would like for it to do is copy over the result data to Sheet2 without overwriting any existing data. And preferably faster as it is intended to run in the background without the user's knowledge. I have tried deleting the Advanced Filter's criteria and extract ranges from the Named Manager as well as disabling ScreenUpdating, EnableEvents and turned Calculation to manual prior to running the code again but all of that seems to increase the amount of time. In fact, doing so actually locks up Excel entirely to where I get the (Excel Not Responding) message. I have also tried simply changing the Advanced Filter's CopyTo:= range to Sheet2 but that too overwrites everything.
I have established the last row and last result row on Sheet1 when the filter runs but when I try to establish a last row for Sheet2, all I seem to get is one new row of #N/A. Here is the code, any help would be appreciated:
I'm looking for a way to copy the results from an AdvancedFilter run on Sheet1 to Sheet2. The code I currently have works to copy the data over however, it overwrites any existing data on Sheet2. My other issue with this is that for my sample of 196 rows of data (15 columns), it is taking approx. 24 seconds to run which is exceedingly long.
What I would like for it to do is copy over the result data to Sheet2 without overwriting any existing data. And preferably faster as it is intended to run in the background without the user's knowledge. I have tried deleting the Advanced Filter's criteria and extract ranges from the Named Manager as well as disabling ScreenUpdating, EnableEvents and turned Calculation to manual prior to running the code again but all of that seems to increase the amount of time. In fact, doing so actually locks up Excel entirely to where I get the (Excel Not Responding) message. I have also tried simply changing the Advanced Filter's CopyTo:= range to Sheet2 but that too overwrites everything.
I have established the last row and last result row on Sheet1 when the filter runs but when I try to establish a last row for Sheet2, all I seem to get is one new row of #N/A. Here is the code, any help would be appreciated:
VBA Code:
Sub INV_historyfilter()
Dim lastNEWITEM As Long, HSTrow As Long, HSTcol As Long
advINVHISTfilterCLEAR 'clears adv filter criteria and extract ranges
With Inventory 'sheet1
lastROW = .Range("A1048576").End(xlUp).Row + 1 'last item row
If lastROW < 3 Then GoTo NoNew
.Range("A2:Q" & lastROW).AdvancedFilter xlFilterCopy, .Range("CD1:CD2"), .Range("CF2:CV2"), Unique:=True
lastRESULTROW = .Range("CF99999").End(xlUp).Row
If lastRESULTROW < 3 Then GoTo NoNew
'''copy results to history sheet'''
'HSTrow = Inventory1.Range("Q1048576").End(xlUp).Row + 1 'last item row <---tried replacing lastNEWITEM with HSTrow below--results in row of #N/A's
For lastNEWITEM = 3 To lastRESULTROW
Inventory1.Range("A3:O" & lastNEWITEM).Value = .Range("CF3:CT" & lastRESULTROW).Value 'copy over filtered results
Inventory1.Range("P" & lastNEWITEM).Value = Date
Inventory1.Range("Q" & lastNEWITEM).Value = "=Row()"
Next lastNEWITEM
End With
NoNew:
End Sub