Solved

filtering with variables

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

What is a Form List Box? (skip if you know this) The forms List Box is the alternative to the ActiveX list box. If you are using excel 2007, you first make sure you have a developer tab (click the Orb)->"Excel Options"->Popular->"Show Developer tab…
How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
Viewers will learn the basics of slicers and timelines for both PivotTables and standard Excel tables in Excel 2013.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now