Macro to filter by 3 parameters / Excel 2002 / WinXP

Freewheel

New Member
Joined
Dec 7, 2005
Messages
20
Hi,

I have a filtering problem that I am unable to solve.

I have a number of source data worksheets, one for each country. Each country datasheet contains invoice data. I then have a statistics worksheet on which I want to pull out various data elements, as filtered by:
a) a drop-down list to select the country
b) entering an "activity period" which limits the invoices to those within two dates.

Here is some sample source data, for the UK worksheet:
Invoices sample.xls
ABCDEF
1InvoiceActivity periodPayment
2ReferenceFromToAmount invoicedAmount paid
3UK-00101/06/200531/08/200582,231.20 82,231.20
4UK-00201/06/200531/08/2005204,494.03 204,494.03
5UK-00301/06/200531/08/200555,296.00 55,296.00
6UK-00404/07/200531/08/20050.00 0.00
7UK-00501/06/200531/08/2005148,539.58 148,539.58
8UK-00601/06/200531/08/2005401,314.46 401,314.46
9UK-00701/06/200531/08/2005179,577.94 179,577.94
10UK-00804/07/200531/08/20050.00 0.00
11UK-00901/06/200531/08/2005228,321.67 228,321.67
12UK-01001/06/200531/08/2005301,150.69 301,150.69
13UK-01101/06/200531/08/2005100,462.68 100,462.68
14UK-01204/07/200531/08/2005
15
United Kingdom


... and here is the Statistics sheet on which I want to produce summarised/filtered data. The country-selector drop-down box is in cell B4; the activity period is selected in cells C4 and D4. I want to pull out the reference number, amount invoiced and amount paid for each matching invoice:
Invoices sample.xls
ABCDE
1
2Select countrySelect activity period
3FromTo
4Austria01/06/200531/08/2005
5
6Invoice ref.Amount invoicedAmount Paid
7
8
9
10
11
12
13
14
15
16
17
Statistics


I have been trying to use formulae to achieve this, but to no avail. I set named ranges for each country worksheet, and used a combination of INDEX, OFFSET, INDIRECT and MATCH. The problem is I only get the first exact match, while I want to pull out all matching invoices for a given country/activity period, and have them listed from row 7 downwards. I suspect I need to use some kind of VBA code filter, but I haven't a clue where to start....

With kind regards,
Simon
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
First make sure the headings in B6:D6 on your Statistics sheet exactly match those in your source data worksheets, ie Reference, Amount invoiced and Amount paid.

Then add a button from the Forms Toolbar and assign it this code in a General module:

Code:
Sub GetData()
    Dim Sh As Worksheet
    Dim Rng As Range
    Application.ScreenUpdating = False
    With Worksheets("Statistics")
        Set Sh = Worksheets(.Range("B4").Value)
        Set Rng = Sh.Range("A2:E" & Sh.Range("A65536").End(xlUp).Row)
        Rng.AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=.Range("C4:D4"), CopyToRange:=.Range("B6:D6"), Unique:=False
        .Range("A1").Select
    End With
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Andrew,
That nearly works... it pulls the data out alright, but does not filter it by date. No matter what dates I enter in cells C4:D4 it pulls out all 12 UK records from my sample source data.

Many thanks for the macro anway. I have no coding experience but I can almost see how it works. But I can't work out why it doesn't filter correctly.

Simon
 
Upvote 0
Oops :oops:

Incorrect criteria range:

Code:
Sub GetData()
    Dim Sh As Worksheet
    Dim Rng As Range
    Application.ScreenUpdating = False
    With Worksheets("Statistics")
        Set Sh = Worksheets(.Range("B4").Value)
        Set Rng = Sh.Range("A2:E" & Sh.Range("A65536").End(xlUp).Row)
        Rng.AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=.Range("C3:D4"), CopyToRange:=.Range("B6:D6"), Unique:=False
        .Range("A1").Select
    End With
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Magic! Thanks Andrew, it works a treat.

At the risk of pushing my luck, how can I get the macro to pull out invoices which have dates within the date filter range (i.e. not just an exact date match)?

E.g. filter range is 01/01/2005 to 31/12/2005
If there was an invoice with activity period 01/05/05 to 01/07/05 I would like to retrieve this too.

Many thanks, this certainly helps me to get my head around how to write code!

Simon
 
Upvote 0
To do that you would have to construct a criteria range from the dates entered. On your Statistics sheet copy C3:D3 to E3. In E4 enter:

=">="&C4

and in F4 enter:

="<="&D4.

Now try this code:

Code:
Sub GetData()
    Dim Sh As Worksheet
    Dim Rng As Range
    Application.ScreenUpdating = False
    With Worksheets("Statistics")
        Set Sh = Worksheets(.Range("B4").Value)
        Set Rng = Sh.Range("A2:E" & Sh.Range("A65536").End(xlUp).Row)
        Rng.AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=.Range("E3:F4"), CopyToRange:=.Range("B6:D6"), Unique:=False
        .Range("A1").Select
    End With
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Again that works a treat. Many thanks!

Just to complicate matters, what I posted was sample data. I am trying to get it to work on my real (confidential) data. In my real data sheet I have more columns than the five that I posted, as follows:

Invoice ref. is still in column A
Activity period is in columns F & G
Amount invoiced is in column M
Amount paid is in column N

My stats sheet is the same as posted.

So I tweaked the range specified in line 7 of your code:

Set Rng = Sh.Range("A2:N" & Sh.Range("A65536").End(xlUp).Row)

Now I get "run time error 1004: the extract range has a missing or illegal file name" and the debugger highlights this line:

Rng.AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=.Range("E3:F4"), CopyToRange:=.Range("B7:D7"), Unique:=False

Any ideas?
 
Upvote 0
A - Unique ID
B - NAA Reference
C - Issue date
D - Receipt date
E - Service Type
F - From
G - To
H - Reference
I - Max amount
J - Date
K - By
L - Working hours
M - Amount invoiced
N - Amount paid

Some of these are in font colour white. There are main headings above in Row 1.

Headings in my Statistics worksheet are now:
B - Unique ID
C - Amount invoiced
D - Amount paid

I pasted these heading names from the other sheet to ensure they are the same.
 
Upvote 0

Forum statistics

Threads
1,214,601
Messages
6,120,465
Members
448,965
Latest member
grijken

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