Jean-Pierre Oosthuizen
Jean-Pierre Oosthuizen

Reputation: 2683

Preventing Excel Chart AutoFill

I need to create a Chart in Excel using VBA.

Just using a simple script as below:

Dim Chart1 As Chart
Set Chart1 = Charts.Add

This inserts the Chart as expected. However, depending on my cursor position the Chart will AutoFill / Auto Populate with the relevant surrounding data.

See two examples below:

enter image description here

The obvious and simple way to avoid this is to select the Cursor via VBA to a definite blank cell location.

I can also have a Loop to run through all the SeriesCollection in the Chart and delete them.

I there something like a Chart1.ClearContent? or what else could I do?

Upvotes: 0

Views: 264

Answers (2)

Rory
Rory

Reputation: 34045

You can also use the chart's SetSourceData method to change to whatever data you actually want, or simply point it at a blank cell:

Chart1.SetSourceData Sheets("Sheet1").Range("A1")

for example.

Upvotes: 1

teylyn
teylyn

Reputation: 35915

The obvious and simple way to avoid this is to select the Cursor via VBA to a definite blank cell location.

Just do that, then. That's not too hard and faster than cleaning out gunk from a chart. One of the few events where .Select is useful.

Upvotes: 0

Related Questions