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

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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