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

我正在运行一个macros,它要求一个工作表名称和一个引用单元格,然后select一个围绕我们select的单元格的单元格区域。 在对我的数据应用一个filter之后,一些行将变得隐藏,因为它们不是必需的。 问题是,macros不考虑这一点,并计算隐藏行。 这里是我在macros的原始版本中使用的代码:

…..应用一些InputBox并search用户的值后,将执行以下行:

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

这样隐藏的行就被包含在select中。 我尝试了下面的修改

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

但是没有成功。

我想知道,任何人都可以提出一种方法来使用ActiveCell.OffsetSpecialCells(xlCellTypeVisible)导致上述macros的function – 名称来select一个范围的单元格,避免隐藏的行过滤后?

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

 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