Page 1 of 2 12 LastLast
Results 1 to 10 of 17

Thread: Copy/Paste from one workbook to another 2 workbooks with range
Thanks Thanks: 0 Likes Likes: 0

  1. #1
    Board Regular
    Join Date
    Feb 2018
    Location
    Midlands, UK
    Posts
    889
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Default Copy/Paste from one workbook to another 2 workbooks with range

    Hi please can you help me I have a command button in Book17 where once clicked I want to copy range A1:E2 and paste into 2 other workbooks which are Book16 and Book18, I have the code below but it doesn't seem to work and the lines come up in red, please can you help me?
    [CODE]Private Sub CommandButton1_Click()
    Workbooks("C:\Users\s21375\Desktop\test2\Book17.xlsx").Sheets("Sheet1").Range("A1:E2").Copy
    Workbooks("C:\Users\s21375\Desktop\test\Book16.xlsx").Sheets("Sheet1").Range("A1:E2").Paste
    Workbooks("C:\Users\s21375\Desktop\test\Book18.xlsx").Sheets("Sheet1").Range("A1:E2").Paste

    End Sub
    [/CODE]

  2. #2
    Board Regular
    Join Date
    Feb 2010
    Location
    London, UK
    Posts
    9,287
    Post Thanks / Like
    Mentioned
    24 Post(s)
    Tagged
    3 Thread(s)

    Default Re: Copy/Paste from one workbook to another 2 workbooks with range

    Is Book17 the active workbook?
    Is Book16 and Book18 both open when you try to run the macro?

    If yes to both, then try:
    Code:
    Private Sub CommandButton1_Click()
        
        With ThisWorkbook.Sheets("Sheet1").Range("A1:E2")
            Workbooks("Book16.xlsx").Sheets("Sheet1").Range("A1:E2").Value = .Value
            Workbooks("Book18.xlsx").Sheets("Sheet1").Range("A1:E2").Value = .Value
        End With
        
    End Sub
    Last edited by JackDanIce; Sep 5th, 2019 at 05:35 AM.


  3. #3
    Board Regular
    Join Date
    Feb 2018
    Location
    Midlands, UK
    Posts
    889
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Copy/Paste from one workbook to another 2 workbooks with range

    Hi thank you for the code. I get a subscript out of range on this for some reason on the line below. Do not need to put the location of the workboos in the code? and what folder hey are in?

    Book17 is the active one where I want to copy the data from, book16 and book18 will be located in different folders/locations, and both these books can be either open or closed when data is pasted to these.

    Code:
    Workbooks("Book16.xlsx").Sheets("Sheet1").Range("A1:E2").Value = .Value

  4. #4
    Board Regular
    Join Date
    Feb 2018
    Location
    Midlands, UK
    Posts
    889
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Copy/Paste from one workbook to another 2 workbooks with range

    Hi I have tried changing it to the following with the paths but still no avail.
    Code:
    Private Sub CommandButton1_Click()
        
        With ThisWorkbook.Sheets("Sheet1").Range("A1:E2")
            Workbooks("C:\Users\s21375\Desktop\test\Book16.xlsx").Sheets("Sheet1").Range("A1:E2").Value = .Value
            Workbooks("C:\Users\s21375\Desktop\test3\Book18.xlsx").Sheets("Sheet1").Range("A1:E2").Value = .Value
        End With
        
    End Sub

  5. #5
    Board Regular
    Join Date
    Feb 2018
    Location
    Midlands, UK
    Posts
    889
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Copy/Paste from one workbook to another 2 workbooks with range

    Hope you can help please? thank you for your time

  6. #6
    Board Regular
    Join Date
    Feb 2018
    Location
    Midlands, UK
    Posts
    889
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Copy/Paste from one workbook to another 2 workbooks with range

    HI I have tried the code below and still no avail. hope you can help
    Code:
    Private Sub CommandButton1_Click()
        
        With ThisWorkbook.Sheets("Sheet1").Range("A1:E2").Copy
            Workbooks("C:\Users\s21375\Desktop\test\Book16.xlsx").Sheets("Sheet1").Range("A1:E2").Value = .Paste
            Workbooks("C:\Users\s21375\Desktop\test3\Book18.xlsx").Sheets("Sheet1").Range("A1:E2").Value = .Paste
        End With
        
    End Sub

  7. #7
    Board Regular
    Join Date
    Feb 2018
    Location
    Midlands, UK
    Posts
    889
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Copy/Paste from one workbook to another 2 workbooks with range

    Hi again, Book17 (target) is the workbook that I need to copy from and book16 and book18 are the workbooks I need to paste into. hope this helps and hope you can help me, the code will need to work as well if the workbooks are either closed or open. many thanks for your assistance.
    Last edited by Patriot2879; Sep 5th, 2019 at 06:58 AM.

  8. #8
    Board Regular
    Join Date
    Feb 2018
    Location
    Midlands, UK
    Posts
    889
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Copy/Paste from one workbook to another 2 workbooks with range

    Hi I have managed to get it working sort off with the code for some reason it only pastes into book18 and not book16 below and how do I stop workbook 16 and 18 from opening?
    Code:
    Private Sub CommandButton1_Click()
     
        Dim srcWB As Workbook
        Dim destWB As Workbook
        Dim fName As String
        Dim lastRow As Long
     
    '   Capture current workbook as source workbook
        Set srcWB = ActiveWorkbook
     
    '   Open destination workbook and capture it as destination workbook
        Workbooks.Open "C:\Users\s21375\Desktop\test\Book16.xlsx"
        Workbooks.Open "C:\Users\s21375\Desktop\test3\Book18.xlsx"
        Set destWB = ActiveWorkbook
      
      
    '   Copy data from source workbook to destination workbook
    srcWB.Sheets("Sheet1").Range("A1:E2").Copy
        destWB.Sheets("Sheet1").Range("A1").PasteSpecial xlPasteValues
    '   Save changes and close destination workbook
        destWB.Close SaveChanges:=True
    End Sub
    Last edited by Patriot2879; Sep 5th, 2019 at 07:19 AM.

  9. #9
    Board Regular
    Join Date
    Feb 2010
    Location
    London, UK
    Posts
    9,287
    Post Thanks / Like
    Mentioned
    24 Post(s)
    Tagged
    3 Thread(s)

    Default Re: Copy/Paste from one workbook to another 2 workbooks with range

    You can't update a closed workbook, you have to open it, paste the values, then close it.

    Replace all of your code with below and try:
    Code:
    Private Sub CommandButton1_Click()
                
        Dim r As Range: Set r = ThisWorkbook.Sheets("Sheet1").Range("A1:E2")
        
        UpdateWorkbook "C:\Users\s21375\Desktop\test\Book16.xlsx", r
        UpdateWorkbook "C:\Users\s21375\Desktop\test3\Book18.xlsx", r
        
        Set r = Nothing
        
    End Sub
    
    
    Private Sub UpdateWorkbook(ByRef sBook As String, ByRef r As Range)
    
        With Workbooks.Open(sBook)
            .Sheets("Sheet1").Cells(1, 1).Resize(r.Rows, r.Columns).Value = r.Value
        End With
        
        ActiveWorkbook.Close True
        
    End Sub
    Last edited by JackDanIce; Sep 5th, 2019 at 07:19 AM.


  10. #10
    Board Regular
    Join Date
    Feb 2018
    Location
    Midlands, UK
    Posts
    889
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Copy/Paste from one workbook to another 2 workbooks with range

    hi thank you for the code I have just tried but get an error on the below line- 'application defined or an application defined error'.
    Code:
    .Sheets("Sheet1").Cells(1, 1).Resize(r.Rows, r.Columns).Value = r.Value

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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