Filtering without Range

blu817

New Member
Joined
Aug 18, 2017
Messages
20
Hello

I am trying to create a macro that can count how many times a stock shows up during a particular day. It needs to do a few things:

1. Filter for prior day or most recent date in column N
2. Filter column H for Volatility; its what the stock has been categorized as
3. Remove duplicates from column D, which is the name of the stocks, to show only single values
4. Show a count of how many symbols

I created the following macro but it only works just on the spreadsheet I created. I feel like it has to do with the range some of my spreadsheets have more lines than others. I've tried expanding it but still doesn't work on other spreadsheets. Any suggestions would be great



Rows("1:1").Select
Selection.AutoFilter

ActiveSheet.Range("$A$1:$N$77081").AutoFilter Field:=14, Operator:= _
xlFilterValues, Criteria2:=Array(2, Date - 1)
ActiveSheet.Range("$A$1:$N$77081").AutoFilter Field:=8, Criteria1:= _
"Volatility"

Columns("D:D").Select
Selection.Copy
Sheets.Add After:=ActiveSheet
ActiveSheet.Paste
Application.CutCopyMode = False
ActiveSheet.Range("$A$1:$A$976574").RemoveDuplicates Columns:=1, Header:= _
xlYes
Range("B2").Select
ActiveCell.FormulaR1C1 = "=COUNTA(RC[-1]:R[998]C[-1])"
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Hello blu817,

What is not happening on the other worksheets? Please be specific.
 
Last edited:
Upvote 0
Hello blu817,

Try this version of your macro and let me know the results.

Code:
Sub Test()


    Dim NewWks  As Worksheet
    Dim Rng     As Range
    Dim RngEnd  As Range
    Dim Wks     As Worksheet
    
        Set Wks = ActiveSheet
            Wks.AutoFilterMode = False
        
        Set Rng = Wks.Range("A1:N1")
        
        Set RngEnd = Wks.Columns("A:N").Find("*", , xlFormulas, xlWhole, xlByColumns, xlPrevious, False, False, False)
            If RngEnd Is Nothing Then Exit Sub
        
        Set Rng = Rng.Resize(RowSize:=RngEnd.Row - Rng.Row + 1)
            Rng.AutoFilter Field:=14, Operator:=xlFilterValues, Criteria2:=Array(2, Date - 1)
            Rng.AutoFilter Field:=8, Criteria1:="Volatility"
           
        On Error Resume Next
            Set Rng = Rng.SpecialCells(xlCellTypeVisible)
            If Err = 0 Then
                Set NewWks = Worksheets.Add(After:=Wks)
                Rng.RemoveDuplicates Columns:=1, Header:=xlYes
                Rng.Copy NewWks.Range("A1")
                Application.CutCopyMode = False
                NewWks.Cells(1, "B").Formula = "=COUNTA(RC[-1]:R[998]C[-1])"
            End If
        On Error GoTo 0


End Sub
 
Last edited:
Upvote 0
Hello blu817,

If you can post a copy of the workbook that would be a big help.
 
Upvote 0
Sorry I wasnt able to attach the excel due to firewall but here are some sample lines. Let me know if this helps

