Copy from one workbook and paste into another

后端 未结 2 1013
借酒劲吻你
借酒劲吻你 2020-11-22 08:56

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

2条回答
  •  感情败类
    2020-11-22 09:25

    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.

提交回复
热议问题