如何复制由 AutoFilter() 方法过滤的行

发布于 2024-09-28 22:18:32 字数 647 浏览 0 评论 0原文

到目前为止,我一直在使用 AutoFilter() 方法,没有任何问题。但我无法标记或获取过滤行的范围,以计算/复制/删除它们。

我看过很多关于 VBA 问题的帖子,但没有看到关于 C# 的帖子。

过滤后,我尝试通过以下任何一行来获取范围:

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);

我什至尝试了其他我什至不记得的方法。

标记范围后,我尝试通过执行以下操作来计算行数:

range.Rows.Count

在每种情况下,我都得到了工作表的总行数,有时我得到 65536 (office 2003),有时只有 1,但肯定不是我想要的行数过滤。

我必须将它与带有对象库 11 的 Office 2003 一起使用。

I've been using the AutoFilter() method with no problem so far. but I wasn't able to mark or get the range of the filtered rows, to count/copy/delete them.

I've seen many posts on this issue with VBA, but none for C#.

After filtering, I tried to get the 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 have even tried other ways that I dont even remember.

After marking the range I tried to count the rows 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 just 1 but surely not the number of rows that I filtered.

I must use it with office 2003 with Object Library 11.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

娜些时光,永不杰束 2024-10-05 22:18:32

好的。经过长时间的睡眠。我有一些力量尝试更多的方向,这一次没有从其他论坛和谷歌获得方向。

试试这个:

  • 在整个工作表的使用范围上应用自动过滤器

Excel.Range范围= workSheet.UsedRange;

  • 在范围上应用自动过滤器
 range.AutoFilter(1, criteriaString , Excel.XlAutoFilterOperator.xlAnd, 缺失, true);

(这将根据第 1 列以 criteriaString 作为条件过滤行)

  • 现在您想要获取过滤范围:
Excel.Range FilteredRange = range.SpecialCells(Microsoft.Office.Interop.Excel.XlCellType.xlCellTypeVisible,缺失);
  • 现在,您可以访问filteredRange.Rows.Count整数来计算行数,并做一些有趣的事情。

就是这样!它必须比我想象的简单。

谢谢

Ok. after a long sleep. i got some power to try more directions, this time without getting directions from other forums and google.

Try this:

  • Apply the autoFilter on the used range of the whole workSheet

Excel.Range range = workSheet.UsedRange;

  • Apply the autoFilter on the range
 range.AutoFilter(1, criteriaString , Excel.XlAutoFilterOperator.xlAnd, missing, true);

(this will filter the rows according to column 1 with criteriaString as the criteria)

  • Now you want to get the filtered range:
Excel.Range filteredRange = range.SpecialCells(Microsoft.Office.Interop.Excel.XlCellType.xlCellTypeVisible, missing);
  • Now, you can access filteredRange.Rows.Count integer to count the rows, and do fun stuff.

THATS IT! it had to be simple then i ever thought it would.

Thanks

等待圉鍢 2024-10-05 22:18:32

对于删除行,只需
range.Delete(Microsoft.Office.Interop.Excel.XlDeleteShiftDirection.xlShiftUp);

For deleting the rows , it is just
range.Delete(Microsoft.Office.Interop.Excel.XlDeleteShiftDirection.xlShiftUp);

oО清风挽发oО 2024-10-05 22:18:32

自动过滤后,我想仅在过滤行的“N”范围内命名术语“COGI”。如何编写以仅选取该范围“N”并插入术语“COGI”

after autofilter, i would like to name a term "COGI" only in range "N" in the filtered rows.how to write to just pick up that range "N" and insert a term "COGI"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文