Excel VBA“此对象没有标题”

时间:2015-06-15 09:25:12

标签: excel vba excel-vba

我在这里有一个子,它返回"object has no title"错误。我在这里不知所措。我真的不明白这个错误。

有趣的是,它之前没有任何错误消息,但现在它只返回没有标题的对象,即使所有图表都将标题设置为true。

Sub embedchart_secondsup()

 Dim totalfailureChart As Chart, iChart As Chart, jChart As Chart

 Dim totalfailureRange As Range, iRange As Range, jRange As Range, destinationSheet As String


destinationSheet = ActiveSheet.Name

 'expected value of Total failure

Set totalfailureRange = Range("'Binomial Sheet'!$A$56:$B$62")
    Set totalfailureChart = ActiveSheet.Shapes.AddChart(xlColumnClustered, Left:=ActiveSheet.Range("J14").Left, Top:=ActiveSheet.Range("J14").Top, Width:=ActiveSheet.Range("J14:O14").Width, Height:=ActiveSheet.Range("J14:O24").Height).Chart
    Set totalfailureChart = totalfailureChart.Location(Where:=xlLocationAsObject, Name:=destinationSheet)

    totalfailureChart.SetSourceData Source:=totalfailureRange
    totalfailureChart.FullSeriesCollection(1).Delete
    totalfailureChart.FullSeriesCollection(1).Delete
    totalfailureChart.SeriesCollection.NewSeries
    totalfailureChart.FullSeriesCollection(1).Values = "='Binomial Sheet'!$B$56:$B$62"
    totalfailureChart.FullSeriesCollection(1).XValues = "='Binomial Sheet'!$A$56:$A$62"
    totalfailureChart.HasLegend = False
    totalfailureChart.HasTitle = True
    totalfailureChart.ChartTitle.Text = "Total Failure"

'expected value of i % (first supplier) failure

Set iRange = Range("'Binomial Sheet'!$E$56:$F$62")
    Set iChart = ActiveSheet.Shapes.AddChart(xlColumnClustered, Left:=ActiveSheet.Range("F2").Left, Top:=ActiveSheet.Range("F2").Top, Width:=ActiveSheet.Range("F2:K2").Width, Height:=ActiveSheet.Range("F2:K12").Height).Chart
    Set iChart = iChart.Location(Where:=xlLocationAsObject, Name:=destinationSheet)

    iChart.SetSourceData Source:=iRange
    iChart.FullSeriesCollection(1).Delete
    iChart.FullSeriesCollection(1).Delete
    iChart.SeriesCollection.NewSeries
    iChart.FullSeriesCollection(1).Values = "='Binomial Sheet'!$F$56:$F$62"
    iChart.FullSeriesCollection(1).XValues = "='Binomial Sheet'!$E$56:$E$62"
    iChart.HasLegend = False
    iChart.HasTitle = True
    iChart.ChartTitle.Text = "i % Failure"

'expected value of j % (second supplier) failure

    Set jRange = Range("'Binomial Sheet'!$I$56:$J$62")
    Set jChart = ActiveSheet.Shapes.AddChart(xlColumnClustered, Left:=ActiveSheet.Range("M2").Left, Top:=ActiveSheet.Range("M2").Top, Width:=ActiveSheet.Range("M2:R2").Width, Height:=ActiveSheet.Range("M2:R12").Height).Chart
    Set jChart = jChart.Location(Where:=xlLocationAsObject, Name:=destinationSheet)

    jChart.SetSourceData Source:=iRange
    jChart.FullSeriesCollection(1).Delete
    jChart.FullSeriesCollection(1).Delete
    jChart.SeriesCollection.NewSeries
    jChart.FullSeriesCollection(1).Values = "='Binomial Sheet'!$J$56:$J$62"
    jChart.FullSeriesCollection(1).XValues = "='Binomial Sheet'!$I$56:$I$62"
    jChart.HasLegend = False
    jChart.HasTitle = True
    jChart.ChartTitle.Text ="j % Failure"

End Sub

编辑:我只是注意到sub,只要我不改变单元格中的任何东西(7,4)和(8,4),即使我没有引用这些单元格

2 个答案:

答案 0 :(得分:0)

我也遇到过这个问题。感谢KazimierzJawor,我发现使用

.SetElement (msoElementChartTitleAboveChart)

在标题上的任何操作之前修复它。

我发现的一个非常奇怪的事情是,如果代码执行正常会发生错误,但是当我在调试器中浏览它时它永远不会发生...我的唯一猜测是它与时序有关,在调试器中,在尝试设置标题之前,显示会刷新。

答案 1 :(得分:0)

我最近在Excel 2016(Windows 10)中遇到过类似的问题。 在我的情况下,似乎使用.SetSourceData不能一致地工作(可能是由于Ted Docherty的帖子中提到的时间问题)。

我不得不使用以下方法手动添加数据系列:

.SeriesCollection.NewSeries

以前(Windows 7和旧版机器上的Excel 2016).SetSourceData工作正常但在新机器上我得到了“对象没有”标题错误,除非在调试模式中单步调试代码。