在VBA Excel中的图表中命名系列集合时出错

时间:2013-01-31 11:03:52

标签: excel vba excel-vba excel-2007

我在Excel VBA中遇到了一些问题。当我尝试运行以下代码时;它说:

Error 1004:Unable to get the name property of the series class!

此错误发生在以下代码行中:(产品早先定义为字符串Zyvox及其他代码)

If namec.Name = product Then

我该怎么办?我尝试了其他类似的东西

   ActiveChart.SeriesCollection(p).name = product

但无济于事!请帮忙!

Public Sub chartBlue()

Dim i As Integer, j As Integer, p As Integer, namec As Series


For i = 4 To Sheets.Count
    Sheets(i).Activate
    For j = 1 To Sheets(i).ChartObjects.Count
        Sheets(i).ChartObjects(j).Activate
        ActiveChart.ChartArea.Select


        For p = 1 To ActiveChart.SeriesCollection.Count
        Set namec = ActiveChart.SeriesCollection(p)


            If namec.Name = product Then
               namec.Border.ColorIndex = 5

            End If

        Next p
    Next j
  '  Windows(Reportname).Activate
    Sheets(i).Range("A1").Activate
Next i

End Sub

1 个答案:

答案 0 :(得分:2)

Amit,我在示例工作表上运行了您的代码,它运行正常!唯一缺少的是变量product的声明和设置,但是你提到你做到了。因此,首先要确保将product声明为字符串并具有内容。然后,在行中设置断点

If namec.Name = product Then
并运行代码。检查namec.Nameproduct的内容,看看它的不同之处