[英]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.Offset
與SpecialCells(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.