I am trying to create a pie chart using VBA. I wrote the below mentioned code but it gives me error 91 while executing. Can anyone help me solve this issue
Code:
Private Sub CommandButton1_Click()
Set excel1 = CreateObject("Excel.Application")
Set worbook = excel1.Workbooks
excel1.Workbooks.Add
excel1.Visible = True
Set ows = excel1.ActiveSheet
'enter the data in newly created excel
ows.Cells(2, 3).Value = "a"
ows.Cells(2, 4).Value = 23
ows.Cells(3, 3).Value = "b"
ows.Cells(3, 4).Value = 34
ows.Cells(4, 3).Value = "c"
ows.Cells(4, 4).Value = 32
ows.Cells(5, 3).Value = "d"
ows.Cells(5, 4).Value = 30
'Add a pie chart to the excel
ows.Shapes.AddChart(XlChartType.xlPie, 400, 200, 250, 150).Select
Set ocht = ActiveChart
Set oRng = ows.Range("c2", "d5")
ocht.SetSourceData Source:=oRng, PlotBy:=xlColumns ' ERROR is OBTAINED at this line
ocht.HasLegend = False
ocht.ChartTitle.Text = "Sample PieChart with Legend and Title"
End Sub
Bookmarks