Macro to extract Columns based on multiple criteria

sush23

New Member
Joined
Aug 8, 2011
Messages
24
Hi there, I have a sheet within a workbook (with other sheets) that has multiple columns and gets updated every month with new data for the month that just closed (appended at the end). For any accountants out there, its a general ledger of transactions for the month, for just one account. Some of the columns within the sheet are generated from the ERP system some by me if I need to analyze something. I uploaded a sample file with dummy data. I was hoping I would get some help with a simple macro that extracts certain columns out of this data based on Criteria that the user would need to enter. The criteria would be in Column Y, Z and AF, which are the month, year and unique identifier, which is a concatenate of two other cells. I would need the data extracted and put in another sheet, preferably in a completely new workbook, but if two complicated the same workbook is fine and the columns I would need to extract are Col F-Q only. So the user would get prompted to enter the month, year and unique identifier and the macro would extract the data matching those criteria in Col F-Q into another sheet for the user. Would this be possible? I was playing around with the index, aggregate and row functions, but it was getting complicated as I need to add another unique identifier to capture all the criteria I want to extract, so I was hoping one of you excel rockstars could help me out. I highlighted to col to be extracted in yellow and criteria columns in green. I appreciate all your help.

Test file.xlsx
BCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAG
3GL ReportEntityAccountDescriptionSub AccountDescriptionJE TypeJE TypeBatch NumberPeriod EnteredReferenceTransaction DateDescriptionDebitCreditNetOther DataOther DataOther DataOther DataOther DataOther DataOther DataOther DataMonth YearOther DataOther DataOther DataOther DataOther DataUnique Identifier
46/30/2021A1234Due from CompanyABCCompany BGJGL78906/1/2021TAX6/4/2021meeting425.000425abcabcabcabcabcabcabcabc62021abcabcabcabcabcA - ABC
56/30/2021A1234Due from CompanyABCCompany BGJGL78916/2/2021REIMB6/5/2021travel11011abcabcabcabcabcabcabcabc62021abcabcabcabcabcA - ABC
66/30/2021A1234Due from CompanyABCCompany BGJGL78926/3/20211236/6/2021taxi45045abcabcabcabcabcabcabcabc62021abcabcabcabcabcA - ABC
76/30/2021A1234Due from CompanyABCCompany BGJGL78936/4/2021APR CC6/7/2021hotel606abcabcabcabcabcabcabcabc62021abcabcabcabcabcA - ABC
86/30/2021A1234Due from CompanyDEFConpany DGJGL78946/5/20215676/8/2021ABC73073abcabcabcabcabcabcabcabc62021abcabcabcabcabcA - DEF
96/30/2021A1234Due from CompanyDEFConpany DGJGL78956/6/2021TAX6/9/2021meeting567805678abcabcabcabcabcabcabcabc62021abcabcabcabcabcA - DEF
106/30/2021B1234Due from CompanyDEFConpany DGJGL78966/7/2021PMT6/10/2021travel24467024467abcabcabcabcabcabcabcabc62021abcabcabcabcabcB - DEF
116/30/2021B1234Due from CompanyDEFConpany DGJGL78976/8/2021PMT6/11/2021taxi12012abcabcabcabcabcabcabcabc62021abcabcabcabcabcB - DEF
126/30/2021B1234Due from CompanyGHICompany EGJGL78986/9/2021PMT6/12/2021hotel75075abcabcabcabcabcabcabcabc62021abcabcabcabcabcB - GHI
136/30/2021B1234Due from CompanyGHICompany EGJGL78996/10/2021PMT6/13/2021ABC880abcabcabcabcabcabcabcabc62021abcabcabcabcabcB - GHI
146/30/2021B1234Due from CompanyGHICompany EGJGL79006/11/2021TAX6/14/2021meeting232342-2319abcabcabcabcabcabcabcabc62021abcabcabcabcabcB - GHI
156/30/2021B1234Due from CompanyGHICompany EGJGL79016/12/2021PRO FEE6/15/2021travel835-27abcabcabcabcabcabcabcabc62021abcabcabcabcabcB - GHI
166/30/2021B1234Due from CompanyGHICompany EGJGL79026/13/2021PRO FEE6/16/2021taxi46-2abcabcabcabcabcabcabcabc62021abcabcabcabcabcB - GHI
176/30/2021B1234Due from CompanyGHICompany EGJGL79036/14/2021PRO FEE6/17/2021hotel14014abcabcabcabcabcabcabcabc62021abcabcabcabcabcB - GHI
186/30/2021B1234Due from CompanyGHICompany EGJGL79046/15/2021PRO FEE6/18/2021ABC65065abcabcabcabcabcabcabcabc62021abcabcabcabcabcB - GHI
197/31/2021A1234Due from CompanyABCCompany BGJGL79057/1/2021PRO FEE7/8/2021meeting606abcabcabcabcabcabcabcabc72021abcabcabcabcabcA - ABC
207/31/2021A1234Due from CompanyABCCompany BGJGL79067/2/2021PRO FEE7/9/2021travel83083abcabcabcabcabcabcabcabc72021abcabcabcabcabcA - ABC
217/31/2021A1234Due from CompanyABCCompany BGJGL79077/3/2021PRO FEE7/10/2021taxi53053abcabcabcabcabcabcabcabc72021abcabcabcabcabcA - ABC
227/31/2021A1234Due from CompanyABCCompany BGJGL79087/4/2021MAR CC7/11/2021hotel7136-129abcabcabcabcabcabcabcabc72021abcabcabcabcabcA - ABC
237/31/2021A1234Due from CompanyDEFConpany DGJGL79097/5/2021MAR CC7/12/2021ABC23023abcabcabcabcabcabcabcabc72021abcabcabcabcabcA - DEF
247/31/2021A1234Due from CompanyDEFConpany DGJGL79107/6/2021MAR CC7/13/2021meeting5383-30abcabcabcabcabcabcabcabc72021abcabcabcabcabcA - DEF
257/31/2021A1234Due from CompanyDEFConpany DGJGL79117/7/2021MAR CC7/14/2021travel5670567abcabcabcabcabcabcabcabc72021abcabcabcabcabcA - DEF
267/31/2021B1234Due from CompanyDEFConpany DGJGL79127/8/2021MAR CC7/15/2021taxi202abcabcabcabcabcabcabcabc72021abcabcabcabcabcB - DEF
277/31/2021B1234Due from CompanyGHICompany EGJGL79137/9/2021MAR CC7/16/2021hotel53053abcabcabcabcabcabcabcabc72021abcabcabcabcabcB - GHI
287/31/2021B1234Due from CompanyGHICompany EGJGL79147/10/2021MAR CC7/17/2021ABC76076abcabcabcabcabcabcabcabc72021abcabcabcabcabcB - GHI
297/31/2021B1234Due from CompanyGHICompany EGJGL79157/11/2021MAR CC7/18/2021meeting78989078989abcabcabcabcabcabcabcabc72021abcabcabcabcabcB - GHI
307/31/2021B1234Due from CompanyGHICompany EGJGL79167/12/20212/21/20217/19/2021travel2340234abcabcabcabcabcabcabcabc72021abcabcabcabcabcB - GHI
317/31/2021B1234Due from CompanyGHICompany EGJGL79177/13/20212/21/20217/20/2021taxi3450345abcabcabcabcabcabcabcabc72021abcabcabcabcabcB - GHI
327/31/2021B1234Due from CompanyGHICompany EGJGL79187/14/20212/21/20217/21/2021hotel543505435abcabcabcabcabcabcabcabc72021abcabcabcabcabcB - GHI
338/31/2021A1234Due from CompanyABCCompany BGJGL79198/1/20216/21/20218/10/2021ABC1246.0001246abcabcabcabcabcabcabcabc82021abcabcabcabcabcA - ABC
348/31/2021A1234Due from CompanyABCCompany BGJGL79208/1/20216/21/20218/11/2021meeting23023abcabcabcabcabcabcabcabc82021abcabcabcabcabcA - ABC
358/31/2021A1234Due from CompanyABCCompany BGJGL79218/1/20216/21/20218/12/2021travel53053abcabcabcabcabcabcabcabc82021abcabcabcabcabcA - ABC
368/31/2021A1234Due from CompanyABCCompany BGJGL79228/1/2021REIMB8/13/2021taxi46046abcabcabcabcabcabcabcabc82021abcabcabcabcabcA - ABC
378/31/2021A1234Due from CompanyDEFConpany DGJGL79238/1/20216/21/20218/14/2021hotel57057abcabcabcabcabcabcabcabc82021abcabcabcabcabcA - DEF
388/31/2021A1234Due from CompanyDEFConpany DGJGL79248/1/20216/21/20218/15/2021ABC23023abcabcabcabcabcabcabcabc82021abcabcabcabcabcA - DEF
398/31/2021A1234Due from CompanyDEFConpany DGJGL79258/1/20216/21/20218/16/2021meeting32032abcabcabcabcabcabcabcabc82021abcabcabcabcabcA - DEF
408/31/2021A1234Due from CompanyDEFConpany DGJGL79268/1/2021PRO FEE8/17/2021travel505abcabcabcabcabcabcabcabc82021abcabcabcabcabcA - DEF
418/31/2021A1234Due from CompanyGHICompany EGJGL79278/1/2021PMT8/18/2021taxi202abcabcabcabcabcabcabcabc82021abcabcabcabcabcA - GHI
428/31/2021A1234Due from CompanyGHICompany EGJGL79288/1/2021PMT8/19/2021hotel42042abcabcabcabcabcabcabcabc82021abcabcabcabcabcA - GHI
438/31/2021B1234Due from CompanyGHICompany EGJGL79298/1/2021PMT8/20/2021ABC53053abcabcabcabcabcabcabcabc82021abcabcabcabcabcB - GHI
448/31/2021B1234Due from CompanyGHICompany EGJGL79308/1/2021PMT8/21/2021meeting24024abcabcabcabcabcabcabcabc82021abcabcabcabcabcB - GHI
458/31/2021B1234Due from CompanyGHICompany EGJGL79318/1/20218/22/2021travel31031abcabcabcabcabcabcabcabc82021abcabcabcabcabcB - GHI
468/31/2021B1234Due from CompanyGHICompany EGJGL79328/1/20218/23/2021taxi32032abcabcabcabcabcabcabcabc82021abcabcabcabcabcB - GHI
478/31/2021B1234Due from CompanyKLMCompany FGJGL79338/1/20217/8/20208/24/2021hotel35035abcabcabcabcabcabcabcabc82021abcabcabcabcabcB - KLM
488/31/2021B1234Due from CompanyABCCompany FGJGL79348/1/2021REIMB8/25/2021ABC1330133abcabcabcabcabcabcabcabc82021abcabcabcabcabcB - ABC
498/31/2021B1234Due from CompanyABCCompany FGJGL79358/1/2021REVERSAL8/26/2021meeting42042abcabcabcabcabcabcabcabc82021abcabcabcabcabcB - ABC
508/31/2021B1234Due from CompanyABCCompany FGJGL79368/1/2021PMT8/27/2021travel43043abcabcabcabcabcabcabcabc82021abcabcabcabcabcB - ABC
518/31/2021B1234Due from CompanyABCCompany FGJGL79378/1/20215/1/20218/28/2021taxi3234120323412abcabcabcabcabcabcabcabc82021abcabcabcabcabcB - ABC
528/31/2021B1234Due from CompanyABCCompany FGJGL79388/1/2021789018/29/2021hotel303abcabcabcabcabcabcabcabc82021abcabcabcabcabcB - ABC
Sheet1
Cell Formulas
RangeFormula
Z4:Z52Z4=MONTH(M4)
AA4:AA52AA4=YEAR(K4)
K5:K18,M34:M52,J5:J52,M20:M32,K20:K32,M5:M18K5=K4+1
Q4:Q52Q4=O4-P4
AG4:AG52AG4=CONCATENATE(C4," - ",F4)
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Hi sush23,

Try this:

VBA Code:
Option Explicit
Sub Macro1()

    Dim strUserCriteria() As String
    Dim ws As Worksheet
    Dim lngLastRow As Long
    Dim wbActive As Workbook, wbNew As Workbook
    
    Set wbActive = ThisWorkbook
    Set ws = wbActive.Sheets("Sheet1") 'Sheet name containing GL data. Change to suit if necessary.
    
    On Error Resume Next
        If WorksheetFunction.CountA(ws.Cells) > 0 Then
            lngLastRow = ws.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
            If lngLastRow < 4 Then
                MsgBox "There doesn't appear to be any data on """ & ws.Name & """ to work with." & vbNewLine & "Please check and try again.", vbExclamation
                Exit Sub
            End If
        End If
    On Error GoTo 0
    
    ws.AutoFilterMode = False 'Remove all filters
    strUserCriteria = Split(InputBox("Enter the month, year and unique identifier (each separated via comma) you'd like to extract matching records for e.g." & vbNewLine & "6,2021,A - ABC", "Extract Records Editor"), ",")
    If UBound(strUserCriteria) < 2 Then
        Exit Sub 'User either pressed Cancel or did not make a proper entry
    End If
    
    Application.ScreenUpdating = False
    
    ws.Range("$B$3:$AG$" & lngLastRow).AutoFilter Field:=25, Criteria1:=strUserCriteria(0), Operator:=xlAnd
    ws.Range("$B$3:$AG$" & lngLastRow).AutoFilter Field:=26, Criteria1:=strUserCriteria(1), Operator:=xlAnd
    ws.Range("$B$3:$AG$" & lngLastRow).AutoFilter Field:=32, Criteria1:=strUserCriteria(2), Operator:=xlAnd
    
    If Application.WorksheetFunction.Subtotal(103, ws.Range("$B$3:$B$" & lngLastRow).Offset(1, 0)) > 0 Then
        Set wbNew = Workbooks.Add(1) 'Create a new workbook with just one sheet. Change to suit if necessaty.
        ws.Range("$B$3:$AG$" & lngLastRow).SpecialCells(xlCellTypeVisible).Copy Destination:=wbNew.Sheets(1).Range("A1")
        wbActive.Activate
        MsgBox "The matching " & Format(Application.WorksheetFunction.Subtotal(103, ws.Range("$B$3:$B$" & lngLastRow).Offset(1, 0)), "#,##0") & " record(s) have now been copied to a new workbook.", vbInformation
    Else
        MsgBox "There were no matching records found.", vbExclamation
    End If
    
    ws.AutoFilterMode = False 'Remove all filters
    
    Application.ScreenUpdating = True

End Sub

Regards,

Robert
 
Upvote 0
Just remembered you only wanted columns F to Q (inclusive) :eek:

To copy just those 12 columns change this line...

VBA Code:
ws.Range("$B$3:$AG$" & lngLastRow).SpecialCells(xlCellTypeVisible).Copy Destination:=wbNew.Sheets(1).Range("A1")

...to this:

VBA Code:
ws.Range("$F$3:$Q$" & lngLastRow).SpecialCells(xlCellTypeVisible).Copy Destination:=wbNew.Sheets(1).Range("A1")
 
Upvote 0
Thanks Trebor 76! Much appreciate your help. Unfortunately, it's giving me an error: Autofilter Method of Range class failed. The debugger goes straight to the lines I replaced with then new code in your second post.

I actually put together the below code yesterday, which works well (I apologize in advance, my code writing is rudimentary at best, but wanted to give it a shot). It basically followed the steps I would take if I were to use the Advanced Filter tool in Excel manually and tried to automate it with a button to make it easier on the user. The user enters the conditions in a separate tab, presses an ACTIVEX button and the code filters and pastes the data into another sheet. The slight annoyances with the code are

1) As soon as I extract the data, the tab the data comes from removes the filters option and I need to constantly go back to the master tab and put the filters on (ALT - A - T). Its not a problem that it unfilters the data, its annoying when it removes the function completely.
2) Would it be possible to add something to this code that pastes the extracted data in a new workbook, so the user doesn't have to copy the tab into another workbook and save it separately each time

Again, appreciate it
Rachel

VBA Code:
Private Sub CommandButton1_Click ()
Sheet3.Activate
Sheet3.Range("F3:Q3").Select
Selection.Copy
Sheet4.Select
Sheet4.Range("A1:L1").pastespecial
Sheet4.Columns("A:L").EntireColumn.Autofit
Sheet4.Range("S1").Select
Sheet3.Select
Application.Cutcopymode=False
Sheet3.Range("F10").Select
Sheet4.Select
Sheet3.Range("B3:AK5000").Advancedfilter Action:clFiltercopy, criteriarange:=Sheet1.Range("c5:e6"), copytorange:=Sheet4.Range("A1:L1"), Unique:=False
End Sub
 
Upvote 0
Thanks Trebor 76! Much appreciate your help. Unfortunately, it's giving me an error: Autofilter Method of Range class failed. The debugger goes straight to the lines I replaced with then new code in your second post.

That's odd as it worked for me :confused:

I actually put together the below code yesterday, which works well

That surprises me a little as there is no clFiltercopy argument for the AdvancedFilter syntax - it is xlFiltercopy. This is a great example of why you should always use Option Explicit. Also the text "Month" in cell Z3 of Sheet3 had a trailing space so it was "Month " which meant the criteria did not work as expected (that may have been a result of copying the data down I must say).

I have written two macros - the first (Macro2) keeps clearing the data in Sheet4 before it pastes the new filter data. The second (Macro3) pastes the results in a newly created workbook with a single sheet but in my humble opinion (unless there's a specific reason for it) I would not do this as there could be numerous workbooks created during the data being analyzed. Both macros remove the advanced filter and then puts filters back on the main data set (I think that's what you're asking for in 1 above).

Robert

VBA Code:
'https://www.mrexcel.com/board/threads/macro-to-extract-columns-based-on-multiple-criteria.1183988/
Option Explicit
Sub Macro2()

    Dim lngLastRow As Long
    Dim rngData As Range, rngCriteria As Range, rngFiltered As Range, rngOutput As Range
    
    Application.ScreenUpdating = False
    
    lngLastRow = Sheet3.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    
    Set rngData = Sheet3.Range("B3:AG" & lngLastRow)
    Set rngCriteria = Sheet1.Range("C5:E6") 'C5:E5 are the headings to be filtered, C6:E6 are the criteria
    Set rngOutput = Sheet4.Range("A1")
    
    rngData.AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=rngCriteria
    Set rngFiltered = Sheet3.Range("$F$3:$Q$" & lngLastRow).SpecialCells(xlCellTypeVisible)
    Sheet4.Cells.ClearContents 'Clear any existing filtered data in output 'Sheet4'
    rngFiltered.Copy Destination:=rngOutput
    
    Sheet3.AutoFilterMode = False 'Remove all filters
    rngData.AutoFilter 'Put a filter on first row
    
    Application.ScreenUpdating = True

End Sub
Sub Macro3()

    Dim lngLastRow As Long
    Dim rngData As Range, rngCriteria As Range, rngFiltered As Range
    Dim wbOutput As Workbook
    
    Application.ScreenUpdating = False
    
    lngLastRow = Sheet3.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    
    Set rngData = Sheet3.Range("B3:AG" & lngLastRow)
    Set rngCriteria = Sheet1.Range("C5:E6") 'C5:E5 are the headings to be filtered, C6:E6 are the criteria
        
    rngData.AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=rngCriteria
    Set rngFiltered = Sheet3.Range("$F$3:$Q$" & lngLastRow).SpecialCells(xlCellTypeVisible)
    
    Set wbOutput = Workbooks.Add(1) 'Create a new workbook with just one sheet. Change to suit if necessaty.
    rngFiltered.Copy Destination:=wbOutput.Sheets(1).Range("A1")
    
    Sheet3.AutoFilterMode = False 'Remove all filters
    rngData.AutoFilter 'Put a filter on first row
    
    Application.ScreenUpdating = True

End Sub
 
Upvote 0
That's odd as it worked for me :confused:



That surprises me a little as there is no clFiltercopy argument for the AdvancedFilter syntax - it is xlFiltercopy. This is a great example of why you should always use Option Explicit. Also the text "Month" in cell Z3 of Sheet3 had a trailing space so it was "Month " which meant the criteria did not work as expected (that may have been a result of copying the data down I must say).

I have written two macros - the first (Macro2) keeps clearing the data in Sheet4 before it pastes the new filter data. The second (Macro3) pastes the results in a newly created workbook with a single sheet but in my humble opinion (unless there's a specific reason for it) I would not do this as there could be numerous workbooks created during the data being analyzed. Both macros remove the advanced filter and then puts filters back on the main data set (I think that's what you're asking for in 1 above).

Robert

VBA Code:
'https://www.mrexcel.com/board/threads/macro-to-extract-columns-based-on-multiple-criteria.1183988/
Option Explicit
Sub Macro2()

    Dim lngLastRow As Long
    Dim rngData As Range, rngCriteria As Range, rngFiltered As Range, rngOutput As Range
   
    Application.ScreenUpdating = False
   
    lngLastRow = Sheet3.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
   
    Set rngData = Sheet3.Range("B3:AG" & lngLastRow)
    Set rngCriteria = Sheet1.Range("C5:E6") 'C5:E5 are the headings to be filtered, C6:E6 are the criteria
    Set rngOutput = Sheet4.Range("A1")
   
    rngData.AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=rngCriteria
    Set rngFiltered = Sheet3.Range("$F$3:$Q$" & lngLastRow).SpecialCells(xlCellTypeVisible)
    Sheet4.Cells.ClearContents 'Clear any existing filtered data in output 'Sheet4'
    rngFiltered.Copy Destination:=rngOutput
   
    Sheet3.AutoFilterMode = False 'Remove all filters
    rngData.AutoFilter 'Put a filter on first row
   
    Application.ScreenUpdating = True

End Sub
Sub Macro3()

    Dim lngLastRow As Long
    Dim rngData As Range, rngCriteria As Range, rngFiltered As Range
    Dim wbOutput As Workbook
   
    Application.ScreenUpdating = False
   
    lngLastRow = Sheet3.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
   
    Set rngData = Sheet3.Range("B3:AG" & lngLastRow)
    Set rngCriteria = Sheet1.Range("C5:E6") 'C5:E5 are the headings to be filtered, C6:E6 are the criteria
       
    rngData.AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=rngCriteria
    Set rngFiltered = Sheet3.Range("$F$3:$Q$" & lngLastRow).SpecialCells(xlCellTypeVisible)
   
    Set wbOutput = Workbooks.Add(1) 'Create a new workbook with just one sheet. Change to suit if necessaty.
    rngFiltered.Copy Destination:=wbOutput.Sheets(1).Range("A1")
   
    Sheet3.AutoFilterMode = False 'Remove all filters
    rngData.AutoFilter 'Put a filter on first row
   
    Application.ScreenUpdating = True

End Sub
Thanks Robert, You were right, I miss-spelled the code, it is xlfiltercopy. I am working on two computers and was typing my short code rather than pasting it. I understand your point on creating a new workbook. Let me try one of these codes and let you know. Again, THANK YOU
 
Upvote 0
Hi sush23,

You're welcome (y)

Since my last post I came across this video that shows a really nifty tip on how you can filter data for particular columns when using the advanced filter by the headings you enter into Row 1 of the output sheet (A1:L1 in Sheet4 from your case). As such I have written the following macro (just make sure your headings are in A1:L1 of Sheet4 before using):

VBA Code:
'Original thread:
'https://www.mrexcel.com/board/threads/macro-to-extract-columns-based-on-multiple-criteria.1183988
'Filtering by column headers:
'https://www.youtube.com/watch?v=evrnIuDRtsQ
Option Explicit
Sub Macro4()

    Dim lngLastRow As Long
    Dim rngData As Range, rngCriteria As Range, rngFiltered As Range, rngOutput As Range
    
    Application.ScreenUpdating = False
    
    'Clear any existing extract
    lngLastRow = Sheet4.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    If lngLastRow >= 2 Then
        Sheet4.Rows("2:" & lngLastRow).Delete
    End If
    
    'Filter and import new extract
    lngLastRow = Sheet3.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Set rngData = Sheet3.Range("B3:AG" & lngLastRow)
    Set rngCriteria = Sheet1.Range("C5:E6") 'C5:E5 are the headings to be filtered, C6:E6 are the criteria
    Set rngOutput = Sheet4.Range("A1:L1") 'Extract columns by header in A1:L1. Source:https://www.youtube.com/watch?v=evrnIuDRtsQ
    
    rngData.AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=rngCriteria, CopyToRange:=rngOutput, Unique:=False
    Sheet4.Columns("A:L").EntireColumn.AutoFit
    
    Sheet3.AutoFilterMode = False 'Remove all filters
    rngData.AutoFilter 'Put a filter on first row
    
    Application.ScreenUpdating = True

End Sub

Regards,

Robert
 
Upvote 0

Forum statistics

Threads
1,214,904
Messages
6,122,169
Members
449,070
Latest member
webster33

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