C#Excel - AutoFilter在第一个空白单元格停止

时间:2015-05-08 12:46:56

标签: c# excel

下午好,

我在电子表格上运行过滤器以删除空白单元格时遇到一些问题。

我正在使用以下代码,该代码应该过滤整个电子表格范围,但它会在第一个空行停止,然后自动过滤器失败。

Range spreadsheetLast = spreadSheetWs.Cells.SpecialCells(Microsoft.Office.Interop.Excel.XlCellType.xlCellTypeLastCell, Type.Missing);
Range spreadsheetRange = spreadSheetWs.get_Range("A1", "T" + spreadsheetLast.Row);
spreadsheetRange.AutoFilter(1, "=", XlAutoFilterOperator.xlAnd, Type.Missing, true);
Range xlFilteredRange = spreadsheetRange.Offset[1, 0].SpecialCells(XlCellType.xlCellTypeVisible, Type.Missing);

然后我使用

删除行
xlFilteredRange.EntireRow.Delete(XlDirection.xlUp);

这基本上删除了电子表格中的所有内容。我已经尝试过使用偏移量并没有运气。

我试图在测试“A1”,“T9999”时将范围更改为手动,这也不起作用。

任何帮助非常感谢,

标记

编辑:将代码更改回原始

1 个答案:

答案 0 :(得分:0)

您可以避免使用AutoFilter并使用.SpecialCells(xlCellTypeBlanks).EntireRow.DeleteRange中查找空白单元格并将其删除。

在VBA(也许是C#?)中你可以使用整个列(A:A)作为Range

以下VBA可以正常删除第一列中包含空白的所有行。我认为这将很容易转换为C#,也可以通过互操作。

Sub DeleteBlanksFromColumnA()

    Range("A:A").SpecialCells(xlCellTypeBlanks).EntireRow.Delete

End Sub
相关问题