34

I have written the following code and continually see pastespecial method of class has failed. I have tried to overcome this issue, but nothing seems to work. I am trying to copy an entire sheet from one workbook, and paste it into another:

Set x = Workbooks.Open(" path to copying book ")
Workbooks.Open(" path to copying book ").Activate
Range("A1").Select
'Cells.Select
Selection.Copy
Set y = Workbooks.Open("path to pasting book")
Workbooks.Open("Path to pasting book").Activate

With y
    Sheets("sheetname").Cells.Select
    Range("A1").PasteSpecial
    'Sheets("sheetname").PasteSpecial
    .Close
End With

With x
    .Close
End With
feetwet
  • 3,248
  • 7
  • 46
  • 84
user2832896
  • 411
  • 2
  • 7
  • 11
  • 3
    Try `.Sheets("sheetname")...` and `.Range("A1").PasteSpecial` if that doesn't work, re-work your code to avoid `Select` method, it is clunky, hard to interpret, resource intensive, and inefficient :) – David Zemens Oct 14 '13 at 00:49

2 Answers2

83

This should do it, let me know if you have trouble with it:

Sub foo()
Dim x As Workbook
Dim y As Workbook

'## Open both workbooks first:
Set x = Workbooks.Open(" path to copying book ")
Set y = Workbooks.Open(" path to destination book ")

'Now, copy what you want from x:
x.Sheets("name of copying sheet").Range("A1").Copy

'Now, paste to y worksheet:
y.Sheets("sheetname").Range("A1").PasteSpecial

'Close x:
x.Close

End Sub

Alternatively, you could just:

Sub foo2()
Dim x As Workbook
Dim y As Workbook

'## Open both workbooks first:
Set x = Workbooks.Open(" path to copying book ")
Set y = Workbooks.Open(" path to destination book ")

'Now, transfer values from x to y:
y.Sheets("sheetname").Range("A1").Value = x.Sheets("name of copying sheet").Range("A1") 

'Close x:
x.Close

End Sub

To extend this to the entire sheet:

With x.Sheets("name of copying sheet").UsedRange
    'Now, paste to y worksheet:
    y.Sheets("sheet name").Range("A1").Resize( _
        .Rows.Count, .Columns.Count) = .Value
End With

And yet another way, store the value as a variable and write the variable to the destination:

Sub foo3()
Dim x As Workbook
Dim y As Workbook
Dim vals as Variant

'## Open both workbooks first:
Set x = Workbooks.Open(" path to copying book ")
Set y = Workbooks.Open(" path to destination book ")

'Store the value in a variable:
vals = x.Sheets("name of sheet").Range("A1").Value

'Use the variable to assign a value to the other file/sheet:
y.Sheets("sheetname").Range("A1").Value = vals 

'Close x:
x.Close

End Sub

The last method above is usually the fastest for most applications, but do note that for very large datasets (100k rows) it's observed that the Clipboard actually outperforms the array dump:

Copy/PasteSpecial vs Range.Value = Range.Value

That said, there are other considerations than just speed, and it may be the case that the performance hit on a large dataset is worth the tradeoff, to avoid interacting with the Clipboard.

David Zemens
  • 53,033
  • 11
  • 81
  • 130
  • David that you very much for your help! Unfortunately I am receiving a compound error saying "Method or data member not found" and it is highlighting the first ".Sheets" line from which it is trying to copy. Do you know what may be the problem? – user2832896 Oct 14 '13 at 01:46
  • The error happens at this: `Sheets("name of copying sheet").Range("A1").Copy` ?? Can you double-check that you've copied it correctly and it doesn't contain any typos? I can't think of any reason you'd get that error (even typos should raise a *different* error message) but try two alternative methods also added to my answer, above. – David Zemens Oct 14 '13 at 02:49
  • I have used the second method you suggested and it works perfectly! thank you very much! – user2832896 Oct 14 '13 at 04:07
  • David, how would I extend this equating of cells to equating entire sheets? – user2832896 Oct 14 '13 at 07:47
  • I added that info, above. – David Zemens Oct 14 '13 at 13:51
  • Set x = Application.Workbooks.Open("xpath") Set y = Application.Workbooks.Open("ypath") With WorkbookCopyingtoo.Sheets("sheet").UsedRange WorkbookCopyingFrom.Sheets("Sheet").Range("A1").Resize(.Rows.Count, Columns.Count) = .Value End With – user2832896 Oct 15 '13 at 00:22
  • This is how I have it David and I am still getting errors saying subscript out of range. Can you see what the problem is? – user2832896 Oct 15 '13 at 00:23
  • As I said before the only thing I can think of that would cause that error on a range assignment is a worksheet name that doesn't exist. You're on your own. Now, since I've answered this particular problem for you, please consider marking the answer as "accepted", if you're having trouble with another error (i.e., your `subscript out of range` error), please post a new question with that problem. – David Zemens Oct 15 '13 at 01:51
  • @DavidZemens how to copy from a workbook to new workbook for specific range? `Set endUserWS = Workbooks.Add Copy Columns 1 by 1 i = 0 For Each col In colNames On Error GoTo colNotFound colN = destWS.Rows(1).Find(col, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False).Column If colN <> -1 Then endUserWS.Sheets(1).Range(Cells(2, i)).Value = destWS.Range(Cells(2, colN), Cells(Cells(Rows.Count, colN).End(xlUp).Row, colN)) End If i = i + 1 Next col ` is it fine? I get blank cells in new workbook. Values dont get copied. – MalTec Mar 12 '14 at 19:22
  • the code you suggested works but i get a popup saying" there is a large amount of information on the clipboard,..." how do i avoid getting this popup – Brian Apr 17 '16 at 13:47
  • Use `Application.DisplayAlerts=False` before closing the file (and remember to reset it to True after closing the file) – David Zemens Apr 17 '16 at 14:04
6

You copied using Cells.
If so, no need to PasteSpecial since you are copying data at exactly the same format.
Here's your code with some fixes.

Dim x As Workbook, y As Workbook
Dim ws1 As Worksheet, ws2 As Worksheet

Set x = Workbooks.Open("path to copying book")
Set y = Workbooks.Open("path to pasting book")

Set ws1 = x.Sheets("Sheet you want to copy from")
Set ws2 = y.Sheets("Sheet you want to copy to")

ws1.Cells.Copy ws2.cells
y.Close True
x.Close False

If however you really want to paste special, use a dynamic Range("Address") to copy from.
Like this:

ws1.Range("Address").Copy: ws2.Range("A1").PasteSpecial xlPasteValues
y.Close True
x.Close False

Take note of the : colon after the .Copy which is a Statement Separating character.
Using Object.PasteSpecial requires to be executed in a new line.
Hope this gets you going.

L42
  • 19,427
  • 11
  • 44
  • 68