How to show just filtered rows in my userform listbox

前端 未结 3 1866
甜味超标
甜味超标 2021-01-24 08:53

I have one Excel sheet, one userform and a listbox is in userform. Now when i filter my sheet and update listbox by click on button that is on my user form i see all rows in lis

3条回答
  •  旧巷少年郎
    2021-01-24 09:24

    When using arrays, the listbox header goes away...
    So you could try to solve the problem using two ideas:
    1. Sort the table, to make the filtered values come to top (under the header of the table);
    2. Filter the table;

    Private Sub fillListBox()
    'lstGrade as the listbox component
    Dim oTab As ListObject
    Dim oRng As Range
        Set oTab = Sheets("Sheet1").ListObjects("MyTable")
    
        'remove any filter and then sort the data using the "SomeValue" to stick it on top of the table
        With oTab
            .Range.AutoFilter
            .Sort.SortFields.Clear
            .Sort.SortFields.Add _
                Key:=Range("MyTable[Column3]"), _
                SortOn:=xlSortOnValues, _
                Order:=xlAscending, _
                CustomOrder:="SomeValue", _
                DataOption:=xlSortNormal
            With .Sort
                .Header = xlYes
                .MatchCase = False
                .Orientation = xlTopToBottom
                .SortMethod = xlPinYin
                .Apply
            End With
        End With
    
        'note that "SomeValue" is the same as in the sorted area above
        oTab.Range.AutoFilter 2, "SomeValue"
    
        '"save" the data into the new range object
        Set oRng = oTab.DataBodyRange.SpecialCells(xlCellTypeVisible)
        lstGrade.RowSource = oRng.Address
    End Sub
    

提交回复
热议问题