繁体   English   中英

使用ActiveCell.Offset()选择一个范围,避免隐藏的单元格

[英]Select a range, avoiding hidden cells, using ActiveCell.Offset()

我正在运行一个宏,该宏要求一个工作表名称和一个参考单元格,然后选择一个单元格范围,围绕我们选择的单元格。 在对我的数据应用过滤器后,某些行被隐藏了,因为不需要它们。 问题是,宏没有考虑到这一点,并且也计算了隐藏的行。 这是我在宏的原始版本中使用的代码:

.....在应用一些InputBox并搜索用户的值之后,将执行以下行:

Range(ActiveCell.Offset(90, 0), ActiveCell.Offset(-252, 2)).Select
Selection.Copy

这样,隐藏行将包含在选择中。 我尝试了以下修改

Range(ActiveCell.Offset(90, 0), ActiveCell.Offset(-252, 2)).SpecialCells(xlCellTypeVisible).Select
Selection.Copy

但是没有成功。

我想知道,有人能建议一种将ActiveCell.OffsetSpecialCells(xlCellTypeVisible)结合使用的方法,从而导致上述宏功能-即选择一定范围的单元格以避免过滤后的隐藏行吗?

要从一系列选定的单元格中仅选择可见的单元格,可以使用以下代码行:

 Selection.SpecialCells(xlCellTypeVisible).Select 

如本例所示:

 Range(ActiveCell.Offset(90, 0), ActiveCell.Offset(-252, 2)).Select Selection.SpecialCells(xlCellTypeVisible).Select Selection.Copy 

下面的代码是有关如何计算行数的示例。 因此,有一个必须要考虑的问题。

如果将所选内容粘贴到原始工作表中,则可能会在复制所选内容的区域中存在隐藏行。 如果是这样,您复制的文本也将被隐藏。

因此,您必须将数据复制到新的工作表中以避免出现该问题,或者必须将数据复制到工作表1的底部。

 Option Explicit 'Define a Constant for the Amount of Rows you Need Private Const ConstAmountRows As Integer = 40 Sub Test() Dim intCountedRows As Integer Dim idx As Integer Dim intDifference As Integer idx = 0 Do If Not (intCountedRows = ConstAmountRows) Then intCountedRows = ConstAmountRows idx = idx + 1 End If Sheets("Sheet1").Select 'Select the Range with the Amount of Rows you need ideally Range("A1:A" & intCountedRows + idx).Select 'Select only the Visible Cells Selection.SpecialCells(xlCellTypeVisible).Select Selection.Copy Sheets("Sheet2").Select 'Select another Sheet '***-> Her you can select the Place you want to Paste the Text<-*** Range("B1").Select ActiveSheet.Paste '*** Count the Rows that you Paste intCountedRows = Selection.Rows.Count 'if the Counted Rows are not equal to the Amount. Repeat Loop While Not (intCountedRows >= ConstAmountRows) End Sub 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM