Solved

filtering with variables

Posted on 2013-06-26
6
145 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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

831 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