The command charts.add
in VBA adds a new chart object to the workbook. I think hitting F11
in Excel does exactly the same.
The problem is, that this command uses the data around the at that point selected worksheet and cell to populate the chart. If there is no data or no usable data, it returns an empty chart.
My question is: "How can I force the command to deliver an empty chart?". I intend to populate the chart with VBA code thereafter.
A simple answer to the question is to create a new empty worksheet and select cell A1 on that worksheet and then create a new chart, but that is a rather ugly solution. Any help on elegant solutions would be appreciated.
Try to add additional line which will immediately remove data right after the chart appears:
Charts.Add
ActiveChart.ChartArea.Clear
EDIT Alternative solution, but this will jump back to data sheet:
'select last cell in the sheet which is usually empty
Dim tmpSel As Range
Set tmpSel = Selection
Cells(Rows.Count, Columns.Count).Select
'add chart
Charts.Add
'back to base sheet and select range previously selected
tmpSel.Parent.Activate
tmpSel.Select
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With