excel vba宏:将列信息复制到另一个工作簿

时间:2017-06-15 22:57:21

标签: excel vba excel-vba

我正在使用此宏来复制工作簿中的3列数据' x'到工作簿' y'而不是复制隐藏的行。

Sub GetDataDemo()
Const FileName As String = "EHS.xlsx"
Const SheetName As String = "PO"
FilePath = "C:\Users\DD\Desktop\"
Dim wb As Workbook
Dim this As Worksheet
Dim i As Long, ii As Long

Application.ScreenUpdating = False

If IsEmpty(Dir(FilePath & FileName)) Then

    MsgBox "The file " & FileName & " was not found", , "File Doesn't Exist"
Else

    Set this = ActiveSheet

    Set wb = Workbooks.Open(FilePath & FileName)

    With wb.Worksheets(SheetName).Range("Y:AA")

        ii = 3
        For i = 3 To 500

            If Not .Rows(i).Hidden Then

                .Cells(i).Copy
                this.Range("P:R").Cells(ii).Paste
                ii = ii + 1
            End If
        Next i
    End With
End If

ActiveWindow.ScreenUpdating = True
End Sub

我一直在线附近收到自动化错误(错误440):

this.Range("P:R").Cells(ii).Paste

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

你应该能够复制&批量粘贴可见的单元格/行。

With wb.Worksheets(SheetName).Range("Y3:AA500")
    on error resume next
    .SpecialCells(xlcelltypevisible).Copy this.Range("P3")
    on error goto 0
End With

.Paste是Worksheet的成员,而不是Range或Cells。

this是保留名称;将保留名称重用为变量并不被视为“最佳实践”。