+ Reply to Thread
Results 1 to 5 of 5

How to set multiple criteria for .advancedfilter in VBA

Hybrid View

  1. #1
    Registered User
    Join Date
    11-19-2014
    Location
    Athens
    MS-Off Ver
    2013
    Posts
    4

    How to set multiple criteria for .advancedfilter in VBA

    Hello All,

    I am trying to use the xlfiltercopy action with the .advancedfilter function in vba to filter through some data and copy it to another sheet. The criteria for the filter, however, will be all values between two variable values that will change on every iteration of the filter, i.e a 'continuous' data set so printing all the criteria onto a range is not possible. Consequently, as the .advancedfilter function doesn't seem to allow for criteria1, criteria2, etc.. like .autofilter, I need a way to set the criteriarange to a 'between' function, i.e. x
    The code I have so far is below.

    Thanks!

    Dorian

    
    Sub Filter_Report()
    
    
    Dim crit1 As Single
    Dim crit1lo As Range
    Dim crit1hi As Range
    Dim crita As Range
    Dim Report As Worksheet
    Dim r As Integer
    Dim data As Worksheet
    Dim crit1rng As Range
    Dim temp As Worksheet
    
    
    Workbooks("MT_Optimizer").Sheets("AAPL_Days_1").Select
    Set data = Workbooks("MT_Optimizer").Sheets("AAPL_Days_1")
    
    
    r = data.Cells(data.Rows.Count, "A").End(xlUp).Row
    Set crita = data.Range("AY30:AY" & r)
    
    
    
    
    crit1 = data.Cells(30, 51).Value
    Set crit1lo = data.Cells(30, 51).Value - 0.05
    Set crit1hi = data.Cells(30, 51).Value + 0.05
    Set crit1rng = Union(Range("<=" & crit1hi) & Range(">=" & crit1lo))
    
    
    
    
    Worksheet.Add
    ActiveSheet.Name = temp
    
    
    crita.AdvancedFilter Action:=xlFilterCopy, criteriarange:=crit1rng, copytorange:=temp("a1"), unique:=False
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    End Sub
    Last edited by dorian821; 06-02-2016 at 04:44 AM.

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: How to set multiple criteria for .advancedfilter in VBA

    Dorian

    It is possible to set a 'between' criteria for advanced filter in a range.

    In the range you would repeat the field twice in the header row and below you would have your ">=" & crit1lo and "<=" & crit1hi.

    Try doing it manually to see what I mean.

    PS Can you add code tags when posting code?
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    11-19-2014
    Location
    Athens
    MS-Off Ver
    2013
    Posts
    4

    Re: How to set multiple criteria for .advancedfilter in VBA

    Thanks, What do you mean repeat the field twice in the header row?

    I've tried what I think you are suggesting, and am receiving the following error at the line for the advanced filter


    Method 'range' of object '_Global' failed

    Sub Filter_Report()
    
    Dim crit1           As Single
    Dim crit1lo         As Single
    Dim crit1hi         As Single
    Dim crita           As Range
    Dim report          As Worksheet
    Dim r               As Integer
    Dim rr              As Integer
    Dim rt               As Integer
    Dim data            As Worksheet
    Dim data1           As Range
    Dim crit1rng        As Range
    Dim temp            As Worksheet
    Dim s               As Integer
    
    Dim rate            As Range
    Dim symbol          As String
    
    
    Set temp = Workbooks("MT_Optimizer").Sheets("Temp_Report")
    Set report = Workbooks("MT_Optimizer").Sheets("Report")
    Workbooks("MT_Optimizer").Sheets("AAPL_Days_1").Select
    Set data = Workbooks("MT_Optimizer").Sheets("AAPL_Days_1")
    Set rate = Workbooks("MT_Optimizer").Sheets("Temp_Report").Range("G1:R3")
    
    
    s = 4
    r = data.Cells(data.Rows.Count, "A").End(xlUp).Row
    rr = report.Cells(data.Rows.Count, "A").End(xlUp).Row
    rt = temp.Cells(data.Rows.Count, "A").End(xlUp).Row
    
    
    
    crit1 = data.Cells(8400, 51).Value
    crit1lo = crit1 - 0.05
    crit1hi = crit1 + 0.05
    
    
    
       
        
        'If ActiveSheet.AutoFilterMode Then
            'ActiveSheet.ShowAllData
        'End If
        
            
        data.Range("A4:BO" & r).AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=Range(">=" & crit1lo & "<=" & crit1hi), _
            copytorange:=temp.Range("A5")
        
        'Rows(s + 1).Select
        'Range(Selection, Selection.End(xlDown)).Select
        'Selection.Copy
        
        'temp.Activate
        'temp.Range("A5").Select
        'temp.Paste
        temp.Calculate
        temp.Range("G2:AP2").Select
        Selection.Copy
        
        report.Select
        rr = report.Cells(data.Rows.Count, "A").End(xlUp).Row
       
        report.Cells(rr + 1, 10).PasteSpecial xlPasteValues
        
        report.Cells(rr + 1, 2).Value = crit1
        
    end sub
    Last edited by dorian821; 06-01-2016 at 09:12 AM. Reason: to add tags

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: How to set multiple criteria for .advancedfilter in VBA

    The criteria range needs to be on a worksheet, you can't 'create' it in the code.

    You can however populate the criteria range using code.

  5. #5
    Registered User
    Join Date
    11-19-2014
    Location
    Athens
    MS-Off Ver
    2013
    Posts
    4

    Re: How to set multiple criteria for .advancedfilter in VBA

    Thanks. How can I populate a range with a 'continuous' number set. In other words, I need to filter all value between, let's say, 50 & 75, where the elements have up to 4 decimal points. That is a lot of numbers to put into a range, especially when a new range needs to be defined and populated for every iteration of over 100,000 iterations. There isn't a way to define the limits of the set as with the 'between' function on excel?

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Advancedfilter with multiple criteria?
    By brucemc777 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-06-2014, 03:34 PM
  2. [SOLVED] VBA AdvancedFilter Three Criteria
    By goss in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-17-2014, 08:17 AM
  3. excel 2010 vba - advancedfilter returning ALL rows, not just criteria ones?
    By wyattea in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-12-2013, 02:36 AM
  4. Loop with AdvancedFilter Using Multiple Sheets
    By freybe06 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-14-2012, 01:10 PM
  5. AdvancedFilter with no criteria
    By opopanax666 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-26-2007, 08:35 AM
  6. [SOLVED] Setting a criteria in an AdvancedFilter macro
    By Wendell A. Clark in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-11-2006, 11:20 AM
  7. [SOLVED] Advancedfilter
    By ram in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-29-2005, 10:20 PM
  8. [SOLVED] Advancedfilter copy in place doesn't use criteria range correctly
    By Josh in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-09-2005, 10:35 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1