Forum Discussion

Kenneth Green's avatar
Kenneth Green
Brass Contributor
Apr 26, 2024
Solved

My VBA copies row if cell NOTBLANK but I need target columns in specific order (Code in post)

Windows 10 Excel 2019 In a workbook, I have two worksheets, "Track Data" and "Titles Data".   In worksheet "Track Data"... Columns A:J will always have data on each row and Column K may have d...
  • djclements's avatar
    djclements
    Apr 26, 2024

    Kenneth Green The <> operator on its own will only work if the cells in column K are truly blank. Since it's not working for you, I'm guessing the cells contain zero-length strings, most likely the result of a formula returning "". In this case, you can try using a formula as the criteria. There are 3 rules when using formulas in the criteria range:

    1. No heading (or use a custom heading not found in the data range, ie: "Criteria1").
    2. The formula must result in True or False.
    3. The formula should reference the first row of the data range.

    So, in the "AdvFilter" worksheet, delete the heading in cell A1 (or use Criteria1 as the heading), then try either one of the following formulas in cell A2:

     

    ='Track Data'!K2<>""
    
    //OR
    
    =TRIM('Track Data'!K2)<>""
    
    //OR
    
    =LEN('Track Data'!K2)>0

     

    Formula Criteria Example

     

    To address the Advanced Filter results lingering afterwards, which can significantly increase the size of the file when dealing with large amounts of data, just add one line of code to clear the results (rgResults.Offset(1).ClearContents), immediately after the line of code that copies the results to an array:

     

    'Copy the results to an array
        Dim rgResults As Range, n As Long
        Set rgResults = wsFilter.Range("A5").CurrentRegion
        n = rgResults.Rows.Count - 1
        If n < 1 Then GoTo CleanUp
        Dim arr As Variant
        arr = rgResults.Offset(1).Resize(n).Value
        rgResults.Offset(1).ClearContents

     

    If you have any other questions, let me know. Cheers!

Resources