VBA macro that filters two criteria in one column and grabs top 8 in another column

bored622

New Member
Joined
Mar 2, 2022
Messages
43
Office Version
  1. 2016
Platform
  1. Windows
Hello everyone,

I'm trying to make a macro button that when clicked on it filters two criteria from column M and filters the top eight from column C.

This is what I got so far:

VBA Code:
Sub Filterfp()

With Worksheets("WaveFile").Range("A6")
.AutoFilter Field:=13, Criteria1:="FP", Operator:=xlOr, Criteria2:="BK"
.AutoFilter Field:=3, Criteria1:="8", Operator:=xlTop10Items

End With

End Sub
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
When posting vba code in the forum, please use the available code tags. It makes your code much easier to read/debug. My signature block below has more details. I have added the tags for you this time. 😊

and filters the top eight from column C.
I presume that means top 8 of the values still showing after the first filter?

Assuming that there will be at least 8 still showing after the first filter, would this suffice to produce those results?

VBA Code:
Sub Filterfp_v2()
  Dim dVal As Double

  With Worksheets("WaveFile").Range("A6")
    .AutoFilter Field:=13, Criteria1:="FP", Operator:=xlOr, Criteria2:="BK"
    dVal = .Worksheet.Evaluate(Replace("AGGREGATE(14,6," & .CurrentRegion.Columns(3).Address & "/((#=""FP"")+(#=""BK"")),8)", "#", .CurrentRegion.Columns(13).Address))
    .AutoFilter Field:=3, Criteria1:=">=" & dVal
  End With
End Sub
 
Upvote 0
When posting vba code in the forum, please use the available code tags. It makes your code much easier to read/debug. My signature block below has more details. I have added the tags for you this time. 😊


I presume that means top 8 of the values still showing after the first filter?

Assuming that there will be at least 8 still showing after the first filter, would this suffice to produce those results?

VBA Code:
Sub Filterfp_v2()
  Dim dVal As Double

  With Worksheets("WaveFile").Range("A6")
    .AutoFilter Field:=13, Criteria1:="FP", Operator:=xlOr, Criteria2:="BK"
    dVal = .Worksheet.Evaluate(Replace("AGGREGATE(14,6," & .CurrentRegion.Columns(3).Address & "/((#=""FP"")+(#=""BK"")),8)", "#", .CurrentRegion.Columns(13).Address))
    .AutoFilter Field:=3, Criteria1:=">=" & dVal
  End With
End Sub
Thank you for making my code look better for the post.

I tried running it, but I get a run-time error of '13'
type mismatch
 
Upvote 0
but I get a run-time error of '13'
type mismatch
Whenever you get a vba error, as well as giving the full error message, please make it clear which line of the code the error occurred on.

Could we have a small set of dummy sample data (with XL2BB) that causes that error?

This is what I tested with. The green cells I manually coloured as I thought they were the rows that you would want displayed at the end of the code.

bored622.xlsm
ABCDEFGHIJKLM
6Hdr1Hdr2Hdr3Hdr4Hdr5Hdr6Hdr7Hdr8Hdr9Hdr10Hdr11Hdr12Hdr13
790AA
845FP
919FP
1046FP
1178FP
1246FP
1362FP
1475AA
1588BK
1622BK
1717BK
1874BK
1943BK
2036BK
2145BK
2258BK
WaveFile


And here is that same worksheet after the code was run.

bored622.xlsm
ABCDEFGHIJKLM
6Hdr1Hdr2Hdr3Hdr4Hdr5Hdr6Hdr7Hdr8Hdr9Hdr10Hdr11Hdr12Hdr13
845FP
1046FP
1178FP
1246FP
1362FP
1588BK
1874BK
2145BK
2258BK
WaveFile
 
Upvote 0
Here is a small amount of data

wave create 2.0 - Copy.xlsm
ABCDEFGHIJKLMNOPQRSTUV
6DistroOrigin CodeDPCIRequest TypeDistro StatusSSZUnit SortableTray CodeTotal Number of PalletsTotal Number of Full VCPsTotal Number of Loose SSPSSPs in VCPPull ModeTotal Number of StoreTotal Number of SSPs in the DistroVCP LengthVCP WidthVCP HeightAllocation Request DateAllocation Request TimeWorkStationAisle
7000000000071330489W097-11-0681BASN015Y100112FP1115.395.108/23/202202:11:25 AMSF03162
8000000000071330494W031-11-0815BASN015Y100112FP1113.1116.808/23/202202:11:29 AMCR03111
9000000000071330497W031-11-0851BASN015Y100312FP2315.514.56.508/23/202202:11:29 AMPT01358
10000000000071330502W031-11-1021BASN015Y100112FP1113.69.44.308/23/202202:11:35 AMSF20699
11000000000071330444W031-11-0196BASN015Y100512FP459.27.54.308/23/202202:11:33 AMSF03140
12000000000071330449W031-11-0257BASN015Y100112FP11975.508/23/202202:11:44 AMSF04460
13000000000071330453W031-11-0274BASN015Y100412BK24974.708/23/202202:11:29 AMSF03106
14000000000071330458W031-11-0297BASN015Y100112BK11974.708/23/202202:11:25 AMSF03155
15000000000071330476W031-11-0480BASN015Y100412BK348.86.96.808/23/202202:11:40 AMSF21615
16000000000071330486W031-11-0676BASN015Y100312BK3315.395.108/23/202202:11:29 AMSF03136
17000000000071330487W031-11-0678BASN015Y100212BK2216.510.25.108/23/202202:11:29 AMSF03124
18000000000071326953W031-11-0163BASN015Y101012BK812973.908/23/202202:11:05 AMSF03122
19000000000071326955W031-11-0165BASN015Y100812BK88973.908/23/202202:11:05 AMSF03122
20000000000071326957W031-11-0167BASN015Y100412CA34974.708/23/202202:11:05 AMSF21611
21000000000071326975W031-11-0213BASN015Y101012CA1112973.908/23/202202:11:05 AMSF21611
22000000000071326983W031-11-0230BASN015Y100912CA69973.908/23/202202:11:05 AMSF03140
23000000000071326984W031-11-0231BASN015Y100512CA35973.908/23/202202:11:05 AMCR03441
24000000000071326990W031-11-0236BASN015Y100112CA11973.908/23/202202:11:05 AMSF20702
25000000000071326991W031-11-0237BASN015Y100612CA56973.908/23/202202:11:05 AMSF02238
WaveFile
 
Upvote 0
Thanks. Your original question was for top 8 in column C. Here, your column C is not numerical, so how could we ascertain "top 8"?
I put the wrong file up. Is there a way to remove my post? I meant to place blank cells and just leave data in columns C and M
 
Upvote 0

Forum statistics

Threads
1,215,202
Messages
6,123,625
Members
449,109
Latest member
Sebas8956

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