Cartolo
Cartolo

Reputation: 13

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!

Upvotes: 0

Views: 183

Answers (1)

user4039065
user4039065

Reputation:

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.

Upvotes: 2

Related Questions