Excel 图表标题莫名其妙地消失了

Excel 图表标题莫名其妙地消失了,excel,vba,charts,Excel,Vba,Charts,我有一个宏,它在给定工作簿的每个选项卡上创建一个图表(在做了一些其他事情之后)。作为其中的一部分,图表应该添加一个标题,并将标题设置为图纸名称。下面是我如何尝试做到这一点的: S.Shapes.AddChart2(227, xlLine).Select ActiveChart.SeriesCollection.NewSeries ActiveChart.FullSeriesCollection(1).Name = "=" & S.Name & "!$H$1" ActiveChar

我有一个宏,它在给定工作簿的每个选项卡上创建一个图表(在做了一些其他事情之后)。作为其中的一部分,图表应该添加一个标题,并将标题设置为图纸名称。下面是我如何尝试做到这一点的:

S.Shapes.AddChart2(227, xlLine).Select
ActiveChart.SeriesCollection.NewSeries
ActiveChart.FullSeriesCollection(1).Name = "=" & S.Name & "!$H$1"
ActiveChart.FullSeriesCollection(1).Values = "=" & S.Name & "!$H$2:$H$" & i
ActiveChart.FullSeriesCollection(1).XValues = "=" & S.Name & "!$G$2:$G$" & i
temp = 0
Do While ActiveChart.HasTitle = False
    If temp <= 5 Then
        ActiveChart.HasTitle = True
        temp = temp + 1
    Else
        MsgBox "The script failed to add a title to the chart on " & S.Name
    End If
Loop
If ActiveChart.HasTitle Then
    ActiveChart.ChartTitle.Text = S.Name
