使用Excel VBA添加辅助轴

那么我已经写了这个代码,不仅仅是添加辅助轴。 但是,有时会在最后一个With命令上返回运行时错误,代码末尾的.Axes(xlValue, xlSecondary).HasTitle = True 。 为了完整起见,我转录了整个程序。 这个运行时错误并不总是会发生,但我似乎并没有明白什么时候发生,或者为什么…你知道如何解决这个问题吗?

任何帮助,将不胜感激

 Private Sub criargraf() Dim cont, cont2 As Integer Application.ScreenUpdating = False If Sheets("SubUN").ChartObjects.Count > 0 Then Sheets("SubUN").ChartObjects.Delete End If Sheets("SubUN").Shapes.AddChart Set graf = Sheets("SubUN").ChartObjects(1) If graf.Chart.SeriesCollection.Count > 0 Then cont = graf.Chart.SeriesCollection.Count Do While cont > 0 graf.Chart.SeriesCollection(1).Delete 'às vezes,Quando se cria um gráfico, este já vem com uma série, portanto tens de fazer um Newseries a menos cont = cont - 1 Loop End If With graf.Chart .ChartArea.Width = 878 .Parent.Height = 470 .Parent.Top = 37 .Parent.Left = 575 .HasLegend = True .Legend.Left = 215 .Legend.Top = 400 .Legend.Height = 100 .Legend.Width = 500 .PlotArea.Height = 350 .PlotArea.Width = 830 .PlotArea.Left = 20 .PlotArea.Top = 30 .ChartType = xlLine .HasTitle = True .ChartTitle.Characters.Text = Range("E134") .Axes(xlCategory, xlPrimary).HasTitle = True .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Meses" .Axes(xlValue, xlPrimary).HasTitle = True .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = Range("E131") End With cont = 1 cont2 = 1 Do While Range("L" & 121 + cont).Value <> "" If Sheets("SubUN").Rows(120 + cont).EntireRow.Hidden = False Then With graf.Chart .SeriesCollection.NewSeries .SeriesCollection(cont2).Values = Sheets("SubUN").Range("M" & (120 + cont) & ":X" & (120 + cont)) .SeriesCollection(cont2).Name = Sheets("SubUN").Range("L" & (120 + cont)) .SeriesCollection(cont2).XValues = Range("M120:X120") End With cont2 = cont2 + 1 End If cont = cont + 1 Loop cont = 1 Do While Range("L" & 145 + cont).Value <> "" If Sheets("SubUN").Rows(145 + cont).EntireRow.Hidden = False Then With graf.Chart .SeriesCollection.NewSeries .SeriesCollection(cont2).Values = Sheets("SubUN").Range("M" & (145 + cont) & ":X" & (145 + cont)) .SeriesCollection(cont2).Name = Sheets("SubUN").Range("L" & (145 + cont)) .SeriesCollection(cont2).XValues = Range("M145:X145") End With cont2 = cont2 + 1 End If cont = cont + 1 Loop cont = 1 Do While Range("L" & 168 + cont).Value <> "" If Sheets("SubUN").Rows(168 + cont).EntireRow.Hidden = False Then With graf.Chart .SeriesCollection.NewSeries .SeriesCollection(cont2).Values = Sheets("SubUN").Range("M" & (168 + cont) & ":X" & (168 + cont)) .SeriesCollection(cont2).Name = Sheets("SubUN").Range("L" & (168 + cont)) .SeriesCollection(cont2).XValues = Range("M168:X168") .SeriesCollection(cont2).AxisGroup = xlSecondary .SeriesCollection(cont2).ChartType = xlColumnClustered .SeriesCollection(cont2).ApplyDataLabels '.SeriesCollection(cont2).DataLabels.Position = xlLabelPositionAbove End With cont2 = cont2 + 1 End If cont = cont + 1 Loop If cont2 > 2 Then graf.Chart.HasAxis(xlValue, xlSecondary) = True With graf.Chart '.Legend.Left = 2000 * (cont2) ^ -1 .Legend.Width = 100 * (cont2) .Axes(xlValue, xlSecondary).HasTitle = True .Axes(xlValue, xlSecondary).AxisTitle.Characters.Text = " Variação (%)" .Axes(xlValue).HasMajorGridlines = True .Axes(xlValue).MajorGridlines.Border.LineStyle = xlDashDotDot .Axes(xlValue).MajorGridlines.Border.Color = RGB(190, 190, 190) End With End If End Sub 

免责声明:这不是一个严格的答案,因为它不包含新的信息,只是重复它。 但是因为这个问题没有得到答案(除了@An old man in the sea.中的一个@An old man in the sea.的暗示,隐藏在一个评论的深处),我想以平常的方式写在这里,可能帮助别人。


我也收到了一些非常有用的错误消息,如System.Runtime.InteropServices.COMException (0x80004005): Error HRESULT E_FAIL has been returned from a call to a COM component.

作为@An old man in the sea. 在一个评论中提出,我在轴连接了一些数据之后移动了有关格式化轴瓷砖,比例尺等的问题部分。 这导致了Exception的消失,并在几天后取得了进展:-)

所以在伪代码而不是

 FormatSecondaryAxis(chart); DrawDataToSecondaryAxis(chart); 

只需交换顺序,然后先画图然后格式化。


哦,顺便说一句,这个C#代码:

 chart.HasAxis[Excel.XlAxisType.xlValue, Excel.XlAxisGroup.xlSecondary] = true; chart.HasAxis[Excel.XlAxisType.xlCategory, Excel.XlAxisGroup.xlSecondary] = true; chart.SetElement(Microsoft.Office.Core.MsoChartElementType.msoElementSecondaryValueAxisTitleAdjacentToAxis); 

并没有做到这一点。 最后一行抛出exception,如果没有实际的数据没有连接到轴。