在可见单元格中复制和粘贴数据

时间:2018-02-22 09:49:02

标签: excel vba excel-vba

我对VBA很陌生,我想从列L到S的标题列下面剪切可用数据,并将其粘贴到同一张表中的列D到K中。请注意,它是一个过滤范围,数据应仅从可见单元格中剪切和粘贴。另请注意,不应包含标题列。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:2)

以下内容如何:

Sub foo()
Dim ws As Worksheet: Set ws = Sheets("Sheet1")
'declare and set your worksheet, amend as required
LastRow = ws.Cells(ws.Rows.Count, "L").End(xlUp).Row
'get the last row with data on Column L
ws.Range("L2:S" & LastRow).SpecialCells(xlCellTypeVisible).Copy
'copy visible cells
ws.Range("D1").PasteSpecial xlPasteAll
'paste into D1
End Sub

<强>更新

根据您最近的评论,我相信以下内容会符合您的期望:

Sub foo()
Dim LastRow As Long
Dim Rng As Range, cell As Range
Dim ws As Worksheet: Set ws = Sheets("Sheet1")
'declare and set your worksheet, amend as required
LastRow = ws.Cells(ws.Rows.Count, "L").End(xlUp).Row
'get the last row with data on Column L
Set Rng = ws.Range("L2:S" & LastRow).SpecialCells(xlCellTypeVisible)
For Each cell In Rng
    cell.Copy ws.Range(cell.Address).Offset(0, -8)
Next
End Sub