Reputation: 3
i want save chart to png format, (inputbox my png name)
my code is
Sub savechartaspng()
Dim fname As String
ActiveSheet.ChartObjects("Chart 1").Activate
If ActiveChart Is Nothing Then Exit Sub
fname = ThisWorkbook.Path & "\" & InputBox("filename") & ".png"
ActiveChart.Export FileName:=fname, filtername:="png"
MsgBox "saved"
End Sub
how inputbox = cancel or closed to exit sub and Do not save ?
tnx
Upvotes: 0
Views: 49
Reputation: 11755
Get the input from the InputBox first, check check to see if it's blank or not, and only continue if it's not blank.
Sub savechartaspng()
Dim fname As String
ActiveSheet.ChartObjects("Chart 1").Activate
If ActiveChart Is Nothing Then Exit Sub
fname = InputBox("filename")
If fname = "" Then Exit Sub
fname = ThisWorkbook.Path & "\" & fname & ".png"
ActiveChart.Export FileName:=fname, filtername:="png"
MsgBox "saved"
End Sub
Note: It might be a goof idea to also have code that checks to make sure the user did not enter any characters that cannot be used in filenames too.
Upvotes: 1