End If
S.Shapes.AddChart2(227,xlLine)。选择
ActiveChart.SeriesCollection.NewSeries
ActiveChart.FullSeriesCollection(1.Name=“=”&S.Name&“!$H$1”
ActiveChart.FullSeriesCollection(1).Values=“=”&S.Name&“!$H$2:$H$”&i
ActiveChart.FullSeriesCollection(1).XValues=“=”&S.Name&“!$G$2:$G$”&i
温度=0
Do While ActiveChart.hasttle=False

如果temp我需要特定的错误,但是我对所选/活动对象有奇怪的怪癖。如果这是对图表的引用,那么您应该能够将这些ActiveChart引用替换为“s”。根据您的Excel版本,它可能是S.Chart.SeriesCollection

要测试的内容,请参见“这一行”

Dim cht as Chart

Set cht = S.Shapes.AddChart2(227, xlLine).Chart
cht.SetElement (msoElementChartTitleAboveChart) 'this line
cht.SeriesCollection.NewSeries
cht.FullSeriesCollection(1).Name = "=" & S.Name & "!$H$1"
cht.FullSeriesCollection(1).Values = "=" & S.Name & "!$H$2:$H$" & i
cht.FullSeriesCollection(1).XValues = "=" & S.Name & "!$G$2:$G$" & i
temp = 0
Do While cht.HasTitle = False
    If temp <= 5 Then
        cht.HasTitle = True
        temp = temp + 1
    Else
        MsgBox "The script failed to add a title to the chart on " & S.Name
    End If
Loop
If cht.HasTitle Then
    cht.ChartTitle.Text = S.Name
End If
Dim cht作为图表
设置cht=S.Shapes.AddChart2(227,xlLine).Chart
cht.SetElement(msoElementChartTitleAboveChart)'此行
cht.SeriesCollection.NewSeries
cht.FullSeriesCollection(1).Name=“=”&S.Name&“!$H$1”
cht.FullSeriesCollection(1).Values=“=”&S.Name&“!$H$2:$H$”&i
cht.FullSeriesCollection(1).XValues=“=”&S.Name&“!$G$2:$G$”&i
温度=0
当cht.hasttle=False时执行

如果temp我需要特定的错误,但是我对所选/活动对象有奇怪的怪癖。如果这是对图表的引用,那么您应该能够将这些ActiveChart引用替换为“s”。根据您的Excel版本,它可能是S.Chart.SeriesCollection

要测试的内容,请参见“这一行”

Dim cht as Chart

Set cht = S.Shapes.AddChart2(227, xlLine).Chart
cht.SetElement (msoElementChartTitleAboveChart) 'this line
cht.SeriesCollection.NewSeries
cht.FullSeriesCollection(1).Name = "=" & S.Name & "!$H$1"
cht.FullSeriesCollection(1).Values = "=" & S.Name & "!$H$2:$H$" & i
cht.FullSeriesCollection(1).XValues = "=" & S.Name & "!$G$2:$G$" & i
temp = 0
Do While cht.HasTitle = False
    If temp <= 5 Then
        cht.HasTitle = True
        temp = temp + 1
    Else
        MsgBox "The script failed to add a title to the chart on " & S.Name
    End If
Loop
If cht.HasTitle Then
    cht.ChartTitle.Text = S.Name
End If
Dim cht作为图表
设置cht=S.Shapes.AddChart2(227,xlLine).Chart
cht.SetElement(msoElementChartTitleAboveChart)'此行
cht.SeriesCollection.NewSeries
cht.FullSeriesCollection(1).Name=“=”&S.Name&“!$H$1”
cht.FullSeriesCollection(1).Values=“=”&S.Name&“!$H$2:$H$”&i
cht.FullSeriesCollection(1).XValues=“=”&S.Name&“!$G$2:$G$”&i
温度=0
当cht.hasttle=False时执行

如果temp我需要特定的错误,但是我对所选/活动对象有奇怪的怪癖。如果这是对图表的引用,那么您应该能够将这些ActiveChart引用替换为“s”。根据您的Excel版本,它可能是S.Chart.SeriesCollection

要测试的内容,请参见“这一行”

Dim cht as Chart

Set cht = S.Shapes.AddChart2(227, xlLine).Chart
cht.SetElement (msoElementChartTitleAboveChart) 'this line
cht.SeriesCollection.NewSeries
cht.FullSeriesCollection(1).Name = "=" & S.Name & "!$H$1"
cht.FullSeriesCollection(1).Values = "=" & S.Name & "!$H$2:$H$" & i
cht.FullSeriesCollection(1).XValues = "=" & S.Name & "!$G$2:$G$" & i
temp = 0
Do While cht.HasTitle = False
    If temp <= 5 Then
        cht.HasTitle = True
        temp = temp + 1
    Else
        MsgBox "The script failed to add a title to the chart on " & S.Name
    End If
Loop
If cht.HasTitle Then
    cht.ChartTitle.Text = S.Name
End If
Dim cht作为图表
设置cht=S.Shapes.AddChart2(227,xlLine).Chart
cht.SetElement(msoElementChartTitleAboveChart)'此行
cht.SeriesCollection.NewSeries
cht.FullSeriesCollection(1).Name=“=”&S.Name&“!$H$1”
cht.FullSeriesCollection(1).Values=“=”&S.Name&“!$H$2:$H$”&i
cht.FullSeriesCollection(1).XValues=“=”&S.Name&“!$G$2:$G$”&i
温度=0
当cht.hasttle=False时执行

如果temp我需要特定的错误,但是我对所选/活动对象有奇怪的怪癖。如果这是对图表的引用,那么您应该能够将这些ActiveChart引用替换为“s”。根据您的Excel版本,它可能是S.Chart.SeriesCollection

要测试的内容,请参见“这一行”

Dim cht as Chart

Set cht = S.Shapes.AddChart2(227, xlLine).Chart
cht.SetElement (msoElementChartTitleAboveChart) 'this line
cht.SeriesCollection.NewSeries
cht.FullSeriesCollection(1).Name = "=" & S.Name & "!$H$1"
cht.FullSeriesCollection(1).Values = "=" & S.Name & "!$H$2:$H$" & i
cht.FullSeriesCollection(1).XValues = "=" & S.Name & "!$G$2:$G$" & i
temp = 0
Do While cht.HasTitle = False
    If temp <= 5 Then
        cht.HasTitle = True
        temp = temp + 1
    Else
        MsgBox "The script failed to add a title to the chart on " & S.Name
    End If
Loop
If cht.HasTitle Then
    cht.ChartTitle.Text = S.Name
End If
Dim cht作为图表
设置cht=S.Shapes.AddChart2(227,xlLine).Chart
cht.SetElement(msoElementChartTitleAboveChart)'此行
cht.SeriesCollection.NewSeries
cht.FullSeriesCollection(1).Name=“=”&S.Name&“!$H$1”
cht.FullSeriesCollection(1).Values=“=”&S.Name&“!$H$2:$H$”&i
cht.FullSeriesCollection(1).XValues=“=”&S.Name&“!$G$2:$G$”&i
温度=0
当cht.hasttle=False时执行

如果temp我最终使用的代码如下。所有这些都归功于吉米·史密斯和大卫·泽蒙斯,他们把我带到了这里,只是想为将来找到这一页的人捕捉最终状态

Dim ch as ChartObject

Set ch = S.ChartObjects.Add(Left:=Range("J2").Left, Top:=Range("J2").Top, Width:=500, Height:=325)
ch.Chart.SeriesCollection.NewSeries
ch.Chart.FullSeriesCollection(1).Name = "=" & S.Name & "!$H$1"
ch.Chart.FullSeriesCollection(1).Values = "=" & S.Name & "!$H$2:$H$" & i
ch.Chart.FullSeriesCollection(1).XValues = "=" & S.Name & "!$G$2:$G$" & i
ch.Chart.ChartType = xlLine
temp = 0
Do While ch.Chart.HasTitle = False
    If temp <= 5 Then
        ch.Chart.HasTitle = True
        temp = temp + 1
    Else
        MsgBox "The script failed to add a title to the chart on " & S.Name
    End If
Loop
If ch.Chart.HasTitle Then
    ch.Chart.ChartTitle.Text = S.Name
End If
Dim ch as ChartObject
设置ch=S.ChartObjects.Add(左:=范围(“J2”)。左,顶部:=范围(“J2”)。顶部,宽度:=500,高度:=325)
ch.Chart.SeriesCollection.NewSeries
ch.Chart.FullSeriesCollection(1).Name=“=”&S.Name&“!$H$1”
ch.Chart.FullSeriesCollection(1).Values=“=”&S.Name&“!$H$2:$H$”&i
ch.Chart.FullSeriesCollection(1).XValues=“=”&S.Name&“!$G$2:$G$”&i
ch.Chart.ChartType=xlLine
温度=0
当ch.Chart.hasttle=False时执行

如果temp我最终使用的代码如下。所有这些都归功于吉米·史密斯和大卫·泽蒙斯,他们把我带到了这里,只是想为将来找到这一页的人捕捉最终状态

Dim ch as ChartObject

Set ch = S.ChartObjects.Add(Left:=Range("J2").Left, Top:=Range("J2").Top, Width:=500, Height:=325)
ch.Chart.SeriesCollection.NewSeries
ch.Chart.FullSeriesCollection(1).Name = "=" & S.Name & "!$H$1"
ch.Chart.FullSeriesCollection(1).Values = "=" & S.Name & "!$H$2:$H$" & i
ch.Chart.FullSeriesCollection(1).XValues = "=" & S.Name & "!$G$2:$G$" & i
ch.Chart.ChartType = xlLine
temp = 0
Do While ch.Chart.HasTitle = False
    If temp <= 5 Then
        ch.Chart.HasTitle = True
        temp = temp + 1
    Else
        MsgBox "The script failed to add a title to the chart on " & S.Name
    End If
Loop
If ch.Chart.HasTitle Then
    ch.Chart.ChartTitle.Text = S.Name
End If
Dim ch as ChartObject
设置ch=S.ChartObjects.Add(左:=范围(“J2”)。左,顶部:=范围(“J2”)。顶部,宽度:=500,高度:=325)
ch.Chart.SeriesCollection.NewSeries
ch.Chart.FullSeriesCollection(1).Name=“=”&S.Name&“!$H$1”
ch.Chart.FullSeriesCollection(1).Values=“=”&S.Name&“!$H$2:$H$”&i
ch.Chart.FullSeriesCollection(1).XValues=“=”&S.Name&“!$G$2:$G$”&i
ch.Chart.ChartType=xlLine
温度=0
当ch.Chart.hasttle=False时执行

如果temp我最终使用的代码如下。所有这些都归功于吉米·史密斯和大卫·泽蒙斯,他们把我带到了这里,只是想为将来找到这一页的人捕捉最终状态

Dim ch as ChartObject

Set ch = S.ChartObjects.Add(Left:=Range("J2").Left, Top:=Range("J2").Top, Width:=500, Height:=325)
ch.Chart.SeriesCollection.NewSeries
ch.Chart.FullSeriesCollection(1).Name = "=" & S.Name & "!$H$1"
ch.Chart.FullSeriesCollection(1).Values = "=" & S.Name & "!$H$2:$H$" & i
ch.Chart.FullSeriesCollection(1).XValues = "=" & S.Name & "!$G$2:$G$" & i
ch.Chart.ChartType = xlLine
temp = 0
Do While ch.Chart.HasTitle = False
    If temp <= 5 Then
        ch.Chart.HasTitle = True
        temp = temp + 1
    Else
        MsgBox "The script failed to add a title to the chart on " & S.Name
    End If
Loop
If ch.Chart.HasTitle Then
    ch.Chart.ChartTitle.Text = S.Name
End If
Dim ch as ChartObject
设置ch=S.ChartObjects.Add(左:=范围(“J2”)。左,顶部:=范围(“J2”)。顶部,宽度:=500,高度:=325)
ch.Chart.SeriesCollection.NewSeries
ch.Chart.FullSeriesCollection(1).Name=“=”&S.Name&“!$H$1”
ch.Chart.FullSeriesCollection(1).Values=“=”&S.Name&“!$H$2:$H$”&i
ch.Chart.FullSeriesCollection(1).XValues=“=”&S.Name&“!$G$2:$G$”&i
ch.Chart.ChartType=xlLine
温度=0
当ch.Chart.hasttle=False时执行

如果temp我最终使用的代码如下。所有这些都归功于吉米·史密斯和大卫·泽蒙斯,他们把我带到了这里,只是想为将来找到这一页的人捕捉最终状态

Dim ch as ChartObject

Set ch = S.ChartObjects.Add(Left:=Range("J2").Left, Top:=Range("J2").Top, Width:=500, Height:=325)
ch.Chart.SeriesCollection.NewSeries
ch.Chart.FullSeriesCollection(1).Name = "=" & S.Name & "!$H$1"
ch.Chart.FullSeriesCollection(1).Values = "=" & S.Name & "!$H$2:$H$" & i
ch.Chart.FullSeriesCollection(1).XValues = "=" & S.Name & "!$G$2:$G$" & i
ch.Chart.ChartType = xlLine
temp = 0
Do While ch.Chart.HasTitle = False
    If temp <= 5 Then
        ch.Chart.HasTitle = True
        temp = temp + 1
    Else
        MsgBox "The script failed to add a title to the chart on " & S.Name
    End If
Loop
If ch.Chart.HasTitle Then
    ch.Chart.ChartTitle.Text = S.Name
End If
Dim ch as ChartObject
设置ch=S.ChartObjects.Add(左:=范围(“J2”)。左,顶部:=范围(“J2”)。顶部,宽度:=500,高度:=325)
ch.Chart.SeriesCollection.NewSeries
ch.Chart.FullSeriesCollection(1).Name=“=”&S.Name&“!$H$1”
ch.Chart.FullSeriesCollection(1).Values=“=”&S.Name&“!$H$2: