Solved

filtering with variables

Posted on 2013-06-26
6
146 Views
Last Modified: 2014-05-23
hi all

i have a list box of values which i would like to exclude from the my data
 
if i add the following code the filter works
 
Range("A:AA").AutoFilter 14, "<>BGD*", xlAnd, "<>TTS*"
 
i've created a function to build the underlined text based on the values in the list box but when i use the variable from the function the auto filter does not work. any suggestions why?
 
Range("A:AA").AutoFilter 14, variable
0
Comment
Question by:tessupport
  • 2
  • 2
6 Comments
 
LVL 24

Expert Comment

by:Steve
ID: 39277498
How are you building the variable.. can you show the code for that please.

if you debug.print variable you should be able to see the "built string".
how does it look?

Soem sample data would be handy as it makes it easier to write code suited to you.
0
 
LVL 1

Author Comment

by:tessupport
ID: 39277516
sub example ()

---- code-------


            Call WH_FILTER(WH)
           
            Worksheets("Version Control").Range("e1").Value = WH

---- code-------

end sub


*************************************************************************

Function WH_FILTER(WH)

Dim i As Long
Dim iEnd As Long
Dim WH1 As String
   
    iEnd = Worksheets("Control").ListBoxWH2.ListCount - 1
   
    If iEnd = 0 Then
       WH1 = Worksheets("Control").ListBoxWH2.List(i)
       WH = """<>" & WH1 & """"
    End If
   
    If iEnd > 0 Then
         
        For i = 0 To iEnd
            WH1 = Worksheets("Control").ListBoxWH2.List(i)
                       
            If i = 0 Then
                WH1 = """<>" & WH1 & "*"","
            End If
                       
            If i > 0 And i < iEnd Then
                WH1 = WH & " xlAnd, " & """<>" & WH1 & "*"","
            End If
           
            If i = iEnd Then
                WH1 = WH & " xlAnd, " & """<>" & WH1 & "*"""
            End If
       
            WH = WH1
        Next i
    End If

End Function
0
 
LVL 24

Accepted Solution

by:
Steve earned 500 total points
ID: 39277667
OK, so, looking into this one...

AutoFilter has a maximum number of criteria of 2.
Hence your code not working.

You can use AdvancedFilter and put the list into a hidden range, this will do the job:
If you need help filling the criteria range with the listbox info that is not a problem, but I think you can handle that.

Sub FilterInPlace()
Dim rng1, rng2
Set rng1 = Sheets("Sheet1").Range("A1:AA" & Sheets("Sheet1").Range("N" & Rows.Count).End(xlUp).Row)
Set rng2 = Sheets("Sheet2").Range("A1:B2")

rng1.AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=rng2, Unique:=False
End Sub

Open in new window


See attached for an example.

You can also copy the results of the filter to another sheet.
This may be handy. See second workbook for copy data to sheet 3.
Example.xlsm
Example-With-data-copied-to-shee.xlsm
0
 
LVL 1

Author Comment

by:tessupport
ID: 39278560
Brill thanks
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question