A Cohen
A Cohen

Reputation: 456

VBA - New Chart is not adding title, legends, or deleting

I recently changed a bit of my code, however, I made sure to change the ranges. The issue that I am running into is that my chart, that is created and placed into its own sheet, is saying that .HasTitle = False, even though my code says True; and when I hover over True, it says True = True. I'll Provide my code and a screenshot of part of the area affected and the chart.

Dim Histo As Chart, d As Worksheet
Set d = Worksheets("Data")
    Application.Run "ATPVBAEN.XLAM!Histogram", d.Range("D2", d.Range("D2").End(xlDown)) _
   , d.Range("G1"), d.Range("F2", d.Range("F2").End(xlDown)), False, False _
   , False, False

Set Histo = Charts.Add
With Histo
    .SetSourceData Source:=d.Range("G2", d.Range("H2").End(xlDown))
    .ChartType = xlColumnClustered
    .HasTitle = True
    .ChartTitle.Text = "Cancelation Distribution by Year"
    .Axes(xlCategory, xlPrimary).HasTitle = True
    .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Total Years of Contract"
    .Axes(xlValue, xlPrimary).HasTitle = True
    .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "# of Contracts Canceled"
    .Location Where:=xlLocationAsNewSheet, Name:="Graph"
End With

Data

Chart

Thanks in advance!

Upvotes: 0

Views: 600

Answers (1)

bobajob
bobajob

Reputation: 1192

Try putting .Location Where:=xlLocationAsNewSheet, Name:="Graph" before .HasTitle = True. I think that In your version, excel is still trying to work out where the chart should go (and so the chart doesn't exist yet) when you try and give it a title.

Upvotes: 1

Related Questions