如何获得所有过滤的数据行在Excel中(与隐藏列)?

我的数据表有filter和隐藏的列。 当filter适用时,我需要遍历所有过滤的数据。 我使用:Excel.Range visibleRange = this.UsedRange.SpecialCells(XlCellType.xlCellTypeVisible,missing)as Excel.Range;

现在visibleRange.Count是0; 使用foreach循环“foreach(Excel.Range行在visibleRange.Row)”行不具有所有的列,从第一个隐藏的列切碎。

我们怎么能通过过滤行循环?

我根本不会使用SpecialCells属性。 只需遍历UsedRange每一行,并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 

每一行(或者说每一个由rowIndex引用的Range对象)将包含所有的列,包括隐藏的列。 如果您需要确定列是否隐藏,那么只需检查Hidden属性,但请记住,这仅适用于整列或行:

 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 

线程死灵时间。 这是我所做的。

 '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包含具有内容的过滤行的总数。