Excel 图表宏调整大小
我有一个宏,可以根据用户突出显示的数据创建图表并将其格式化为我所需的规格,但我意识到我只使用第一列是x轴日期的数据对其进行了测试。如果情况并非如此,并且所有突出显示的列都包含图表的数据,则宏将失败Excel 图表宏调整大小,excel,vba,Excel,Vba,我有一个宏,可以根据用户突出显示的数据创建图表并将其格式化为我所需的规格,但我意识到我只使用第一列是x轴日期的数据对其进行了测试。如果情况并非如此,并且所有突出显示的列都包含图表的数据,则宏将失败 Sub fullPageLine() Dim rng As Range Dim cht As chart Dim i As Long, color As Long Dim srs As Series Set cht = ActiveSheet.Shapes.AddChart2(227, xlLine
Sub fullPageLine()
Dim rng As Range
Dim cht As chart
Dim i As Long, color As Long
Dim srs As Series
Set cht = ActiveSheet.Shapes.AddChart2(227, xlLine).chart
'Adjust chart size full vs half using number between ScaleHeight/Width and msoFalse
cht.Parent.ShapeRange(1).ScaleWidth 1.3, msoFalse, msoScaleFromTopLeft
cht.Parent.ShapeRange(1).ScaleHeight 1.3, msoFalse, msoScaleFromTopLeft
'Format axes
With cht.ChartArea
.Format.TextFrame2.TextRange.Font.Name = "Arial"
.Format.TextFrame2.TextRange.Font.Size = 7
.Format.TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(1, 0, 0)
End With
'Insert and format axis titles & plot area
cht.Axes(xlValue, xlPrimary).HasTitle = True
With cht.PlotArea
.Left = 10
.Width = 422
.Height = 220
End With
With cht.Axes(xlValue).AxisTitle
.Left = 5
.Top = 20
.Orientation = xlHorizontal
End With
'Format title change left and top values to shift title location
With cht.ChartTitle
.Font.Size = 10
.Left = 0
.Top = 2
With .Format.TextFrame2.TextRange.Characters.Font
.BaselineOffset = 0
.Bold = msoTrue
.Size = 10
.Name = "Arial"
.Caps = msoAllCaps
.Fill.ForeColor.RGB = RGB(1, 0, 0)
End With
End With
'Format legend
If cht.HasLegend Then
With cht.Legend.Format.TextFrame2.TextRange.Font
.NameComplexScript = "Arial"
.NameFarEast = "Arial"
.Name = "Arial"
.Size = 7
End With
End If
'Change chart series fill color
For i = 1 To cht.FullSeriesCollection.count
' Get the color based on series index
Select Case i
Case 1
color = RGB(242, 105, 36)
Case 2
color = RGB(1, 0, 0)
Case 3
color = RGB(85, 85, 85)
Case 4
color = RGB(128, 128, 128)
Case 5
color = RGB(192, 192, 192)
End Select
' Assign series color formats
'## NOTE: This only works for the cases defined in the above Select statement.
Set srs = cht.FullSeriesCollection(i)
With srs.Format.Line
.Visible = msoTrue
.ForeColor.RGB = color
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = bright
.Transparency = 0
End With
Next
End Sub
我得到一个弹出窗口,上面写着“系统错误&H80004005(-2147467259)。未指定的错误”,并将其追溯到代码的这一部分:
With cht.PlotArea
.Left = 10
.Width = 422
.Height = 220
End With
我不明白为什么根据图表的x轴类型,调整绘图区域的大小会导致错误。非常感谢您的建议。以&H8开头的错误消息通常是权限类型错误。指针正在寻址您无权访问的内存区域,或者出现文件权限错误。有时,当调用子例程时,它链接到将变量推/推到堆栈上。如果调用例程和子例程中的参数不相同,则可能发生此类型错误。我会在运行程序时打开任务管理器,查看程序运行时生成了多少进程。您可能正在创建许多进程,并且存在某种堆栈问题。这是一个堆栈问题,谢谢!请允许我请求您接受答案,以便其他人能够更快地获取信息,从而有助于社区@ahb23以&H8开头的错误消息通常是权限类型错误。指针正在寻址您无权访问的内存区域,或者出现文件权限错误。有时,当调用子例程时,它链接到将变量推/推到堆栈上。如果调用例程和子例程中的参数不相同,则可能发生此类型错误。我会在运行程序时打开任务管理器,查看程序运行时生成了多少进程。您可能正在创建许多进程,并且存在某种堆栈问题。这是一个堆栈问题,谢谢!我想请你接受这个答案,这样其他人可以更快地获得信息,从而帮助社区@ahb23