Mario
Mario

Reputation: 177

VBA Excel: Show visible cells in listbox only

I have the below mentioned code, I am trying to load filtered cells only into the listbox but I don't know why the below mentioned code is not functional.

Legend:

Upvotes: 1

Views: 2637

Answers (2)

user2271770
user2271770

Reputation:

The problem might stem from the fact that you add to the list box the value of a special cell that might not exist if the cell is hidden.

Try for the body of the For loop:

' ... previous code '
If Not WsSelector.Rows(k).EntireRow.Hidden Then
        .AddItem WsSelector.Cells(k, 1).Value
End If
' rest of the code ... '

Also, make sure that AvailableNumberList points to the correct object in your code.

Upvotes: 1

user4039065
user4039065

Reputation:

You're are using PatternCounter as the upper limit in your For .. Next but this is being set using the MAX (e.g. 4) subfunction of SUBTOTAL. This might work on sequential numbers in an unfiltered list but it is unlikely to be accurate in a filtered list. Using the COUNT (2/102) or COUNTA (3/103) subfunction might be more appropriate.

You were using SUBTOTAL(4, ...) so I would assume that you are dealing with numbers. Use a straight count on numbers on visible cells in column F and modify the remainder of the code to resemble this.

    PatternCounter = Application.WorksheetFunction.Subtotal(2, WsSelector.Range("F:F"))
    With WsSelector.cells(1,1).currentregion.offset(1,0).SpecialCells(xlCellTypeVisible)
        AvailableNumberList.Clear
        For k = 1 To PatternCounter
            AvailableNumberList.AddItem .cells(k, 1).Value
        Next k
    End With

Upvotes: 1

Related Questions