VBA - array of filtered data to populate listbox

前端 未结 3 1915
误落风尘
误落风尘 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 18:53

    Try, please the next code, if you want to use a continuous (built) array. It is possible to build it from the discontinuous range address, too:

        Sub Filter_Offene()
          Dim sh As Worksheet, lastRow As Long, rngFilt As Range, arrFin As Variant
    
          Set sh = Sheets("Data")
          lastRow = sh.Range("R" & Rows.count).End(xlUp).Row
            rngFilt.AutoFilter field:=18, Criteria1:="WAHR"
    
            Set rngFilt = rngFilt.Offset(1).SpecialCells(xlCellTypeVisible)
    
            arrFin = ContinuousArray(rngFilt, sh, "R:R")
    
            With ComboBox1
                .list = arrFin
                .ListIndex = 0
            End With
        End Sub
    
        Private Function ContinuousArray(rngFilt As Range, sh As Worksheet, colLet As String) As Variant
            Dim arrFilt As Variant, El As Variant, arFin As Variant
            Dim rowsNo As Long, k As Long, i As Long, j As Long, arrInt As Variant
    
            arrFilt = Split(rngFilt.address, ",")' Obtain an array of areas addresses
            'real number of rows of the visible cells range:
            For Each El In arrFilt
                 rowsNo = rowsNo + Range(El).Rows.count
            Next
            'redim the final array at the number of rows
            ReDim arFin(1 To rowsNo, 1 To rngFilt.Columns.count)
    
            rowsNo = 1
            For Each El In arrFilt            'Iterate between the areas addresses
                rowsNo = Range(El).Rows.count 'number of rows of the area
                arrInt = ActiveSheet.Range(El).value' put the area range in an array
                For i = 1 To UBound(arrInt, 1) 'fill the final array
                    k = k + 1
                    For j = 1 To rngFilt.Columns.count
                         arFin(k, j) = arrInt(i, j)
                    Next j
                Next i
            Next
        ContinuousArray = arFin
    End Function
    

提交回复
热议问题