Select the first filtered cell then move onto the next filtered cell down

后端 未结 3 1771
抹茶落季
抹茶落季 2021-01-12 15:37

I have an Excel spreadsheet that has contact details, for example:

    A                 B            C            D                    E
1   Select who you          


        
3条回答
  •  [愿得一人]
    2021-01-12 16:00

    I believe the Hidden property of a range is what you want. The following code worked for me:

    Dim row As Range
    For Each row In Range("MyTable").Rows
        If not row.EntireRow.Hidden Then
            ''' DO STUFF '''
        End If
    Next
    

    I have always found that using a For Each loop is a much cleaner way to iterate through data in an excel sheet. "MyTable" was the name I gave to the range of interest but if you prefer you can just enter a the limits of the range like Range("A1:D4"). Though I think it is a better practice to use named ranges as it makes your code more readable.

    EDIT: To address your comment...

    If you insert a row into the middle of a named range the limits of the range automatically expand. Though if your table is going to be the only data in the worksheet you can also use the UsedRange property of a worksheet object. For instance:

    Dim row As Range
    For Each row In Worksheets("MySheet").UsedRange.Rows
        If not row.EntireRow.Hidden Then
            ''' DO STUFF '''
        End If
    Next
    

    If all you have is the first row of the table you can expand this range to the full table using:

    dim FirstRow as Range
    dim LastRow as Range
    dim myTable as Range
    set FirstRow = Range("A1:B1")
    set LastRow = FirstRow.End(xlDown)
    set myTable = Range(FirstRow, LastRow)
    

    And then use the same For Each loop as before. Hope this helps!

提交回复
热议问题