Error when adding title to chart

nbuk

New Member
Joined
Jun 25, 2010
Messages
30
I am creating several charts and I get an error when I try to add a title to some graphs.

Code:
        'Graph
        With Sheet1.ChartObjects.Add(Left:=390, Width:=375, Top:=10, Height:=225)
        .Chart.SetSourceData Source:=Sheet22.Range("A1:R2")
        .Chart.ChartType = xlColumnClustered
        .Chart.ChartTitle.Characters.Text = "SC1 Downtime " & StartDate & "-" & EndDate
        .Chart.SetElement (msoElementPrimaryValueAxisTitleRotated)
        .Chart.Axes(xlValue, xlPrimary).AxisTitle.Text = "Total Hours"
        End With
        
        With Sheet1.ChartObjects.Add(Left:=775, Width:=375, Top:=10, Height:=225)
        .Chart.SetSourceData Source:=Sheet22.Range("A3:R4")
        .Chart.ChartType = xlColumnClustered
        .Chart.ChartTitle.Characters.Text = "SC2 Downtime " & StartDate & "-" & EndDate
        .Chart.SetElement (msoElementPrimaryValueAxisTitleRotated)
        .Chart.Axes(xlValue, xlPrimary).AxisTitle.Text = "Total Hours"
        End With
        
        With Sheet1.ChartObjects.Add(Left:=390, Width:=375, Top:=245, Height:=225)
        .Chart.SetSourceData Source:=Sheet22.Range("A5:R6")
        .Chart.ChartType = xlColumnClustered
        .Chart.ChartTitle.Characters.Text = "SC5 Downtime " & StartDate & "-" & EndDate
        .Chart.SetElement (msoElementPrimaryValueAxisTitleRotated)
        .Chart.Axes(xlValue, xlPrimary).AxisTitle.Text = "Total Hours"
        End With
        
        With Sheet1.ChartObjects.Add(Left:=775, Width:=375, Top:=245, Height:=225)
        .Chart.SetSourceData Source:=Sheet22.Range("A7:R8")
        .Chart.ChartType = xlColumnClustered
        .Chart.ChartTitle.Characters.Text = "SC4 Downtime " & StartDate & "-" & EndDate
        .Chart.SetElement (msoElementPrimaryValueAxisTitleRotated)
        .Chart.Axes(xlValue, xlPrimary).AxisTitle.Text = "Total Hours"
        End With
        
        'Graph Line
        With Sheet1.ChartObjects.Add(Left:=390, Width:=375, Top:=480, Height:=225)
        .Chart.SetSourceData Source:=Sheet8.Range("T" & iStart & ":T" & iEnd)
        .Chart.ChartType = xlLineMarkers
        .Chart.ChartTitle.Characters.Text = "SC1 " & StartDate & "-" & EndDate
        .Chart.SeriesCollection(1).XValues = Sheet8.Range("B" & iStart & ":B" & iEnd)
        .Chart.Axes(xlCategory).CategoryType = xlCategoryScale
        .Chart.SetElement (msoElementPrimaryValueAxisTitleRotated)
        .Chart.Axes(xlValue, xlPrimary).AxisTitle.Text = "Inch2/min"
        End With
        
        With Sheet1.ChartObjects.Add(Left:=775, Width:=375, Top:=480, Height:=225)
        .Chart.SetSourceData Source:=Sheet9.Range("T" & iStart & ":T" & iEnd)
        .Chart.ChartType = xlLineMarkers
        .Chart.ChartTitle.Characters.Text = "SC2 " & StartDate & "-" & EndDate
        .Chart.SeriesCollection(1).XValues = Sheet9.Range("B" & iStart & ":B" & iEnd)
        .Chart.Axes(xlCategory).CategoryType = xlCategoryScale
        .Chart.SetElement (msoElementPrimaryValueAxisTitleRotated)
        .Chart.Axes(xlValue, xlPrimary).AxisTitle.Text = "Inch2/min"
        End With
        
        With Sheet1.ChartObjects.Add(Left:=390, Width:=375, Top:=515, Height:=225)
        .Chart.SetSourceData Source:=Sheet10.Range("T" & iStart & ":T" & iEnd)
        .Chart.ChartType = xlLineMarkers
        .Chart.ChartTitle.Characters.Text = "SC5 " & StartDate & "-" & EndDate
        .Chart.SeriesCollection(1).XValues = Sheet10.Range("B" & iStart & ":B" & iEnd)
        .Chart.Axes(xlCategory).CategoryType = xlCategoryScale
        .Chart.SetElement (msoElementPrimaryValueAxisTitleRotated)
        .Chart.Axes(xlValue, xlPrimary).AxisTitle.Text = "Inch2/min"
        End With
        
        With Sheet1.ChartObjects.Add(Left:=775, Width:=375, Top:=515, Height:=225)
        .Chart.SetSourceData Source:=Sheet11.Range("T" & iStart & ":T" & iEnd)
        .Chart.ChartType = xlLineMarkers
        .HasTitle = True
        .Chart.ChartTitle.Characters.Text = "SC4 " & StartDate & "-" & EndDate
        .Chart.SeriesCollection(1).XValues = Sheet11.Range("B" & iStart & ":B" & iEnd)
        .Chart.Axes(xlCategory).CategoryType = xlCategoryScale
        .Chart.SetElement (msoElementPrimaryValueAxisTitleRotated)
        .Chart.Axes(xlValue, xlPrimary).AxisTitle.Text = "Inch2/min"
        End With
The first 4 graphs are executed perfectly, but when I got to the fifth, I get an error on the line which adds the title:

"This object has no title"

Does anyone know why this happens on that particular graph (and all others after it). I used the same line in previous charts and it worked just fine.

Thanks!!
 
2003 as well.

Its amazing, I restarted my system and the code worked without any changes from my original post.... I just hope it wont do this again.

Thanks for the help :)
 
Upvote 0

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
I came back to this project today and I am getting an error again. This is very strange because it was working well for the first couple of hours through testing but then started giving me an error again.

Here is the code:

Code:
With Sheet1.ChartObjects.Add(Left:=390, Width:=375, Top:=25, Height:=225)
        .chart.SetSourceData Source:=Sheet22.Range("A1:L2")
        .chart.ChartType = xlColumnClustered
        .chart.HasTitle = True
        .chart.ChartTitle.Characters.Text = "REVERB1 Downtime " & Me("Month").Value & "/" & Me("Year").Value
        .chart.SetElement (msoElementPrimaryValueAxisTitleRotated)
        .chart.Axes(xlValue, xlPrimary).AxisTitle.Text = "Total Minutes"
        .chart.Legend.Delete
        .chart.CopyPicture Appearance:=xlScreen, Format:=xlPicture
        End With

I have a lot of different graphs. Some run fine without the "hastitle = true" line and others get an error. Also, if I take out the "hastitle = true" line from where i get an error it still doesn't help.

Ant ideas?
 
Upvote 0
Figured out why sometimes it worked and sometimes didn't.

Me("Month").Value and Me("Year").Value are invalid since I changed variable names to cboMonth and cboYear to make my code more readable in another area.

Silly mistake.

Thanks for all of your help Andrew Poulsom
 
Upvote 0

Forum statistics

Threads
1,214,643
Messages
6,120,702
Members
448,980
Latest member
CarlosWin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top