繁体   English   中英

Excel VBA宏:将列信息复制到另一个工作簿

[英]excel vba macro: copy column info to another workbook

我正在使用此宏将数据从工作簿“ x”的3列复制到工作簿“ 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

谢谢您的帮助!

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

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

.Paste是工作表的成员,而不是范围或单元的成员。

this是保留名称; 最好不要将保留名称重用为var。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM