greenhillchris
New Member
- Joined
- Mar 5, 2022
- Messages
- 18
- Office Version
- 365
Hi there
I have the following code that will open 2 workbooks and copy and paste data from one workbook to the other and then save and close the workbooks.
This works great but I am looking to copy and paste based on the last row rather than defining ranges each time, so I have amended the above code to find the last row for each workbook but the problem now is that it doesn’t actually copy and paste anything it just opens and closes the two workbooks.
I was wondering if anyone could advise me on where I am going wrong with this code and why it would not be copying and pasting.
Thanks for any help
I have the following code that will open 2 workbooks and copy and paste data from one workbook to the other and then save and close the workbooks.
VBA Code:
Sub OpenCopy1()
Workbooks.Open "C:\Users\cgreenhill\Desktop\Store Manager Individual Consultation Tracker - Master.xlsx"
Workbooks.Open "C:\Users\cgreenhill\Desktop\CW Region\Store Manager Tracker - 442 Staines.xlsx"
Workbooks("Store Manager Tracker - 442 Staines").Worksheets("Tracker").Range("B6:CM20").Copy _
Workbooks("Store Manager Individual Consultation Tracker - Master").Worksheets("Tracker").Range("B6")
Workbooks("Store Manager Individual Consultation Tracker - Master").Close SaveChanges:=True
Workbooks("Store Manager Tracker - 442 Staines").Close SaveChanges:=True
End Sub
This works great but I am looking to copy and paste based on the last row rather than defining ranges each time, so I have amended the above code to find the last row for each workbook but the problem now is that it doesn’t actually copy and paste anything it just opens and closes the two workbooks.
VBA Code:
Sub OpenCopy2()
Dim LR1 As Long
Dim LR2 As Long
Workbooks.Open "C:\Users\cgreenhill\Desktop\Store Manager Individual Consultation Tracker - Master.xlsx"
Workbooks.Open "C:\Users\cgreenhill\Desktop\CW Region\Store Manager Tracker - 442 Staines.xlsx"
LR1 = Workbooks("Store Manager Individual Consultation Tracker - Master").Worksheets("Tracker").Range("B" & Rows.Count).End(xlUp).Row + 1
LR2 = Workbooks("Store Manager Tracker - 442 Staines").Sheets("Tracker").Range("B" & Rows.Count).End(xlUp).Row
Workbooks("Store Manager Tracker - 442 Staines").Sheets("Tracker").Range("B6:CM" & LR2).Copy _
Workbooks("Store Manager Individual Consultation Tracker - Master").Worksheets("Tracker").Range("B" & LR1)
Workbooks("Store Manager Individual Consultation Tracker - Master").Close SaveChanges:=True
Workbooks("Store Manager Tracker - 442 Staines").Close SaveChanges:=True
End Sub
I was wondering if anyone could advise me on where I am going wrong with this code and why it would not be copying and pasting.
Thanks for any help