?
Solved

filtering with variables

Posted on 2013-06-26
6
Medium Priority
?
150 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 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

752 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