Automation Error Copy Pasting from two worksbooks into third one

Dokat

Active Member
Joined
Jan 19, 2015
Messages
304
Office Version
  1. 365
Hi,

I have below code where i copy paste from two workbooks into third one. First oart of the code works fine however after adding the second part it returns run time error -2147221080. I believe its because of opening second workbook but not sure. Anyone has experience with this priblem and have a solution?


1647923838043.png


VBA Code:
Private Sub WorkbookOpen()

Application.ScreenUpdating = False

Dim Lrs As Long
Lrs = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
Range("A7346:F" & Lrs).ClearContents

Workbooks.Open ("C:\Users\dokat\OneDrive - BIC\BIC\Power BI\Trade Report\Montly Data\2017-2022 6 Year Trended PnL by L3 - YTD February.xlsm")

Dim Lr  As Long
Dim ws As Worksheet
Set ws = Workbooks("2017-2022 6 Year Trended PnL by L3 - YTD February.xlsm").Worksheets("Summary")
Lr = ws.Cells(Rows.Count, 1).End(xlUp).Row

ws.Range("A1:F" & Lr).Copy

Workbooks("Sta P&L Test.xlsm").Sheets("Sta P&L").Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues

Application.CutCopyMode = False

Workbooks("2017-2022 6 Year Trended PnL by L3 - YTD February.xlsm").Close



Workbooks.Open ("C:\Users\dokat\OneDrive - BIC\BIC\Power BI\Trade Report\Montly Data\2017-2022 6 Year Trended PnL by L3 - February")

Dim Lrm  As Long
Dim wsm As Worksheet
Set wsm = Workbooks("2017-2022 6 Year Trended PnL by L3 - February.xlsm").Worksheets("Summary")
Lrm = wsm.Cells(Rows.Count, 1).End(xlUp).Row

ws.Range("A2:F" & Lrm).Copy

Workbooks("Sta P&L Test.xlsm").Sheets("Sta P&L").Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues

Application.CutCopyMode = False

Workbooks("2017-2022 6 Year Trended PnL by L3 - February").Close




Application.ScreenUpdating = True

End Sub
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Hi,

I have below code where i copy paste from two workbooks into third one. First oart of the code works fine however after adding the second part it returns run time error -2147221080. I believe its because of opening second workbook but not sure. Anyone has experience with this priblem and have a solution?


View attachment 60650

VBA Code:
Private Sub WorkbookOpen()

Application.ScreenUpdating = False

Dim Lrs As Long
Lrs = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
Range("A7346:F" & Lrs).ClearContents

Workbooks.Open ("C:\Users\dokat\OneDrive - BIC\BIC\Power BI\Trade Report\Montly Data\2017-2022 6 Year Trended PnL by L3 - YTD February.xlsm")

Dim Lr  As Long
Dim ws As Worksheet
Set ws = Workbooks("2017-2022 6 Year Trended PnL by L3 - YTD February.xlsm").Worksheets("Summary")
Lr = ws.Cells(Rows.Count, 1).End(xlUp).Row

ws.Range("A1:F" & Lr).Copy

Workbooks("Sta P&L Test.xlsm").Sheets("Sta P&L").Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues

Application.CutCopyMode = False

Workbooks("2017-2022 6 Year Trended PnL by L3 - YTD February.xlsm").Close



Workbooks.Open ("C:\Users\dokat\OneDrive - BIC\BIC\Power BI\Trade Report\Montly Data\2017-2022 6 Year Trended PnL by L3 - February")

Dim Lrm  As Long
Dim wsm As Worksheet
Set wsm = Workbooks("2017-2022 6 Year Trended PnL by L3 - February.xlsm").Worksheets("Summary")
Lrm = wsm.Cells(Rows.Count, 1).End(xlUp).Row

ws.Range("A2:F" & Lrm).Copy

Workbooks("Sta P&L Test.xlsm").Sheets("Sta P&L").Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues

Application.CutCopyMode = False

Workbooks("2017-2022 6 Year Trended PnL by L3 - February").Close




Application.ScreenUpdating = True

End Sub
Solved the problem. There was a typo


Below worked.

VBA Code:
Private Sub CopyPasteYTD()
On Error Resume Next
Application.ScreenUpdating = False

Dim Lrs As Long
Lrs = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
Range("A7346:F" & Lrs).ClearContents

Workbooks.Open ("C:\Users\dokat\OneDrive - BIC\BIC\Power BI\Trade Report\Montly Data\2017-2022 6 Year Trended PnL by L3 - YTD February.xlsm")

Dim Lr  As Long
Dim ws As Worksheet
Set ws = Workbooks("2017-2022 6 Year Trended PnL by L3 - YTD February.xlsm").Worksheets("Summary")
Lr = ws.Cells(Rows.Count, 1).End(xlUp).Row

ws.Range("A1:F" & Lr).Copy

Workbooks("Sta P&L Test.xlsm").Sheets("Sta P&L").Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues

Application.CutCopyMode = False

Workbooks("2017-2022 6 Year Trended PnL by L3 - YTD February.xlsm").Close

Workbooks.Open ("C:\Users\dokat\OneDrive - BIC\BIC\Power BI\Trade Report\Montly Data\2017-2022 6 Year Trended PnL by L3 - February.xlsm")

Dim Lrm  As Long
Dim wsm As Worksheet
Set wsm = Workbooks("2017-2022 6 Year Trended PnL by L3 - February.xlsm").Worksheets("Summary")
Lrm = wsm.Cells(Rows.Count, 1).End(xlUp).Row

wsm.Range("A2:F" & Lrm).Copy

Workbooks("Sta P&L Test.xlsm").Sheets("Sta P&L").Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues

Application.CutCopyMode = False

Workbooks("2017-2022 6 Year Trended PnL by L3 - February.xlsm").Close


Application.ScreenUpdating = True

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,187
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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