Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Advanced Filter Command Button 5

Status
Not open for further replies.

BDNH

Technical User
Joined
Jan 22, 2003
Messages
33
Location
CL
Can someone give me an example VBA code to be able to use a command button to run an advanced filter in Excel 97. I dont know VBA so something I can cut and copy would be great. (I can set the data,criteria, and extract ranges)

Also, if the extract range can be set to another sheet that would help alot.

Thanks in advance

 
Try the following :

Sub AdvFilter()
Range("DataList").AdvancedFilter _
Action:=xlFilterCopy, _
CriteriaRange:=Range("Criteria"), _
CopyToRange:=Range("Destination"), _
Unique:=False 'or True
End Sub

I have used named ranges for convenience.

"DataList" is the range you wish to filter,
"Criteria" is the range containing the Criteria and
"Destination" is the range ( a single Row with the Field names you wish to copy to) , and can be on a different worksheet.

A.C.
 
acron,

Thanks for your reply. I copy and pasted your code and it looks like this:

Private Sub CommandButton1_Click()
Range("A2:AG31").AdvancedFilter _
Action:=xlFilterCopy, _
CriteriaRange:=Range("AK2:BQ2"), _
CopyToRange:=Range("AK5:BQ5"), _
Unique:=False

End Sub



Nothing happens at all when I click the command button.
I tried changing the ranges to named ranges and I get this message:

Run-time error '1004'
Advanced filter method of range failed

When I hit Debug the body of the code is highlighted in yellow. I also tried using your code exactly as it is posted

Sub AdvFilter()
Range("DataList").AdvancedFilter _
Action:=xlFilterCopy, _
CriteriaRange:=Range("Criteria"), _
CopyToRange:=Range("Destination"), _
Unique:=False
End Sub


but nothing happens. Any Ideas of what I'm doing wrong?

Thanks
 
Thanks anyway acron, i was able to use the record macro function and assign it to a command button (with a little help)

Thanks for your response
 
BDHN,

Based on what you've described, I feel a need to help you out further...

OBSERVATION 1)

It appears the reason why you weren't able to get A.C.'s code to work, is because you are not yet aware of what he was referring to by reference to "range names".

A range name is an arbitrary name that you can create/assign to any cell or range-of-cells anywhere in the workbook. So where he referred to "DataList", you needed to create such a name (or you could use your own preference for the name) for the range of cells where your data is located. In your example, you've referred to A2:AG31.

And you would also need to do the same for "Criteria" and "Destination".

Please appreciate the EXTREME SIGNIFICANCE of why it is IMPORTANT to create and use range names... a) It allows you to make your application much more user-friendly for yourself and end-users, and b) with reference to using VBA code (such as what you recorded), such code is referred to as "HARD CODE" - meaning that if and when changes/modifications are made to your application, your code will NOT change. With many smaller applications, they tend to "grow" and therefore you could end up with a larger application where you have several VBA routines that you have "hard coded". Then, when you need to make changes (insertion/deletion of rows, moving data from place to place, etc), you will of course need to review all your code and make whatever changes are required to update your VBA code.

IF "range names" are used, however, there will NOT be any need to modify the VBA code. This is because "internally", Excel maintains a "link" between the names and the cells to which the names have been assigned. Thus, when rows/columns are inserted/deleted, or data moved, the range names are automatically adjusted.

OBSERVATION 2)

I noticed you entered your "criteria" and "extraction" range (or destination range) on the SAME sheet as your data list (or database) - i.e. EVERYTHING on the SAME sheet.

I appreciate you might have had good reason to do this. Depending on the version of Excel you have, you might have encountered two BUGS in the form of FALSE ERROR MESSAGES that tell you: a) It's NOT possible to place your criteria on a different sheet, and b) It's NOT possible to extract data to a SEPARATE sheet.

With older versions of Excel (as well as current), it is not only possible to place criteria on a SEPARATE sheet, but it it actually "PREFERABLE". This is because if you do place your criteria on the SAME sheet, Excel will give you plenty of headaches - because it will attempt to "think for you", and in the process, will cause confusion and errors regarding the use of field names in the criteria, etc. When you place your criteria on a SEPARATE sheet, and if you use formula(s) in your criteria, be aware that Excel will show #NAME? in the cell containing your formula. This is normal. In later versions of Excel, buried in the bowels of the Help function, Excel does mention that this #NAME? is normal in the case of creating "criteria".

It IS possible to extract data to one or more SEPARATE sheets. If you attempt to do so via the menu, as mentioned, you might be stopped by the ERROR MESSAGE. However, be aware that extraction of data to a separate sheet using VBA code (like the example provided by A.C.) works with no problem - provided you first create the range names. The "destination" range (or whatever name you decide to use) can be assigned to ANY worksheet. Normally, it should be assigned to the top row that contains the field names. You can be selective in the field names you use, and you can change the order.

You can set up separate routines that reference the SAME database, but reference different criteria ranges, for extraction of "different" data to different sheets - potentially with different (pre-set) headings and print settings suited for the different types of reports being generated.

I hope this helps get you "up and running" with this powerful component of Excel.

Regards, ...Dale Watson dwatson@bsi.gov.mb.ca
 
I wonder if by using "A2:AG31", you omitted to include the field names. Were the field names in A1:AG1 ?. If so then they should be included, i.e. use "A1:AG31". You must include the field names in the criteria and extract ranges also.

Best advice now is to read Dale's excellent dissertation on Advanced Filtering and Range Names above. Worth a star.

A.C.

 
A.C.

Thanks for the endorsement and the STAR. Your contribution is also worthy of a STAR. ;-)

Regards, ...Dale Watson dwatson@bsi.gov.mb.ca
 
Dale,

Thank you for your observations. I did however understand the named ranges concept and named my database range, criteria range, and exctraction range accordingly.

I still dont know what I was doing wrong with the code.
I ended up using the record macro function (also with named ranges) then assigning the module to a command button. This seams to be working great and I have extracted the data to a seperate sheet

I still want to know what I was doing wrong with Acron's code. Im going to try it again on a "cleaned-up" version of the same data and I will be aware of Acron's last observation.

All your and Acrons comments have been very welcome and I appreciate your help

BDNH

 
I use advanced filters quite a lot and have simply recorded a macro setting up the filter which I use off a button to rebuild the filter again and again but I will try the VBA examples given here.

Just one point I clear the destination range including the labels in the macro before I rebuild the filter, for two reasons:

a) if the first use of the filter returns 50 rows and a subsequent use returns 25 rows then then last 25 rows stay in place from the pevious use of the filter.

b) if you don't clear the labels you often get the filter miss-operating and only the first column gets populated

There are a number of bugs in the advanced filter but once you are aware of them it is a powerful tool.

Hope I'm not saying things taht you already know!

Regards

Keith
 
KLMorgan

Good points, I will sure keep them in mind. As I've said before All comments and help are welcome.

Thanks BDNH
 
Sorry to be a pain, asking questions on someone elses questions but....

When I typed the code in to Excel 2000 it showed compile errors and it seemed to be the := after the Action it didn't like but once I pasted the code in it worked perfectly even when I changed the range names and the macro name.

I can't see what caused the errors

Regards

Keith
 
Acron/DaleWatson,
A star to each of you for clearing this up for me. Many thinaks. Now a question: How can I make the CopyToRange:=Range("Destination") argument dynamic so that data extracted from DataList is added to the end of the Destination range?

Joe
 
acron,

just wanted to let you know that I tried this again and it worked great!

I Don't know what I was doing wrong before --- again, thanks for your help

BDNH
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top