简体   繁体   中英

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

I am running a macro, that asks for a sheet name and a reference cell, and then selects a range of cells, surrounding the cell of our choice. After applying a filter to my data, some of the rows become hidden, as they are not needed. The problem is, that the macro does not take that into consideration and counts the hidden rows too. Here is the code, that I use in the original version of the macro:

.....after applying some InputBox and a search for the user's value, the following row is executed:

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

In this way hidden rows are included in the selection. I tried the following modification

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

However without success.

I was wondering, can anyone suggest a way to use ActiveCell.Offset in combination with SpecialCells(xlCellTypeVisible) resulting in the above described functionality of the macro -namely to select a range of cells avoiding the hidden rows after filtering?

To select just the visible cells from a range of selected cells, you can use the following line of code:

 Selection.SpecialCells(xlCellTypeVisible).Select 

Like in this Example:

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

The Code Below is a example on how you could count the Rows. So but there is a Problem where you have to think about.

If You Paste the Selection to your Original Sheet, there is a Chance that there are Hidden Rows in the area where you Copied the Selection. If this is so, The Text which you copied there will be Hidden too.

So you have to Copied the Data to a new Sheet to avoid that Problem or you have to Copied the Data at the bottom of the Sheet 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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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