Excel VBA“此对象没有标题”

vpfxa7rd  于 2023-02-10  发布在  其他
关注(0)|答案(3)|浏览(203)

我这里有一个sub,返回"object has no title"错误,我这里很茫然,真的不明白这个错误。
有趣的是,它以前工作时没有任何错误消息,但现在它只返回对象没有标题,即使所有图表的标题设置为真。

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"

末端子组件
编辑:我刚刚注意到,只要我不修改单元格(7,4)和(8,4)中的任何内容,即使我没有引用这些单元格,sub也可以

xbp102n0

xbp102n01#

我也一直有这个问题。多亏了KazimierzJawor,我发现使用

.SetElement (msoElementChartTitleAboveChart)

在对所有权进行任何操作之前都是如此。
我确实发现了一件非常奇怪的事情,如果代码正常执行,错误就会发生,但当我在调试器中浏览它时,它永远不会发生...我唯一的猜测是,它与时间有关,在调试器中,在尝试设置标题之前,显示会刷新。

t3psigkw

t3psigkw2#

我最近在Excel 2016(Windows 10)中遇到了一个类似的问题。在我的案例中,使用.SetSourceData似乎并不一致(可能是由于Ted Docherty的帖子中提到的时间问题)。
我不得不使用以下工具手动添加数据系列:

.SeriesCollection.NewSeries

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

pbossiut

pbossiut3#

我有同样的问题,并发现对于我的情况下,问题是受保护的表。所以使用我。取消保护mypass之前,设置标题,一切顺利。

相关问题