+ Reply to Thread
Results 1 to 4 of 4

Pasting into another workbook

Hybrid View

  1. #1
    Registered User
    Join Date
    06-19-2012
    Location
    Michigan
    MS-Off Ver
    Excel 2016
    Posts
    97

    Pasting into another workbook

    Hey Guys,

    I'm trying to write a macro that copies values from one sheet and pastes them into Sheet1 in a different workbook.

    I have the code below, but it errors out at the pasting step ( Range("A1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone )

    The error is:

    "Run-time error '1004';

    PasteSpecial method of Range Class failed"

    Anyone have any idea what's going on?

    Thanks!

    Sub InventorySave()
    
    Dim wbk As Workbook
    
    Sheet4.Range("A1:G1000").Copy
    
    Set wbk = Workbooks.Open("C:\Users\Matt.Matt-Laptop\Desktop\InventoryTemplate.xlsm")
    
    With wbk.Sheets("Sheet1")
    
    Range("A1:G1000").ClearContents
    
    Range("A1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone
    
    Application.DisplayAlerts = False
    
        ActiveWorkbook.SaveAs Filename:="C:\Users\Matt.Matt-Laptop\Desktop\InventoryTemplate.xlsm", FileFormat:=52
    
        ActiveWorkbook.SaveAs Filename:="C:\Users\Matt.Matt-Laptop\Desktop\Inventory Upload.txt", FileFormat:=xlTextWindows
    
    Application.DisplayAlerts = True
    
    ActiveWorkbook.Close False
    
    End With
    
    End Sub

  2. #2
    Forum Expert Vikas_Gautam's Avatar
    Join Date
    06-04-2013
    Location
    Ludhiana,Punjab, India
    MS-Off Ver
    Excel 2013
    Posts
    1,850

    Re: Pasting into another workbook

    The Problem is that you used clearcontents method before pastespecial method..
    The possible correction would be....

    Sub InventorySave()
    
    OrgFile=Activeworkbook.name
    
    Dim wbk As Workbook
    
    Set wbk = Workbooks.Open("C:\Users\Matt.Matt-Laptop\Desktop\InventoryTemplate.xlsm")
    
    With wbk.Sheets("Sheet1")
    
    Range("A1:G1000").ClearContents
    
    Workbooks(orgfile).sheets("sheet4").range("A1:G1000").copy
    Range("A1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone
    
    Application.DisplayAlerts = False
    
        ActiveWorkbook.SaveAs Filename:="C:\Users\Matt.Matt-Laptop\Desktop\InventoryTemplate.xlsm", FileFormat:=52
    
        ActiveWorkbook.SaveAs Filename:="C:\Users\Matt.Matt-Laptop\Desktop\Inventory Upload.txt", FileFormat:=xlTextWindows
    
    Application.DisplayAlerts = True
    
    ActiveWorkbook.Close False
    
    End With
    
    End Sub
    Last edited by Vikas_Gautam; 10-22-2014 at 12:02 AM.
    Regards,
    Vikas Gautam
    Excel-buzz.blogspot.com

    Excel is not a matter of Experience, its a matter of Application.

    Say Thanks, Click * Add Reputation

  3. #3
    Registered User
    Join Date
    06-19-2012
    Location
    Michigan
    MS-Off Ver
    Excel 2016
    Posts
    97

    Re: Pasting into another workbook

    hmm now I'm getting,

    "Run-time error '9':

    Subscript out of range"

    With debugging telling me that error is at,

    Workbooks(OrgFile).Sheets("sheet4").Range("A1:G1000").Copy

  4. #4
    Forum Expert Vikas_Gautam's Avatar
    Join Date
    06-04-2013
    Location
    Ludhiana,Punjab, India
    MS-Off Ver
    Excel 2013
    Posts
    1,850

    Re: Pasting into another workbook

    Try this then..
    Or Upload both files to work upon...

    Sub InventorySave()
    OrgFile=Activeworkbook.name
    Dim wbk As Workbook
    Set wbk = Workbooks.Open("C:\Users\Matt.Matt-Laptop\Desktop\InventoryTemplate.xlsm")
    With wbk.Sheets("Sheet1")
    Range("A1:G1000").ClearContents
    Workbooks(orgfile).activate
    sheets("sheet4").range("A1:G1000").copy
    Workbooks(wbk.name).activate
    Range("A1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone
    Application.DisplayAlerts = False
        ActiveWorkbook.SaveAs Filename:="C:\Users\Matt.Matt-Laptop\Desktop\InventoryTemplate.xlsm", FileFormat:=52
        ActiveWorkbook.SaveAs Filename:="C:\Users\Matt.Matt-Laptop\Desktop\Inventory Upload.txt", FileFormat:=xlTextWindows
    Application.DisplayAlerts = True
    ActiveWorkbook.Close False
    End With
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. macro to find matching value in workbook and pasting it to another workbook
    By aman2059 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-19-2014, 03:41 PM
  2. [SOLVED] Opening new workbook and pasting values into current workbook - code issues
    By Stew1234 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-15-2014, 07:56 AM
  3. Filtering in another workbook, copying filtered data and pasting in source workbook
    By saadtariq in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-13-2012, 12:55 PM
  4. [SOLVED] Macro Wanted for looping thru worksheets in one workbook and pasting to another workbook!!
    By eduardito in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 07-03-2012, 07:43 AM
  5. Replies: 0
    Last Post: 08-11-2011, 01:23 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1