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

Patriot2879

Well-known Member
Joined
Feb 1, 2018
Messages
1,021
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]
 

JackDanIce

Well-known Member
Joined
Feb 3, 2010
Messages
9,409
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:

Patriot2879

Well-known Member
Joined
Feb 1, 2018
Messages
1,021
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
 

Patriot2879

Well-known Member
Joined
Feb 1, 2018
Messages
1,021
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
 

Patriot2879

Well-known Member
Joined
Feb 1, 2018
Messages
1,021
Hope you can help please? thank you for your time
 

Patriot2879

Well-known Member
Joined
Feb 1, 2018
Messages
1,021
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
 

Patriot2879

Well-known Member
Joined
Feb 1, 2018
Messages
1,021
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:

Patriot2879

Well-known Member
Joined
Feb 1, 2018
Messages
1,021
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:

JackDanIce

Well-known Member
Joined
Feb 3, 2010
Messages
9,409
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:

Patriot2879

Well-known Member
Joined
Feb 1, 2018
Messages
1,021
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
 

Forum statistics

Threads
1,085,841
Messages
5,386,278
Members
401,991
Latest member
jimmywhitetrix

Some videos you may like

This Week's Hot Topics

Top