在单击右键菜单中的 select 数据并 select 编辑另一个系列之前,不会显示图表系列名称
chart series names are not shown until select data in right click menu is clicked and another series is selected
我在 excel 中创建了一个图表,它需要在数据透视切片器选择发生变化时刷新。因为必须添加动态趋势线(基于用户定义的趋势值),所以还没有创建数据透视图,而是从数据透视切片器选择更改时更新的范围中选择图表数据。基于图表刷新的数据透视切片器选择是通过 VBA 完成的。没有趋势线它也能正常工作。 But when trend line is selected and pivot slicer selection is changed, randomly I get 1004 error when the trend line is referred to by it's name i.e. ".SeriesCollection(name of the trend line)".在检查时,我发现图表系列名称不知何故没有更新(屏幕截图下方)。
Select 数据源 Window
当我点击任何其他系列时,图表会立即刷新并显示系列名称(屏幕截图下方)
我试过使用 chart.refresh、chart.activate,但 none 有效。或者,如果我保存 excel,关闭并打开它,我可以正常查看图表,但这不是我的解决方案。
请有人帮我解决这个问题。
我正在使用数据透视表获取数据并使用公式将数据从数据透视表获取到命名范围。更改数据透视切片器后,我将使用命名范围作为源来刷新图表(如下面的代码所示)。
Sub ChartRefresh(ByVal chrtNm As String, Optional ByVal targetFlag As String)
Dim srcRng As Range
Set srcRng = ThisWorkbook.Names(chrtNm).RefersToRange
With ThisWorkbook.Sheets("Graphs").ChartObjects(chrtNm).Chart
.SetSourceData Source:=srcRng, PlotBy:=xlColumns
.ChartType = xlColumnClustered
.Refresh
If targetFlag = "Yes" Then
Call TargetShow(chrtNm)
End If
End With
End Sub
选择趋势线后,我将不同范围的趋势线值添加为新系列(代码下方),这会产生问题。
Sub TargetShow(ByVal chrtNm As String)
Dim srcRng As Range
Set srcRng = ThisWorkbook.Names(chrtNm).RefersToRange
With ThisWorkbook.Sheets("Graphs").ChartObjects(chrtNm).Chart
.SeriesCollection.Add Source:=srcRng.Offset(0, -1).Resize(srcRng.Rows.Count, 1), rowcol:=xlColumns, SeriesLabels:=True
.ChartType = xlColumnClustered
On Error Resume Next
With .SeriesCollection("Target")
If Err.Number = 1004 Then
Err.Clear
MsgBox "Chart Refresh Error. Saving and Closing. Please re-open."
Call TurnOn
Application.DisplayAlerts = False
ThisWorkbook.Close SaveChanges:=True
End If
.ChartType = xlLineMarkers
.Format.Line.ForeColor.RGB = RGB(255, 0, 0)
.Format.Fill.ForeColor.RGB = RGB(255, 0, 0)
End With
On Error GoTo 0
End With
End Sub
我已经通过将趋势线数据添加到命名范围本身解决了这个问题(请参阅下面的代码)。
Sub ChartRefresh(ByVal chrtNm As String, Optional ByVal targetFlag As String)
Dim srcRng As Range
Set srcRng = ThisWorkbook.Names(chrtNm).RefersToRange
With ThisWorkbook.Sheets("BTC Graphs").ChartObjects(chrtNm).Chart
With .SeriesCollection(.SeriesCollection.Count)
.Format.Line.Visible = msoFalse
End With
.SetSourceData Source:=srcRng, PlotBy:=xlColumns
.ChartType = xlColumnClustered
.Refresh
' If targetFlag = "Yes" Then
' Call TargetShow(chrtNm)
' End If
If targetFlag = "Yes" Then
With .SeriesCollection(.SeriesCollection.Count)
.Format.Line.Visible = msoTrue
.ChartType = xlLineMarkers
.Format.Line.ForeColor.RGB = RGB(255, 0, 0)
.Format.Fill.ForeColor.RGB = RGB(255, 0, 0)
.DataLabels.Orientation = xlHorizontal
End With
End If
End With
End Sub
我在 excel 中创建了一个图表,它需要在数据透视切片器选择发生变化时刷新。因为必须添加动态趋势线(基于用户定义的趋势值),所以还没有创建数据透视图,而是从数据透视切片器选择更改时更新的范围中选择图表数据。基于图表刷新的数据透视切片器选择是通过 VBA 完成的。没有趋势线它也能正常工作。 But when trend line is selected and pivot slicer selection is changed, randomly I get 1004 error when the trend line is referred to by it's name i.e. ".SeriesCollection(name of the trend line)".在检查时,我发现图表系列名称不知何故没有更新(屏幕截图下方)。
Select 数据源 Window
当我点击任何其他系列时,图表会立即刷新并显示系列名称(屏幕截图下方)
我试过使用 chart.refresh、chart.activate,但 none 有效。或者,如果我保存 excel,关闭并打开它,我可以正常查看图表,但这不是我的解决方案。
请有人帮我解决这个问题。
我正在使用数据透视表获取数据并使用公式将数据从数据透视表获取到命名范围。更改数据透视切片器后,我将使用命名范围作为源来刷新图表(如下面的代码所示)。
Sub ChartRefresh(ByVal chrtNm As String, Optional ByVal targetFlag As String)
Dim srcRng As Range
Set srcRng = ThisWorkbook.Names(chrtNm).RefersToRange
With ThisWorkbook.Sheets("Graphs").ChartObjects(chrtNm).Chart
.SetSourceData Source:=srcRng, PlotBy:=xlColumns
.ChartType = xlColumnClustered
.Refresh
If targetFlag = "Yes" Then
Call TargetShow(chrtNm)
End If
End With
End Sub
选择趋势线后,我将不同范围的趋势线值添加为新系列(代码下方),这会产生问题。
Sub TargetShow(ByVal chrtNm As String)
Dim srcRng As Range
Set srcRng = ThisWorkbook.Names(chrtNm).RefersToRange
With ThisWorkbook.Sheets("Graphs").ChartObjects(chrtNm).Chart
.SeriesCollection.Add Source:=srcRng.Offset(0, -1).Resize(srcRng.Rows.Count, 1), rowcol:=xlColumns, SeriesLabels:=True
.ChartType = xlColumnClustered
On Error Resume Next
With .SeriesCollection("Target")
If Err.Number = 1004 Then
Err.Clear
MsgBox "Chart Refresh Error. Saving and Closing. Please re-open."
Call TurnOn
Application.DisplayAlerts = False
ThisWorkbook.Close SaveChanges:=True
End If
.ChartType = xlLineMarkers
.Format.Line.ForeColor.RGB = RGB(255, 0, 0)
.Format.Fill.ForeColor.RGB = RGB(255, 0, 0)
End With
On Error GoTo 0
End With
End Sub
我已经通过将趋势线数据添加到命名范围本身解决了这个问题(请参阅下面的代码)。
Sub ChartRefresh(ByVal chrtNm As String, Optional ByVal targetFlag As String)
Dim srcRng As Range
Set srcRng = ThisWorkbook.Names(chrtNm).RefersToRange
With ThisWorkbook.Sheets("BTC Graphs").ChartObjects(chrtNm).Chart
With .SeriesCollection(.SeriesCollection.Count)
.Format.Line.Visible = msoFalse
End With
.SetSourceData Source:=srcRng, PlotBy:=xlColumns
.ChartType = xlColumnClustered
.Refresh
' If targetFlag = "Yes" Then
' Call TargetShow(chrtNm)
' End If
If targetFlag = "Yes" Then
With .SeriesCollection(.SeriesCollection.Count)
.Format.Line.Visible = msoTrue
.ChartType = xlLineMarkers
.Format.Line.ForeColor.RGB = RGB(255, 0, 0)
.Format.Fill.ForeColor.RGB = RGB(255, 0, 0)
.DataLabels.Orientation = xlHorizontal
End With
End If
End With
End Sub