简体   繁体   中英

excel vba macro: copy column info to another workbook

I am using this macro to copy data from 3 columns from workbook 'x' to workbook 'y' while not copying the hidden rows.

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

I keep getting the Automation error (Error 440) near the line:

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

Thanks for your help in advance!

You should be able to copy & paste the visible cells/rows in bulk.

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

.Paste is a member of Worksheet, not of Range or Cells.

this is a reserved name; it is not considered 'best practice' to reuse reserved names as vars.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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