I have finally found a code that meets my needs to create a chart (Code below). However I have two problems with that:
1- Worst thing is that every time I run the code it makes 2 sheets on my workbook. One contains a blank chart frame and one contains the chart itself. The latter, thanks to the code becomes hidden but anyways, after 5 times running the code I have 10 new sheets on my workbook that have to go and delete all of them. (This is now solved in comments below the post)
2- I can not resize it with any resizing code that I tried. Here I have .ChartArea.Height
and .ChartArea.Width
which gives me the error: the shape is locked and cannot be resized.
Is there any practical way to control the size really?
Private Sub CommandButton4_Click()
Charts.Add
chartarray1 = Array(Val(UserForm1.TextBox6.Value), Val(UserForm1.TextBox7.Value))
chartarray2 = Array("methane", "carbon")
Dim mychart As Chart
Dim fname As String
Set mychart = Charts.Add
With mychart
.SeriesCollection.NewSeries
.SeriesCollection(1).Name = "emissions"
.SeriesCollection(1).XValues = chartarray2
.SeriesCollection(1).values = chartarray1
.ChartType = xlBarClustered
.ChartArea.Height = 107
.ChartArea.Width = 167
.ChartStyle = 6
End With
ActiveChart.Export "C:\Users\shsy\chart1.jpg"
f = activesheet.Name
Sheets(f).Select
ActiveWindow.SelectedSheets.Visible = False
fname = "C:\Users\shsy\chart1.jpg"
UserForm1.Image1.Picture = LoadPicture(fname)
End Sub
Thanks in advance for your help.