![](/img/trans.png)
[英]Excel VBA Concatenate only visible cells of filtered column. Test code included
[英]vba code to select only visible cells in specific column except heading
我需要代码来仅选择特定列中除标题之外的可见单元格。
这是我尝试过的:
ActiveSheet.UsedRange.Offset(1, 0).SpecialCells(xlCellTypeVisible).Copy
但是上面的代码会选择除标题之外的整个列。 我只需要它选择一个特定的列(使用 D 列作为您的答案)。
这将选择 D 列中包含数据的所有可见单元格,但标题和最后一个包含数据的可见单元格除外:
Option Explicit
Sub SelectVisibleInColD()
Dim lRow As Long
With ActiveSheet
lRow = .Cells(.Rows.Count, 4).End(xlUp).Row
If lRow < 3 Then Exit Sub
.Cells(1, 4).Offset(1, 0).Resize(lRow - 2).SpecialCells(xlCellTypeVisible).Select
End With
End Sub
这是你正在尝试的吗? 这将选择所有可见单元格,直到A 列中除标题外的最后一行:
Sub Sample()
Dim ws As Worksheet
Dim lRow As Long
Set ws = ThisWorkbook.Sheets("Sheet1")
With ws
'~~> I also want to exclude the last cell which is visible and contains data
lRow = .Range("A" & .Rows.Count).End(xlUp).Row - 1
If lRow < 3 Then Exit Sub
Debug.Print Intersect( _
.Range("A2:A" & lRow), _
.Range("A1").Offset(1, 0).SpecialCells(xlCellTypeVisible) _
).Address
End With
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.