Macro using advanced Filter

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,566
Office Version
  1. 2021
Platform
  1. Windows
I can manually use advanced filter to extract my data m, but when trying to write code to extract the data, only the headings are extracted

Option Explicit

Code:
 Sub Extract_Data()
Dim LR As Long
With Sheets("Extracted Data")
LR = Cells(.Rows.Count, "A").End(xlUp).Row
 .Range("A1:G" & LR).ClearContents

  Sheets("Imported Data").Columns("A:G").AdvancedFilter Action:=xlFilterCopy, _
CriteriaRange:=Range("'Extracted Data'!Criteria"), CopyToRange:=.Range("A1") _
, Unique:=False
End With
End Sub

Kindly check my code and amend
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
The code works.
You can put a generic sample of your data: sheet "Imported Data" and of the data in the range "Criteria". Use XL2BB tool.

By the way, the code can be simplified in this way:
VBA Code:
Sub Extract_Data()
  With Sheets("Extracted Data")
    .Range("A1:G" & .Cells(.Rows.Count, "A").End(3).Row).ClearContents
    Sheets("Imported Data").Columns("A:G").AdvancedFilter 2, .Range("Criteria"), .Range("A1")
  End With
End Sub
 
Upvote 0
Come on Howard you know the rules, especially regarding cross posting.
 
Upvote 0
I do know the rules, but guinely forgot to show the link, which I have now posted below

 
Upvote 0
Normally we don't download the files, you could put an example, use XL2BB tool, I want to see how the data is in the sheet and how are the criteria.
 
Upvote 0
Hi Dante


See sheet where I want data extracted "Extracted Data"

Macro to extract Data Using Advanced Filter with Multiple Date Ranges.xlsm
ABCDEFGHIJKLMNOPQRSTU
1Stock No.InvoiceTypeCodeDescriptionDateValueCodeDateDateValue
2ABL-1>=08/03/2020<=11/03/2020<=84877
3
Extracted Data




Source Sheet

Macro to extract Data Using Advanced Filter with Multiple Date Ranges.xlsm
ABCDEFG
1Stock No.InvoiceTypeCodeDescriptionDateValue
2AFAJXXMJWJKE0296216851ReplenishmentABL-1Shirts Blue S06/03/202037733.00
3AFAJXXMJWJKD8859517026ReplenishmentKLP-10Shirts Blue S06/03/202038928.00
4MAJAXXMRKAKL7431117037ReplenishmentABL-2Shirts Blue S07/03/202015623.00
5MAJGXXMTKGKS2913717053ReplenishmentABL-2Shirts Blue S07/03/202011842.00
6AFAPXXMJ2PKE0144717212ReplenishmentKLP-10Shirts W M07/03/202021650.00
7AFANXXMJ2NKY2232117223ReplenishmentABL-1Shirts W M07/03/202021781.00
8WF01XXERK1KS6007017307ReplenishmentKLP-10Shirts W M07/03/202018579.00
9WF01XXERK1KS6054017310ReplenishmentKLP-10Shirts W M07/03/202019493.00
10AFAJXXMJWJKS3131617311ReplenishmentABL-1Shirts W M08/03/202035976.00
11AFAPXXMJ2PKY1744117313ReplenishmentABL-1Shirts W M09/03/202025159.00
12MAJGXXMTKGKU4298917314ReplenishmentABL-1Shirt B M10/03/202011900.00
13MAJGXXMTKGKU4322917315ReplenishmentABL-1Shirt B M11/03/202011842.00
14MAJGXXMTKGKS2719417316ReplenishmentKLP-10Shirt B M12/03/202013591.00
15MAJGXXMTKGKS1951117317ReplenishmentKLP-10Shirt B M13/03/202013378.00
16MAJGXXMTKGKL0354317318ReplenishmentKLP-11Shirt B M14/03/202013321.00
17MAJAXXMRKAKJ3446117319ReplenishmentABL-1Shirt B M15/03/202016711.00
18WF01XXERK1KS5971317323ReplenishmentKLP-10Shirt B M16/03/202018579.00
Imported Data
 
Upvote 0
In the case of dates, in the advanced filter, you must use the number that excel uses as the date.
One way to get the number of a date is with the DATEVALUE formula. In addition, the criteria for date must go in a formula, as shown below:


Dante Amor
RSTU
1CodeDateDateValue
2ABL-1>=43898<=43901<=84877
Extracted Data
Cell Formulas
RangeFormula
S2S2=">="&DATEVALUE("08/03/2020")
T2T2="<="&DATEVALUE("11/03/2020")
 
Upvote 0

Forum statistics

Threads
1,215,475
Messages
6,125,028
Members
449,205
Latest member
Eggy66

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