How to copy rows filtered by AutoFilter ()

I am using the AutoFilter () method without any problems. but I could not mark or get a range of filtered rows, count / copy / delete them.

I saw a lot of posts about this issue with VBA, but none of them are for C #.

After filtering, I tried to get a range by any of these lines:

range = ws.UsedRange.CurrentRegion.SpecialCells(Excel.XlCellType.xlCellTypeVisible, missing);
range = ws.AutoFilter.Range.SpecialCells(Excel.XlCellType.xlCellTypeVisible, missing);
range = ws.Cells.SpecialCells(Excel.XlCellType.xlCellTypeVisible, missing);

And I even tried other ways that I don’t even remember.

After marking the range, I tried to count the lines by doing:

range.Rows.Count

and in each case I got the total number of lines of the sheet, sometimes I got 65536 (office 2003), and sometimes only 1, but, of course, not the number of lines that I filtered.

I have to use it with Office 2003 with an object library.

+3
3

Ok. . , , google.

:

Excel.Range range = workSheet.UsedRange;

 range.AutoFilter(1, criteriaString , Excel.XlAutoFilterOperator.xlAnd, missing, true);

( 1 String )

  • :
Excel.Range filteredRange = range.SpecialCells(Microsoft.Office.Interop.Excel.XlCellType.xlCellTypeVisible, missing);
  • filteredRange.Rows.Count integer, .

! , , .

+1

, "COGI" "N" . , "N" "COGI"

0

To delete lines, it is simple   range.Delete(Microsoft.Office.Interop.Excel.XlDeleteShiftDirection.xlShiftUp);

0
source

Source: https://habr.com/ru/post/1771098/


All Articles