解决运行时424错误; ActiveChart.SetSourceData

问题描述

下午好,

初学者使用2002年编写的旧版宏。Macro只是编译来自不同工作簿的数据,并使用图表创建一个文件。从Excel 2003升级到现在的PC已经引起了一些错误,这些错误大部分已经得到解决。最后剩下的问题之一是构建图表的子程序。初始错误(1004)发生在该行

ActiveChart.SetSourceData Source:=Sheets("Data").Range(_
PlotCells),PlotBy:=XlColumns

经过一些研究后,我在这里找到了信息... https://social.msdn.microsoft.com/Forums/zh-CN/ce290e29-634b-4383-9bb5-fddda8db3974/excel-macro-not-able-to -setsourcedata-for-chart?forum = isvvba 导致我将该行更改为

ActiveChart.SetSourceData Source:=PlotCells,PlotBy:=xlColumns

不再发生1004错误,但现在已将其替换为424错误。不确定是什么原因,因为我相信我已正确调整了所有维度的大小并将数据从一个子传递到另一个。调试时,PlotCells包含一个值。完整子帖子发布在下面。

Sub Build_Chart(ByVal File_Path As String,ByVal NumFreq As String,ByVal NumTemp As String,ByRef FreqV() As String)
    '
    ' Macro6 Macro
    ' Macro recorded 1/11/02 by Edward P. Smith
    '
    ' Making the Plot

        
    Dim X_Column,FCell,PlotCells,ii,tt,Freq,Cell,W_Book
    
    
    X_Column = "J"
    FCell = NumFreq + 3
    PlotCells = X_Column & FCell & ","
        
    Workbooks.Open Filename:=File_Path
    W_Book = ActiveWorkbook.Name
        
    'Add the Chart
    Charts.Add
    ActiveChart.ChartType = xlXYScatterSmoothNoMarkers
        
    For ii = 1 To NumFreq
        'Get the values to Plot
    
        For tt = 1 To NumTemp - 2
            If Not tt = NumTemp - 2 Then
                PlotCells = PlotCells & X_Column & (FCell + tt * (NumFreq + 1)) & ","
            Else
                PlotCells = PlotCells & X_Column & (FCell + tt * (NumFreq + 1))
            End If
        Next tt
        If ii = 1 Then
            ActiveChart.SetSourceData Source:=PlotCells,PlotBy:=xlColumns
                ActiveChart.Location Where:=xlLocationAsNewSheet
        Else
            Sheets("Data").Select
            Range(PlotCells).Select
            Selection.copy
            Sheets("Chart1").Select
            ActiveChart.PlotArea.Select
            ActiveChart.SeriesCollection.Paste Rowcol:=xlColumns,SeriesLabels:=False,_
                CategoryLabels:=False,Replace:=False,NewSeries:=True
        End If
        
        FCell = FCell + 1
        PlotCells = X_Column & FCell & ","
    Next ii
        
    ' Title the Chart and Axis
    
    With ActiveChart
        .HasTitle = True
        .ChartTitle.Characters.Text = "PPM Change"
        .Axes(xlCategory,xlPrimary).HasTitle = True
        .Axes(xlCategory,xlPrimary).AxisTitle.Characters.Text = "Temperature"
        .Axes(xlValue,xlPrimary).HasTitle = True
        .Axes(xlValue,xlPrimary).AxisTitle.Characters.Text = "PPM"
    End With
    
    ' Name the plot series
    
    For ii = 1 To NumFreq
        ActiveChart.SeriesCollection(ii).Name = "=""" & MyRound(FreqV(ii - 1),2) & " GHz"""
    Next ii
      
    ' Set the X-Axis values to the temperatures
    For ii = 1 To NumFreq
        ActiveChart.SeriesCollection(ii).XValues = _
            "=Data!R" & ((NumFreq + 1) * NumTemp) + 4 & "C1:R" & (((NumFreq + 1) * NumTemp) + 3) + (NumTemp - 1) & "C1"
    Next ii
        
    ' Set the Scale to our default values
    ActiveChart.Axes(xlValue).Select
    With ActiveChart.Axes(xlValue)
        .MinimumScale = -1000
        .MaximumScale = 2000
        .MinorUnitIsAuto = True
        .MajorUnitIsAuto = True
        .Crosses = xlCustom
        .CrossesAt = -1000
        .ReversePlotOrder = False
        .ScaleType = xlLinear
    End With
    
    ActiveChart.Axes(xlCategory).Select
    
    With ActiveChart.Axes(xlCategory)
        .MinimumScaleIsAuto = True
        .MaximumScaleIsAuto = True
        .MinorUnitIsAuto = True
        .MajorUnitIsAuto = True
        .Crosses = xlCustom
        .CrossesAt = -1000
        .ReversePlotOrder = False
        .ScaleType = xlLinear
    End With

    ActiveChart.PlotArea.Select
    
    With Selection.Border
        .ColorIndex = 16
        .Weight = xlThin
        .Linestyle = xlContinuous
    End With
    Selection.Interior.ColorIndex = xlNone

    Windows(W_Book).Activate
    ActiveWorkbook.Save
    
    'MyDelay 20
    
    ActiveWindow.Close


End Sub

任何帮助表示赞赏。谢谢。

解决方法

最初的1004“对象'_Worksheet'的方法'范围'失败”错误可能是由于PlotCells的长度超过范围可以接受的最大字符数(255)引起的。

由于Source期望Range对象不是字符串,因此引发424“需要对象”错误。

如果Len( PlotCells )> 255,则必须使用Union()来建立范围。