Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get charts.add command to deliver an empty chart?

Tags:

excel

vba

charts

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.

like image 552
JazZeus Avatar asked Dec 11 '22 10:12

JazZeus


1 Answers

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
like image 125
Kazimierz Jawor Avatar answered Dec 18 '22 08:12

Kazimierz Jawor