VBA - array of filtered data to populate listbox

前端 未结 3 1914
误落风尘
误落风尘 2021-01-13 18:23

Okay so I am filtering a sheet (\"Data\") by a criteria:

Sub Filter_Offene()
    Sheets(\"Data\").Range(\"A:R\").AutoFilter Field:=18, Criteria1:=\"WAHR\"
En         


        
3条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-13 19:11

    Here is a fun little fact, Excel creates an hidden named range once you start filtering data. If you have continuous data (headers/rows) this would return your range without looking for it. Though since it seem to resemble UsedRange it may still be better to search your last used column and row and create your own Range variable to filter. For this exercise I'll leave it be. Furthermore, as indicated in the comments above, one can loop over Areas of visible cells. I'd recommend a check beforehand just to be safe that there is filtered data other than headers.

    Sub Test()
    
    Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets("Data")
    Dim Area as Range
    
    ws.Cells(1, 1).AutoFilter 18, "WAHR"    
    With ws.Range("_FilterDatabase")
        If .SpecialCells(12).Count > .Columns.Count Then
            For Each Area In .Offset(1).Resize(.Rows.Count - 1, .Columns.Count).SpecialCells(12).Areas
                Debug.Print Area.Address 'Do something
            Next
        End If
    End With
    
    End Sub
    

    The above works if no headers are missing obviously.

提交回复
热议问题