FININSTRTYPEFININSTRSYMBOLCUSIPFININSTRNAMEEXPDATESTRIKEPUT_CALLCODEOVERRIDETYPEPREVIOUS_PRICEEDITED_PRCECREATEDBYCREATIONDATELAST_EDITED_BYLAST_EDITED_DATE
DEBT135087UL6Canadian Government Bond###################Final Pricenull0aa10/26/18 17:41 abb10/26/18 17:41 a
DEBT135087UL6Canadian Government Bond###################Final Pricenull0aa10/26/18 17:57 abb10/26/18 17:57 a
DEBT135087VW1Canadian Government Bond###################Final Pricenull0aa10/26/18 17:41 abb10/26/18 17:41 a
DEBT135087VW1Canadian Government Bond###################Final Pricenull0aa10/26/18 17:57 abb10/26/18 17:57 a
DEBT135087WL4Canadian Government Bond###################Final Pricenull0aa10/26/18 17:41 abb10/26/18 17:41 a
DEBT135087WL4Canadian Government Bond###################Final Pricenull0aa10/26/18 17:57 abb10/26/18 17:57 a
DEBT135087WV2Canadian Government Bond###################Final Pricenull0aa10/26/18 17:41 abb10/26/18 17:41 a
DEBT135087WV2Canadian Government Bond###################Final Pricenull0aa10/26/18 17:57 abb10/26/18 17:57 a
DEBT135087XQ2Canadian Government Bond###################Final Pricenull0aa10/26/18 17:41 abb10/26/18 17:41 a
DEBT135087XQ2Canadian Government Bond###################Final Pricenull0aa10/26/18 17:57 abb10/26/18 17:57 a
DEBT135087XW9Canadian Government Bond###################Final Pricenull0aa10/26/18 17:41 abb10/26/18 17:41 a
DEBT135087XW9Canadian Government Bond###################Final Pricenull0aa10/26/18 17:57 abb10/26/18 17:57 a
DEBT135087YQ1Canadian Government Bond###################Final Pricenull0aa10/26/18 17:41 abb10/26/18 17:41 a
DEBT135087YQ1Canadian Government Bond###################Final Pricenull0aa10/26/18 17:57 abb10/26/18 17:57 a
DEBT912810QA9United States Treasury Note/Bond###################Final Pricenull0aa10/26/18 17:40 abb10/26/18 17:40 a
DEBT912810QA9United States Treasury Note/Bond###################Final Pricenull0aa10/26/18 17:57 abb10/26/18 17:57 a
EQUIAALPH99999AAL6Adjusted AMR Corporation Placeholder###################Final Pricenull0aa10/26/18 17:52 abb10/26/18 17:52 a
EQUIAALPH99999AAL6Adjusted AMR Corporation Placeholder###################Final Pricenull0aa10/26/18 17:45 abb10/26/18 17:45 a
OPTNAAUnullAlmaden Minerals Ltd11/16/18 0:00 a 0000002.500CVolatility6.6536203622aa10/22/18 13:50 abb10/22/18 13:50 a
OPTNAAUnullAlmaden Minerals Ltd11/16/18 0:00 a 0000002.500CVolatility6.6536203622aa10/22/18 13:50 abb10/22/18 13:50 a
OPTNAAUnullAlmaden Minerals Ltd11/16/18 0:00 a 0000002.500PVolatility7.3703141072aa10/22/18 13:50 abb10/22/18 13:50 a
OPTNAAUnullAlmaden Minerals Ltd11/16/18 0:00 a 0000002.500PVolatility7.3703141072aa10/22/18 13:50 abb10/22/18 13:50 a
OPTNAAUnullAlmaden Minerals Ltd11/16/18 0:00 a 0000005.000CVolatility7.8041094322aa10/22/18 13:50 abb10/22/18 13:50 a
OPTNAAUnullAlmaden Minerals Ltd11/16/18 0:00 a 0000005.000CVolatility7.8041094322aa10/22/18 13:50 abb10/22/18 13:50 a
OPTNAAUnullAlmaden Minerals Ltd11/16/18 0:00 a 0000005.000PVolatility8.5410320342aa10/22/18 13:50 abb10/22/18 13:50 a
OPTNAAUnullAlmaden Minerals Ltd11/16/18 0:00 a 0000005.000PVolatility8.5410320342aa10/22/18 13:50 abb10/22/18 13:50 a
OPTNAAUnullAlmaden Minerals Ltd12/21/18 0:00 a 0000002.500CVolatility3.3889517672aa10/22/18 13:50 abb10/22/18 13:50 a
OPTNAAUnullAlmaden Minerals Ltd12/21/18 0:00 a 0000002.500CVolatility3.3889517672aa10/22/18 13:50 abb10/22/18 13:50 a
OPTNAAUnullAlmaden Minerals Ltd12/21/18 0:00 a 0000002.500PVolatility4.3532591832aa10/22/18 13:50 abb10/22/18 13:50 a
OPTNAAUnullAlmaden Minerals Ltd12/21/18 0:00 a 0000002.500PVolatility4.3532591832aa10/22/18 13:50 abb10/22/18 13:50 a
OPTNAAUnullAlmaden Minerals Ltd12/21/18 0:00 a 0000005.000CVolatility4.1140421772aa10/22/18 13:50 abb10/22/18 13:50 a
OPTNAAUnullAlmaden Minerals Ltd12/21/18 0:00 a 0000005.000CVolatility4.1140421772aa10/22/18 13:50 abb10/22/18 13:50 a
OPTNAAUnullAlmaden Minerals Ltd12/21/18 0:00 a 0000005.000PVolatility5.0810963932aa10/22/18 13:50 abb10/22/18 13:50 a
OPTNAAUnullAlmaden Minerals Ltd12/21/18 0:00 a 0000005.000PVolatility5.0810963932aa10/22/18 13:50 abb10/22/18 13:50 a
OPTNAAUnullAlmaden Minerals Ltd3/15/19 0:00 a 0000002.500CVolatility2.0056873032aa10/22/18 13:50 abb10/22/18 13:50 a
OPTNAAUnullAlmaden Minerals Ltd3/15/19 0:00 a 0000002.500CVolatility2.0056873032aa10/22/18 13:50 abb10/22/18 13:50 a
OPTNAAUnullAlmaden Minerals Ltd3/15/19 0:00 a 0000002.500PVolatility3.2895483932aa10/22/18 13:50 abb10/22/18 13:50 a
OPTNAAUnullAlmaden Minerals Ltd3/15/19 0:00 a 0000002.500PVolatility3.2895483932aa10/22/18 13:50 abb10/22/18 13:50 a
OPTNAAUnullAlmaden Minerals Ltd3/15/19 0:00 a 0000005.000CVolatility2.0965319592aa10/22/18 13:50 abb10/22/18 13:50 a
OPTNAAUnullAlmaden Minerals Ltd3/15/19 0:00 a 0000005.000CVolatility2.0965319592aa10/22/18 13:50 abb10/22/18 13:50 a
OPTNAAUnullAlmaden Minerals Ltd3/15/19 0:00 a 0000005.000PVolatility3.4640645392aa10/22/18 13:50 abb10/22/18 13:50 a
OPTNAAUnullAlmaden Minerals Ltd3/15/19 0:00 a 0000005.000PVolatility3.4640645392aa10/22/18 13:50 abb10/22/18 13:50 a
OPTNAAUnullAlmaden Minerals Ltd6/21/19 0:00 a 0000002.500CVolatility02aa10/22/18 13:50 abb10/22/18 13:50 a
OPTNAAUnullAlmaden Minerals Ltd6/21/19 0:00 a 0000002.500CVolatility0.00012aa10/22/18 13:50 abb10/22/18 13:50 a
OPTNAAUnullAlmaden Minerals Ltd6/21/19 0:00 a 0000002.500PVolatility3.1900142192aa10/22/18 13:50 abb10/22/18 13:50 a
OPTNAAUnullAlmaden Minerals Ltd6/21/19 0:00 a 0000002.500PVolatility3.1900142192aa10/22/18 13:50 abb10/22/18 13:50 a
OPTNAAUnullAlmaden Minerals Ltd6/21/19 0:00 a 0000005.000CVolatility02aa10/22/18 13:50 abb10/22/18 13:50 a
OPTNAAUnullAlmaden Minerals Ltd6/21/19 0:00 a 0000005.000CVolatility0.00012aa10/22/18 13:50 abb10/22/18 13:50 a
OPTNAAUnullAlmaden Minerals Ltd6/21/19 0:00 a 0000005.000PVolatility3.6030956842aa10/22/18 13:50 abb10/22/18 13:50 a
OPTNAAUnullAlmaden Minerals Ltd6/21/19 0:00 a 0000005.000PVolatility3.6030956842aa10/22/18 13:50 abb10/22/18 13:50 a
OPTNABDCnullAlcentra Capital Corporation10/19/18 0:00 a 0000002.500CVolatility0.50.45aa9/24/18 16:04 abb9/24/18 16:05 a
OPTNABDCnullAlcentra Capital Corporation10/19/18 0:00 a 0000002.500CVolatility0.50.45aa9/24/18 16:04 abb9/24/18 16:05 a
OPTNABDCnullAlcentra Capital Corporation10/19/18 0:00 a 0000002.500PVolatility0.50.45aa9/24/18 16:04 abb9/24/18 16:05 a
OPTNABDCnullAlcentra Capital Corporation10/19/18 0:00 a 0000002.500PVolatility0.50.45aa9/24/18 16:04 abb9/24/18 16:05 a
OPTNABDCnullAlcentra Capital Corporation10/19/18 0:00 a 0000005.000CVolatility0.50.45aa9/24/18 16:04 abb9/24/18 16:05 a
OPTNABDCnullAlcentra Capital Corporation10/19/18 0:00 a 0000005.000CVolatility0.50.45aa9/24/18 16:04 abb9/24/18 16:05 a
OPTNABDCnullAlcentra Capital Corporation10/19/18 0:00 a 0000005.000PVolatility0.50.45aa9/24/18 16:04 abb9/24/18 16:05 a
OPTNABDCnullAlcentra Capital Corporation10/19/18 0:00 a 0000005.000PVolatility0.50.45aa9/24/18 16:04 abb9/24/18 16:05 a
OPTNABDCnullAlcentra Capital Corporation10/19/18 0:00 a 0000007.500CVolatility0.50.45aa9/24/18 16:04 abb9/24/18 16:05 a
OPTNABDCnullAlcentra Capital Corporation10/19/18 0:00 a 0000007.500CVolatility0.50.45aa9/24/18 16:04 abb9/24/18 16:05 a
OPTNABDCnullAlcentra Capital Corporation10/19/18 0:00 a 0000007.500PVolatility0.50.45aa9/24/18 16:04 abb9/24/18 16:05 a
OPTNABDCnullAlcentra Capital Corporation10/19/18 0:00 a 0000007.500PVolatility0.50.45aa9/24/18 16:04 abb9/24/18 16:05 a
OPTNABDCnullAlcentra Capital Corporation10/19/18 0:00 a 0000010.000CVolatility0.50.45aa9/24/18 16:04 abb9/24/18 16:05 a
OPTNABDCnullAlcentra Capital Corporation10/19/18 0:00 a 0000010.000CVolatility0.50.45aa9/24/18 16:04 abb9/24/18 16:05 a
OPTNABDCnullAlcentra Capital Corporation10/19/18 0:00 a 0000010.000PVolatility0.50.45aa9/24/18 16:04 abb9/24/18 16:05 a
OPTNABDCnullAlcentra Capital Corporation10/19/18 0:00 a 0000010.000PVolatility0.50.45aa9/24/18 16:04 abb9/24/18 16:05 a
OPTNABDCnullAlcentra Capital Corporation10/19/18 0:00 a 0000012.500CVolatility0.50.45aa9/24/18 16:04 abb9/24/18 16:05 a
OPTNABDCnullAlcentra Capital Corporation10/19/18 0:00 a 0000012.500CVolatility0.50.45aa9/24/18 16:04 abb9/24/18 16:05 a
OPTNABDCnullAlcentra Capital Corporation10/19/18 0:00 a 0000012.500PVolatility0.50.45aa9/24/18 16:04 abb9/24/18 16:05 a
OPTNABDCnullAlcentra Capital Corporation10/19/18 0:00 a 0000012.500PVolatility0.50.45aa9/24/18 16:04 abb9/24/18 16:05 a
OPTNABDCnullAlcentra Capital Corporation11/16/18 0:00 a 0000002.500CVolatility0.50.45aa9/24/18 16:04 abb9/24/18 16:05 a
OPTNABDCnullAlcentra Capital Corporation11/16/18 0:00 a 0000002.500CVolatility0.50.45aa9/24/18 16:04 abb9/24/18 16:05 a
OPTNABDCnullAlcentra Capital Corporation11/16/18 0:00 a 0000002.500PVolatility0.50.45aa9/24/18 16:04 abb9/24/18 16:05 a
OPTNABDCnullAlcentra Capital Corporation11/16/18 0:00 a 0000002.500PVolatility0.50.45aa9/24/18 16:04 abb9/24/18 16:05 a
OPTNABDCnullAlcentra Capital Corporation11/16/18 0:00 a 0000005.000CVolatility0.50.45aa9/24/18 16:04 abb9/24/18 16:05 a
OPTNABDCnullAlcentra Capital Corporation11/16/18 0:00 a 0000005.000CVolatility0.50.45aa9/24/18 16:04 abb9/24/18 16:05 a
OPTNABDCnullAlcentra Capital Corporation11/16/18 0:00 a 0000005.000PVolatility0.50.45aa9/24/18 16:04 abb9/24/18 16:05 a
OPTNABDCnullAlcentra Capital Corporation11/16/18 0:00 a 0000005.000PVolatility0.50.45aa9/24/18 16:04 abb9/24/18 16:05 a
OPTNABDCnullAlcentra Capital Corporation11/16/18 0:00 a 0000007.500CVolatility0.50.45aa9/24/18 16:04 abb9/24/18 16:05 a
OPTNABDCnullAlcentra Capital Corporation11/16/18 0:00 a 0000007.500CVolatility0.50.45aa9/24/18 16:04 abb9/24/18 16:05 a
OPTNABDCnullAlcentra Capital Corporation11/16/18 0:00 a 0000007.500PVolatility0.50.45aa9/24/18 16:04 abb9/24/18 16:05 a
OPTNABDCnullAlcentra Capital Corporation11/16/18 0:00 a 0000007.500PVolatility0.50.45aa9/24/18 16:04 abb9/24/18 16:05 a
OPTNABDCnullAlcentra Capital Corporation11/16/18 0:00 a 0000010.000CVolatility0.50.45aa9/24/18 16:04 abb9/24/18 16:05 a
OPTNABDCnullAlcentra Capital Corporation11/16/18 0:00 a 0000010.000CVolatility0.50.45aa9/24/18 16:04 abb9/24/18 16:05 a
OPTNABDCnullAlcentra Capital Corporation11/16/18 0:00 a 0000010.000PVolatility0.50.45aa9/24/18 16:04 abb9/24/18 16:05 a
OPTNABDCnullAlcentra Capital Corporation11/16/18 0:00 a 0000010.000PVolatility0.50.45aa9/24/18 16:04 abb9/24/18 16:05 a

<tbody>
</tbody>
 
Upvote 0
Hello blu817,

Thanks for the sample data. There are couple of issues I found with the dates. When filtering dates there is also a time associated with the date. If you want yesterday's date, you cannot simply subtract 1. If the date is on Monday then the previous day would be Sunday. Unless you are working on Sunday, this probably not the approach you want. Both of these issue can be fixed in the macro. Is the work week Monday through Friday or something else?




 
Upvote 0
You are absolutely right and my oversight. And in the world of macros the extra HH:MM makes a huge difference. The data will strictly be Monday - Friday but I plan on using it to dig through months of data on different spreadsheets from various days.
 
Upvote 0

Forum statistics

Threads
1,216,126
Messages
6,129,004
Members
449,480
Latest member
yesitisasport

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