tags:

views:

335

answers:

1

I need to use VBA to filter some information in excel. As I have an excel with 20 columns, now want to use AutoFilter function to search in some columns if it contains one value (Ex: ID010). what i want is it'll display all rows that have at least one column contains ID010.

Currently, i use the below code to search. However, it could not find any data because all the criteria seem to tie together using AND operator

' Search range, [argIn]---> search value
With [D5:M65536]
        .AutoFilter Field:=4, Criteria1:=argIn
        .AutoFilter Field:=5, Criteria1:=argIn
        .AutoFilter Field:=6, Criteria1:=argIn
        .AutoFilter Field:=7, Criteria1:=argIn
        .AutoFilter Field:=8, Criteria1:=argIn
        .AutoFilter Field:=9, Criteria1:=argIn
        .AutoFilter Field:=10, Criteria1:=argIn
        .AutoFilter Field:=11, Criteria1:=argIn
        .AutoFilter Field:=12, Criteria1:=argIn
        .AutoFilter Field:=13, Criteria1:=argIn
End With

I wonder if anyone could give me some hints or examples how to handle this issue.

Thank you in advance.

+1  A: 

The autofilter functionality won't help you here, as you've discovered the behaviour is to AND the filters together.

One approach would be to have a control column in the spreadsheet (perhaps maintained through VBA) populated with worksheet functions like

=COUNTIF(A$2:A$20, "ID010")

And then apply the filter to the control column.

Steve Homer
Thank for your reply. It worked perfectly
tlpd