1

我使用以下代码定义了一个命名:

ActiveWorkbook.ActiveSheet.Names.Add Name:="BCLabel", RefersToR1C1:= _
    "=OFFSET(R46C3,1,0,COUNTA(R46C3:R69C3)-2)"
ActiveWorkbook.ActiveSheet.Names("BCLabel").Comment = ""

此代码对多个工作表重复,因此我有许多名为 BCLabel 的命名范围,尽管这指的是不同的范围。例如

=OFFSET('January'!$C$46,1,0,COUNTA('January'!$C$46:$C$69)-2)
=OFFSET('February'!$C$46,1,0,COUNTA('February'!$C$46:$C$69)-2)
=OFFSET('March'!$C$46,1,0,COUNTA('March'!$C$46:$C$69)-2)

我想在每个引用正确命名范围的工作表中编写图表的创建。我尝试了以下代码:

ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlColumnClustered
ActiveChart.Name = "BCChart"
ActiveChart.SeriesCollection(1).Values = _
    "==BCLabel"
ActiveChart.SeriesCollection(2).Values = _
    "==BCLabel"

但是这不起作用:“==BCLabel”没有引用正确的命名范围,“==ActiveSheet.BCLabel”也没有。我将不胜感激任何帮助。

顺便说一句,我还遇到了 ActiveChart.Name = "BCChart" 行的错误。

谢谢您的帮助!

4

1 回答 1

0

尝试使用以下代码(这对我有用):

ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlColumnClustered
ActiveChart.SeriesCollection(1).Values = Range("BCLabel")
ActiveChart.SeriesCollection(2).Values = Range("BCLabel")

顺便说一句,最好避免SelectActive...声明。所以,我重写了你的代码如下:

Sub test()
    Dim ws as Worksheet
    Dim sh As Shape

    Set ws = Worksheets("Sheet1")
    Set sh = ws.Shapes.AddChart

    With sh.Chart
        .ChartType = xlColumnClustered
        .SeriesCollection(1).Values = ws.Range("BCLabel")
        .SeriesCollection(1).Values = ws.Range("BCLabel")
    End With

End Sub

关于行内错误ActiveChart.Name = "BCChart",您可以在本文中看到,它是图表对象的只读属性

于 2014-01-15T18:44:11.373 回答