简体   繁体   中英

How do I copy between two workbooks in Excel?

I'm trying to copy some information from one workbook to another in Excel. I hate this language, I can't figure out when to use activate when not to, etc...

Here is my code that is giving me a run-time error 1004. I would like to get the copy and paste to work without switching back and forth between sheets. My reference for the code is here .

Here is my actual code:

Option Base 1

Sub populate_acc_template()
'
' populate_acc_template Macro
' Customer:  ACC.  This template populates the spreadsheet with data from an HRIS sheet.
'

' Start by defining the book to be pulled from and getting the first and last rows
' of that book.

    Dim template_book As Workbook
    Set template_book = ThisWorkbook
    Dim pull_book As Workbook
    Set pull_book = Workbooks.Open(Application.ActiveWorkbook.Path & "\books\sample_book.xlsx")

    With ActiveSheet
        FirstRow = 2
        LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    End With

    insert_length = LastRow - FirstRow


' Now insert the number of rows that we need in the template.

    template_book.Sheets("Promotion Calculations").Rows(9 & ":" & 9 + insert_length).Insert Shift:=xlDown


' Copy and paste the information from the bulk data.

    Dim paste_array(1 To 9) As String

    paste_array(1) = 5
    paste_array(2) = 6
    paste_array(3) = 4
    paste_array(4) = 9
    paste_array(5) = 10
    paste_array(6) = 3
    paste_array(7) = 2
    paste_array(8) = 7
    paste_array(9) = 8

    For i = 1 To UBound(paste_array)

        ' Copy the entire column containing text.
         template_book.Sheets("Promotion Calculations").Range(Cells(8, paste_array(i)), Cells(8 + insert_length, paste_array(i))).Value = pull_book.Sheets("Data Sheet").Range(Cells(FirstRow, i), Cells(LastRow, i))
    Next i
End Sub

One problem is this statement (there may be others, but this is obviously a problem) as it appears you have unqualified Cells within Range assignments.

template_book.Sheets("Promotion Calculations").Range(Cells(8, paste_array(i)), Cells(8 + insert_length, paste_array(i))).Value = pull_book.Sheets("Data Sheet").Range(Cells(FirstRow, i), Cells(LastRow, i))
Next i

When not qualified, Cells always implicitly refers to ActiveSheet.Cells , so, the problem is that you are essentially doing:

Sheet1.Range(Sheet1.Cells(1,1), Sheet1.Cells(1,2)).Value = Sheet2.Range(Sheet1.Cells(1,1), Sheet1.Cells(1,2)).Value

Since the Cells on Sheet1 cannot produce a Range on Sheet2, the 1004 error raises.

I find it usually easier, and cleaner to read/interpret, if you explicitly define the ranges for copy/paste:

Dim pasteRange as Range
Dim copyRange as Range

For i = 1 To UBound(paste_array)
    With template_book.Sheets("Promotion Calculations")
        Set pasteRange = .Cells(8, paste_array(i)).Resize(insert_length, 1)
    End WIth

    With pull_book.Sheets("Data Sheet")
        Set copyRange = .Range(.Cells(FirstRow, i), .Cells(LastRow, i))
    End With

    pasteRange.Value = copyRange.Value

Next

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