gpt4 book ai didi

excel - 使用 Excel VBA 添加辅助轴

转载 作者:行者123 更新时间:2023-12-04 20:14:22 26 4
gpt4 key购买 nike

好吧,我编写的代码不仅仅是添加辅助轴。但是,有时它会在最后一个 上返回运行时错误。与 命令,在 .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. 的提示),我想以通常的方式在这里写下来,以帮助其他人。

我还收到了一些非常有用的错误消息,例如 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.在评论中建议,在轴连接了一些数据之后,我移动了有关格式化轴图 block 、比例尺等的有问题的部分。这导致异常消失,几天后取得了进展:-)

所以用伪代码代替

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);

不成功。最后一行抛出异常,如果没有实际数据未连接到轴。

关于excel - 使用 Excel VBA 添加辅助轴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31434312/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com