Consulting

Results 1 to 8 of 8

Thread: Filtercopy does not return records listed with criteria value below 7 characters

  1. #1
    VBAX Regular
    Joined
    Oct 2010
    Posts
    18
    Location

    Filtercopy does not return records listed with criteria value below 7 characters

    I wish to extract a selection of fields from one list in sheet1 to a target area in second sheet matching specified criteria. Basically, this is done using Excel Advanced Filter, specifying relevant arguments (looping through the list of criteria values).
    rSourceRng.AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=Range(sFiltRng), CopyToRange:=WS2.Range(sDestRng), Unique:=True

    Each criteria is a numeric values (following a standard Danish standard numeric format) formatted as text which applies also for the values listed in database.

    The problem is that Filtercopy does not return those database records listed with criteria value below 7 characters (in this case values below 1.000.000).

    Excel itself will gladly return all records matching the extract criteria. (Test by placing the curser in the target range before activating advanced filter). Note also, that the worksheet function =dcounta will return the correct number of records as well in VBA as in Excel.

    Any ideas?I wish to extract a selection of fields from one list in sheet1 to a target area in second sheet matching specified criteria. Basically, this is done using Excel Advanced Filter, specifying relevant arguments (looping through the list of criteria values).
    rSourceRng.AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=Range(sFiltRng), CopyToRange:=WS2.Range(sDestRng), Unique:=True

    Each criteria is a numeric values (following a standard Danish standard numeric format) formatted as text which applies also for the values listed in database.

    The problem is that Filtercopy does not return those database records listed with criteria value below 7 characters (in this case values below 1.000.000).

    Excel itself will gladly return all records matching the extract criteria. (Test by placing the curser in the target range before activating advanced filter). Note also, that the worksheet function =dcounta will return the correct number of records as well in VBA as in Excel.

    Any ideas?

    Sub FilterCopy()
    '---
    'Extract records using advanced filter
    '---
     
    Dim WS1 As Worksheet, WS2 As Worksheet, cl As Range
    Dim rSourceRng As Range     'Database list
    Dim rTargetRng As Range     'Extract to range
    Dim rFilterRng As Range     'Filter range
    Dim rExtractLst As Range    'List of values to iterate
     
    Dim sDestRng As String      ')
    Dim sFiltRng As String      ') String arguments
    Dim lRecords As Long        'Count of matching records in database
     
    Set WS1 = Worksheets("SourceData")
    Set WS2 = Worksheets("TargetData")
     
    Set rExtractLst = Range("VBARefList")
    Set rSourceRng = WS1.Range("A1").CurrentRegion
    Set rTargetRng = WS2.Range("A1")
    Set rFilterRng = Range("VBACriteria")
     
    For Each cl In rExtractLst  'Iterate list of criteria to examine individually
        'Place criteria value in criteria range
        rFilterRng.Cells(2).Formula = "=" & """=" & cl.Value & """"
       
        'Clear target area to eliminate from preceding iteration
        rTargetRng.CurrentRegion.Offset(1, 0).EntireRow.Delete
       
        'Measure string arguments of records coming from source data
        lRecords = Application.DCountA(rSourceRng, 1, rFilterRng) + 1
        sDestRng = Range(WS2.Cells(1, 1), WS2.Cells(lRecords, rTargetRng.CurrentRegion.Columns.Count)).Address
        sFiltRng = "'" & rFilterRng.Parent.Name & "'!" & rFilterRng.Address
       
        'Execute advance filter copy
        rSourceRng.AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=Range(sFiltRng), CopyToRange:=WS2.Range(sDestRng), Unique:=True
    Next cl
     
    'Finish off
    Set WS1 = Nothing
    Set WS2 = Nothing
    Set rSourceRng = Nothing
    Set rTargetRng = Nothing
    Set rFilterRng = Nothing
    Set rExtractLst = Nothing
           
    MsgBox "Finished...", vbOKOnly + vbInformation, "VBA\ Filtercopy"
    End Sub
    Attached Files Attached Files

  2. #2
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    This is sufficient:

    Sub M_snb()
        Sheets("parametersheet").Cells(2, 1) = Replace(Sheets("sourcedata").Cells(9, 3).Value, ".", "")
       Sheets("sourcedata").Cells(1).CurrentRegion.AdvancedFilter 2, Sheets("parametersheet").Cells(1).CurrentRegion, Sheets("targetdata").Cells(20, 1)
    End Sub

  3. #3
    VBAX Regular
    Joined
    Oct 2010
    Posts
    18
    Location
    Thanks. I see what you are doing. Following that lead, I can specify the criteria directly with
    rFilterRng.Cells(2).Formula = "=" & """=" & Replace(cl.Value, ".", "") & """"

    However, I can't rule out possible alternative use of "." placed in a non-numeric format. E.g.
    ProductA.01
    ProductA.02
    e.c.t.

    I need to navigate safely in how excel evaluates the filter criteria against the field data in the source list (as mentioned, excel itself is happy to filtercopy directly on the native criteria).

    Look forward to any ideas

  4. #4
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    No: you misinterpreted my suggestion.

    Using
    rFilterRng.Cells(2).Formula = "=" & """=" & Replace(cl.Value, ".", "") & """"
    was the crux why your code didn't work.
    Please study my suggestion again: it's about a Value, not a formula.
    The criterionrange must contain Values

  5. #5
    VBAX Regular
    Joined
    Oct 2010
    Posts
    18
    Location
    I think I got it, and you’re right – Filtercopy works correctly on values. The thing is, however, - I can’t assume that I’m always looking for values – thus I’m not allowed to remove “.” from the criteria. At the core of my problem is, that VBA Filtercopy works correctly on 7 digits (e.g. =”=1.000.000”), but not on 6 digits (e.g. =”=999.999”). Excel Advanced Filter works correctly on both.

  6. #6
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    There's no problem in using 2 filter criteria at the same time: 1875 or 1.875

  7. #7
    VBAX Regular
    Joined
    Oct 2010
    Posts
    18
    Location
    Close but no cigar as they say. Imagine the value 0.735. You will be looking for =”=735” and =”0.735”. Neither of these will be find the records correctly. I liked the idea though …

  8. #8
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    You shouldn't use the = sign.

    See the attachment.
    Attached Files Attached Files
    Last edited by snb; 02-28-2014 at 02:43 AM.

Tags for this Thread

Posting Permissions

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