![](/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.