【发布时间】:2013-07-17 12:46:29
【问题描述】:
我正在使用 VBScript 从 Excel 2003 中的数据列创建线散点图。结果很好,但我想编辑图表的一些属性,例如背景颜色和轴标签。我在 Excel 中手动完成并记录了一个宏,它给了我以下 VBA 代码:
ActiveChart.PlotArea.Select
With Selection.Border
.ColorIndex = 16
.Weight = xlThin
.LineStyle = xlContinuous
End With
With Selection.Interior
.ColorIndex = 2
.PatternColorIndex = 1
.Pattern = xlSolid
End With
ActiveChart.Axes(xlCategory).Select
With Selection.TickLabels
.ReadingOrder = xlContext
.Orientation = 45
End With
ActiveChart.Axes(xlValue).AxisTitle.Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.ReadingOrder = xlContext
.Orientation = xlHorizontal
End With
ActiveChart.ChartArea.Select
End Sub
这对于 VBA 来说看起来不错,但我在将其转换为 VBScript 时遇到了麻烦。我应该如何开始?这是我目前的代码:
Set objChart = objExcel.Charts.Add()
With objExcel.ActiveChart
.ChartType = xlXYScatterLinesNoMarkers
.SeriesCollection(1).Interior.Color = RGB(255, 0, 0)
.HasTitle = True
.ChartTitle.Text = "usage"
.Axes(xlCategory, xlPrimary).HasTitle = True
.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Time"
.Axes(xlValue, xlPrimary).HasTitle = True
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "units"
.HasLegend = False
.SetSourceData objWorksheet.Range("E1","F" & LastRow), xlColumns
.SetSourceData objWorksheet.Range("E1:F200"), xlColumns
End With
.SeriesCollection(1).Interior.Color = RGB(255, 0, 0) 行导致错误:“无法设置 Interior 类的颜色属性”。我猜我不应该在 .Activechart 下调用 .SeriesCollection。有什么建议么?在这一点上,我很高兴能够将图表的背景颜色更改为白色,并将 x 轴标签旋转 45 度。
提前谢谢你。
【问题讨论】:
-
那个 VBA 看起来非常接近 VBScript... 你试过吗:用 objExcel.ActiveChart
结尾? -
如果您使用的是 VBScript,则不能使用任何常量,例如
xlCategory:Excel VBA 知道这些值是什么,但它们在 VBScript 中不可用。您可以在对象浏览器中找到数值(VBEditor 中的 F2) -
... 或直接输入
?xlCategory并在即时窗口中按 Enter。
标签: excel vba vbscript colors charts