98.7% Of all customers recommend us, we're so confident about our results we publish all reviews and stats
View Live Stats View ReviewsForum home » Delegate support and help forum » Microsoft Excel VBA Training and help » Using vba to filter data
Using vba to filter data
Resolved · Medium Priority · Version 2010
Kim has attended:
Excel VBA Advanced course
Using vba to filter data
Hi,
Could you help me? I am trying to filter data based on a number of criteria (that changed) in a report.
In sheet 4, I have 6 criterials
In sheet 1, I have report and I want to filter in column B (AutoFilter Field:=2) those 4 criterials
The record macro code, based on 4 criterials is:
ActiveSheet.Range("$A$1:$W$197").AutoFilter Field:=2, Criteria1:= _
"name A"
ActiveSheet.Range("$A$1:$W$197").AutoFilter Field:=2, Criteria1:= _
"=name A", Operator:=xlOr, Criteria2:= _
"=name B"
ActiveSheet.Range("$A$1:$W$197").AutoFilter Field:=2, Criteria1:=Array( _
"name A", "name B", _
"name C"), Operator:=xlFilterValues
ActiveSheet.Range("$A$1:$W$197").AutoFilter Field:=2, Criteria1:=Array( _
"name A", "Name B", _
"name C", _
"name D"), Operator:=xlFilterValues
The problem is Name A, B, C, D changes and could be increased based on the sheet 4.
I have stored the value to array and the array work. But I dont know how to make the filter works.
My code is
Dim vArray() As Variant
Dim iRow As Integer
Dim iCol As Integer
Dim iNumRow As Integer
Dim iNumCol As Integer
Sheets("Sheet4").Select
iNumRow = Range("a1").CurrentRegion.Rows.Count
iNumCol = Range("a1").CurrentRegion.Columns.Count
ReDim vArray(1 To iNumRow, 1 To iNumCol)
For iRow = LBound(vArray, 1) To UBound(vArray, 1)
For iCol = LBound(vArray, 2) To UBound(vArray, 2)
vArray(iRow, iCol) = Cells(iRow, iCol).Value
Next
Next
Sheets("Report").Select
ActiveSheet.Range("$A$1:$W$197").AutoFilter Field:=2, Criteria1:=vArray(iRow, iCol), Operator:=xlFilterValues
I know the bit of code doesnt work is the last one. But I dont know how to fix it.
could you please help?
Many thanks
Kim
RE: using vba to filter data
Hi Kim,
Thank you for the forum question. It is very complicated what you want. It is not a bad idea to store the criteria in an array.
To use the criteria stored in the array you must refer to the row and column.
vArray(1,1) will get you the first criteria (row 1, column 1), vArray(2,1) will get you the criteria from row 2 column .
You are using vArray(iRow, iCol) only together with the FOR NEXT loops.
I hope the answer has guided you in the right direction.
Kind regards
Jens Bonde
Microsoft Office Specialist Trainer
Tel: 0207 987 3777
Best STL - https://www.stl-training.co.uk
98%+ recommend us
London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector
RE: using vba to filter data
Hi Jens
Thank you for your reply. would that mean storing criteria in an array doesnt work for autofilter? is there a way to autofilter many criteria in 1 autofilter field by vba code and with the criteria is taken from a table of data ( could be just 1 column) if that s easier?
RE: using vba to filter data
Hi Kim,
I have done some codes which I hope will help you.
In the attached workbook you will find a flat list with data in sheet 1. Next to the data you will find a combo box. In the list in the combo box you can select a criteria from the list on sheet 2. If you change the criteria list on sheet 2 you need to press the button "Update filter list" and you will find new or changed criteria in the combo box. When you select a criteria from the combo box you will see that the list will be filtered.
I used a ActiveX control Combo box. You will find the ActiveX controls if you click on the developer Tab and in the controls group click Insert.
If you click Design Mode in the control group and double click the combo box in the worksheet you will get to the Privat module where you can find the code for the combo box. The two other buttons are form control buttons and they run the macros in Module1 in the workbook.
I hope that this will be a help.
Kind regards
Jens Bonde
Microsoft Office Specialist Trainer
Tel: 0207 987 3777
Best STL - https://www.stl-training.co.uk
98%+ recommend us
London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector
Attached files...
Fri 19 Sep 2014: Automatically marked as resolved.
Training information:
Welcome. Please choose your application (eg. Excel) and then post your question. Our Microsoft Qualified trainers will then respond within 24 hours (working days). Frequently Asked Questions
Any suggestions, questions or comments? Please post in the Improve the forum thread. |
Excel tip:Creating Quick Column ChartsSelect the information you want to appear in the chart and press F11. This creates a new chart on a seperate worksheet. |