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

Patriot2879

Well-known Member
Joined
Feb 1, 2018
Messages
1,227
Office Version
  1. 2010
Platform
  1. Windows
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:
[FONT=Times New Roman][SIZE=3][COLOR=#000000][/COLOR][/SIZE][/FONT][FONT=Calibri][SIZE=3][COLOR=#000000]Private Sub CommandButton1_Click()[/COLOR][/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3][COLOR=#000000][/COLOR][/SIZE][/FONT][FONT=Calibri][SIZE=3][COLOR=#000000]Workbooks("C:\Users\s21375\Desktop\test2\Book17.xlsx").Sheets("Sheet1").Range("A1:E2").Copy[/COLOR][/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3][COLOR=#000000][/COLOR][/SIZE][/FONT][FONT=Calibri][SIZE=3][COLOR=#000000]Workbooks("C:\Users\s21375\Desktop\test\Book16.xlsx").Sheets("Sheet1").Range("A1:E2").Paste[/COLOR][/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3][COLOR=#000000][/COLOR][/SIZE][/FONT][FONT=Calibri][SIZE=3][COLOR=#000000]Workbooks("C:\Users\s21375\Desktop\test\Book18.xlsx").Sheets("Sheet1").Range("A1:E2").Paste[/COLOR][/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3][COLOR=#000000][/COLOR][/SIZE][/FONT][FONT=Calibri][SIZE=3][COLOR=#000000] [/COLOR][/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3][COLOR=#000000][/COLOR][/SIZE][/FONT][FONT=Calibri][SIZE=3][COLOR=#000000]End Sub[/COLOR][/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3][COLOR=#000000][/CO[/COLOR][/SIZE][/FONT]DE]
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
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:
Upvote 0
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
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0
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:
Upvote 0
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:
Upvote 0
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:
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,213,522
Messages
6,114,112
Members
448,549
Latest member
brianhfield

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top