excel:对象变量或未设置块变量

excel:对象变量或未设置块变量,excel,vba,Excel,Vba,我在第三行遇到这个错误。有人知道为什么吗 这是完整的代码。请记住,这是excel 2003 Dim cht As Chart Dim Xleft As Double, Ytop As Double Xleft = cht.PlotArea.InsideLeft 您需要将cht设置为图表对象 根据上面的代码进行编辑,我想如果你选择一个图表,你会发现你的问题消失了 Sub DrawSmoothTransparentShapesOnRadarChart() Dim cht As Char

我在第三行遇到这个错误。有人知道为什么吗

这是完整的代码。请记住,这是excel 2003

Dim cht As Chart
Dim Xleft As Double, Ytop As Double
Xleft = cht.PlotArea.InsideLeft

您需要将cht设置为图表对象

根据上面的代码进行编辑,我想如果你选择一个图表,你会发现你的问题消失了

Sub DrawSmoothTransparentShapesOnRadarChart()
      Dim cht As Chart
      Set cht = Worksheets(1).ChartObjects(1)

      Dim srs As Series
      Dim iSrs As Long
      Dim Npts As Integer, Ipts As Integer
      Dim myShape As Shape
      Dim Xnode As Double, Ynode As Double
      Dim Rmax As Double, Rmin As Double
      Dim Xleft As Double, Ytop As Double
      Dim Xwidth As Double, Yheight As Double
      Dim dPI As Double
      Dim iFillColor As Long
      Dim iLineColor As Long

      Set cht = ActiveChart
      Xleft = cht.PlotArea.InsideLeft
      Xwidth = cht.PlotArea.InsideWidth
      Ytop = cht.PlotArea.InsideTop
      Yheight = cht.PlotArea.InsideHeight
      Rmax = cht.Axes(2).MaximumScale
      Rmin = cht.Axes(2).MinimumScale
      dPI = WorksheetFunction.Pi()

      For iSrs = 1 To cht.SeriesCollection.Count

        Set srs = cht.SeriesCollection(iSrs)

        Select Case srs.ChartType
          Case xlRadar, xlRadarFilled, xlRadarMarkers

            Npts = srs.Points.Count

            Xnode = Xleft + Xwidth / 2 * _
                (1 + (srs.Values(Npts) - Rmin) / (Rmax - Rmin) _
                * Sin(2 * dPI * (Npts - 1) / Npts))

            Ynode = Ytop + Yheight / 2 * _
                (1 - (srs.Values(Npts) - Rmin) / (Rmax - Rmin) _
                * Cos(2 * dPI * (Npts - 1) / Npts))

            With cht.Shapes.BuildFreeform _
                (msoEditingAuto, Xnode, Ynode)
              For Ipts = 1 To Npts

                Xnode = Xleft + Xwidth / 2 * _
                    (1 + (srs.Values(Ipts) - Rmin) / (Rmax - Rmin) _
                    * Sin(2 * dPI * (Ipts - 1) / Npts))

                Ynode = Ytop + Yheight / 2 * _
                    (1 - (srs.Values(Ipts) - Rmin) / (Rmax - Rmin) _
                    * Cos(2 * dPI * (Ipts - 1) / Npts))

                .AddNodes msoSegmentLine, msoEditingAuto, _
                    Xnode, Ynode
              Next
              Set myShape = .ConvertToShape
            End With

            For Ipts = 1 To Npts
              myShape.Nodes.SetEditingType 3 * Ipts - 2, msoEditingSmooth
            Next

            Select Case iSrs
              Case 1
                iFillColor = 44
                iLineColor = 12
              Case 2
                iFillColor = 45
                iLineColor = 10
              Case 3
                iFillColor = 43
                iLineColor = 17
            End Select

            With myShape
              .Fill.ForeColor.SchemeColor = iFillColor
              .Line.ForeColor.SchemeColor = iLineColor
              .Line.Weight = 1.5
              .Fill.Transparency = 0.5
            End With
        End Select
      Next

    End Sub

它说无法获取工作表类的chartobjects属性工作表1上有图表吗?您可以将1更改为工作表的名称和图表的名称,例如Sheet1。谢谢。我没有图表。我加了一张图表。现在它说同一行上的类型不匹配您使用的是哪种图表类型?我试过两个版本的Excel,它都能正常工作。为了实验起见,比如说,从头开始创建一个条形图数据,然后再试一次。
Sub DrawSmoothTransparentShapesOnRadarChart()
      Dim cht As Chart
      ''Delete this line, or comment it:
      ''Set cht = Worksheets(1).ChartObjects(1)

      Dim srs As Series
      Dim iSrs As Long
      Dim Npts As Integer, Ipts As Integer
      Dim myShape As Shape
      Dim Xnode As Double, Ynode As Double
      Dim Rmax As Double, Rmin As Double
      Dim Xleft As Double, Ytop As Double
      Dim Xwidth As Double, Yheight As Double
      Dim dPI As Double
      Dim iFillColor As Long
      Dim iLineColor As Long

      ''This line set cht equal to the selected chart
      Set cht = ActiveChart