Saving Chart Data and error handling

Tech1_uk

New Member
Joined
Sep 22, 2018
Messages
17
Hi All,

I have command button that will save chart from a sheet as a jpg file.(Ifound this code on this Forum) This is working OK but I need to make sure the file is saved to the users desktop. I pretty sure the code I have used is OK for this but I won't mind someone checking it please. Currently the file will overwrite a existing file of the same name without a prompt.

May be it is best if the user can select their own filename and location, but I have no idea where to start with that, but that would fix the 2 problems above.

Also currently if the sheet"Charts" doesn't exist it will error, I need a msgbox and then return to the userform.

Any help would be great.

Cheers

Code:
Private Sub CommandButton6_Click()Dim objChrt As ChartObject
Dim myChart As Chart


    Set objChrt = Sheets("Chart").ChartObjects(1)
    Set myChart = objChrt.Chart


    myFileName = "Cell Data.jpg"


    On Error Resume Next
    Kill ThisWorkbook.Path & "\" & myFileName
    On Error GoTo 0


myChart.Export Filename:=myFileName, Filtername:="jpg"
    MsgBox "File Cell Data.jpg has been saved to the desktop", vbInformation


End Sub
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).

Forum statistics

Threads
1,223,911
Messages
6,175,326
Members
452,635
Latest member
laura12345

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