代码之家  ›  专栏  ›  技术社区  ›  PerlDev

如何在excel中获取所有筛选的数据行(带隐藏列)?

  •  1
  • PerlDev  · 技术社区  · 15 年前

    我的数据表有过滤器和隐藏列。当过滤器应用时,我需要遍历所有过滤的数据。我使用:

    现在可见范围.Rows.Count为0;使用foreach循环“foreach(Excel范围排成可见范围。行)“行中没有从第一个隐藏列中切掉的所有列。

    我们如何循环过滤过的行?

    2 回复  |  直到 15 年前
        1
  •  4
  •   barrowc    15 年前

    我不会用 SpecialCells UsedRange 然后检查 Hidden 财产随你去。

    不确定您使用的是哪种语言,但下面是VBA中的一个示例:

    Dim rowIndex As Range
    
    With Worksheets("Sheet1")
        For Each rowIndex In .UsedRange.Rows
            If (rowIndex.Hidden) Then
                ' do nothing - row is filtered out
            Else
                ' do something
            End If
        Next rowIndex
    End With
    

    每一行(或者更确切地说是每一行 Range 对象引用者 rowIndex )将包含所有列,包括隐藏的列。如果需要确定列是否隐藏,那么只需检查 隐藏的

    Dim rowIndex As Range
    Dim colNumber As Integer
    
    With Worksheets("Sheet1")
        For Each rowIndex In .UsedRange.Rows
            If (rowIndex.Hidden) Then
                ' do nothing - row is filtered out
            Else
                For colNumber = 1 To .UsedRange.Columns.Count
                    ' Need to check the Columns property of the Worksheet
                    ' to ensure we get the entire column
                    If (.Columns(colNumber).Hidden) Then
                        ' do something with rowIndex.Cells(1, colNumber)
                    Else
                        ' do something else with rowIndex.Cells(1, colNumber)
                    End If
                Next colNumber
            End If
        Next rowIndex
    End With
    
        2
  •  0
  •   Nat Ritmeyer    12 年前

    线坏死时间。我就是这么做的。

    'Count the total number of used rows in the worksheet (Using Column A to count on)
    
    numFilteredCells = Application.WorksheetFunction.Subtotal(3, Range("A1:A" & Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row))
    
    'Find Last filtered row with content
    
    j = Range("A1").Cells(Rows.Count, 1).End(xlUp).Offset(0, 0).Row
    
    'Subtract the total number of filtered rows with content, + 1
    
    jTargetDataRow = j - numFilteredCells + 1
    

    jTargetDataRow 现在包含第一个包含内容的筛选行, j 包含最后一个,以及 numFilteredCells