Found this code on the net to essentially add a graph when a button is clicked but delete the graph if the button is deselected. I am quite new to vba but I am not understanding why there is an error 424 for the highlighted code and how to resolve it or if this solution can be completed in a better way. How would one add the object required for GoogleBtn?
VBA Code
Private Sub GoogleBtn_Click()
Dim mySheet As Worksheet
Dim shp As Shape
Dim myChart As Chart
Set mySheet = ActiveWorkbook.Worksheets("Sheet1")
On Error Resume Next
Set shp = mySheet.Shapes("GoogleChart")
On Error GoTo 0
If shp Is Nothing Then
Set shp = mySheet.Shapes.AddChart(XlChartType:=xlColumnClustered, _
Left:=GoogleBtn.Left + GoogleBtn.Width + 2, Top:=GoogleBtn.Top, Height:=100, Width:=150)
End If
Set myChart = shp.Chart
If GoogleBtn.Value = True Then
myChart.SetSourceData Source:=Sheets("Sheet1").Range("A1:B")
myChart.ChartType = xlLine
shp.Name = "GoogleChart"
Else
shp.Delete
End If
End Sub
VBA Code
Private Sub GoogleBtn_Click()
Dim mySheet As Worksheet
Dim shp As Shape
Dim myChart As Chart
Set mySheet = ActiveWorkbook.Worksheets("Sheet1")
On Error Resume Next
Set shp = mySheet.Shapes("GoogleChart")
On Error GoTo 0
If shp Is Nothing Then
Set shp = mySheet.Shapes.AddChart(XlChartType:=xlColumnClustered, _
Left:=GoogleBtn.Left + GoogleBtn.Width + 2, Top:=GoogleBtn.Top, Height:=100, Width:=150)
End If
Set myChart = shp.Chart
If GoogleBtn.Value = True Then
myChart.SetSourceData Source:=Sheets("Sheet1").Range("A1:B")
myChart.ChartType = xlLine
shp.Name = "GoogleChart"
Else
shp.Delete
End If
End Sub