Run time error with VBA to copy Chart objects from closed workbook

jessrabbit

New Member
Joined
Feb 26, 2011
Messages
18
Good morning and Happy Easter,

Please could someone help to find a solution to the run time error 5 (Invalid Procedure call or argument) on this line of the code below?

VBA Code:
wsf.ChartObjects("Chart_1").CopyPicture xlScreen, xlP

The whole code is here and is intended to copy specified charts from closed workbooks and paste into the current workbook;

VBA Code:
Sub Macro3()
'
' Macro to copy KPI charts from their closed workbook sources into one location.
'
 
' Declare variables
Dim sourceWorkbook As Workbook
Dim targetWorkbook As Workbook
Dim wsf As Worksheet
Dim wst As Worksheet
 
' Set the destination workbook to the active workbook
Set targetWorkbook = ThisWorkbook
 
' Disable screen updating
Application.ScreenUpdating = False
 
' Open the source workbook
Set sourceWorkbook = Workbooks.Open("xyz123.xlsx")
 
'Set the worksheet that you want to copy data from
Set wsf = sourceWorkbook.Sheets("KPIs")
 
'Set the worksheet that you want to copy data to
Set wst = targetWorkbook.Sheets("KPI Summary")
 
'Copy the Chart to the destination workbook
wsf.ChartObjects("Chart_1").CopyPicture xlScreen, xlP
wst.Range ("c9").Paste
wsf.ChartObjects("Chart_2").CopyPicture xlScreen, xlP
wst.Range ("i9").Paste
wsf.ChartObjects("Chart_3").CopyPicture xlScreen, xlP
wst.Range ("o9").Paste
 
'Close the source workbook without saving changes
sourceWorkbook.Close SaveChanges:=False
 
'Enable screen updating
Application.ScreenUpdating = True
 
'Release object variables to release memory
Set ws = Nothing
Set sourceWorkbook = Nothing
Set targetWorkbook = Nothing
End Sub
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Hi, I've experimented with a few more options and found the solution which is to use;

VBA Code:
wst.Range("c9").PasteSpecial Paste:=Picture

Thank you for looking at this post and hope you have a lovely day.

Jess
 
Upvote 0
The second argument of the CopyPicture method should be xlPicture, for example...

VBA Code:
wsf.ChartObjects("Chart_1").CopyPicture xlScreen, xlPicture

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,183
Members
452,615
Latest member
bogeys2birdies

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