Solved

filtering with variables

Posted on 2013-06-26
6
149 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

717 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