调用图表过程时无效的过程调用错误

时间:2013-04-27 07:01:44

标签: arrays vba charts

有人会猜到为什么我会收到“运行时错误5:无效的程序调用或参数? 图表工作正常,只是我得到了这个错误,我想摆脱它。

Option Explicit
Option Base 1

Sub dort9()

Dim cMin As Double
Dim cMax As Double
Dim lDer As Long
Dim NoIntervals As Long
Dim plaga() As Variant
Dim i As Long
Dim J As Long

Dim dest As Range
Dim A As Integer
Dim B As Integer
Dim rColumn() As Variant
Dim result() As Variant
ReDim result(1 To 4)
ReDim result2(1 To 4)


NoIntervals = 4
ReDim Preserve plaga(14)

    With Worksheets("Sheet1") 
        plaga = .Range(.Cells(1, 1), .Cells(14, 1)).Value
    End With

Call tri1(plaga)

cMin = WorksheetFunction.Min(plaga)
cMax = WorksheetFunction.Max(plaga)

Dim longInter As Double
longInter = (cMax - cMin) / NoIntervals

ReDim plaga2(1 To NoIntervals) As Long
ReDim arrIntervals(1 To NoIntervals)

Dim pla As Variant
Dim lCom As Long
Dim res As Variant

    For i = 1 To NoIntervals
        arrIntervals(i) = cMax - ((i - 1) * longInter)
    Next i

    For Each pla In plaga
        res = Application.Match(pla, arrIntervals, -1)
        plaga2(res) = plaga2(res) + 1
    Next pla


    result(1) = plaga2(1)

    For B = 2 To 4
        result(B) = (plaga2(B) + result(B - 1))
    Next B

    For A = 1 To 4
        result2(A) = WorksheetFunction.Round((result(A) / 14) * 100, 2) & " %"
    Next A

    Set dest = Worksheets("Sheet1").Range("D1")
    dest.Resize(4, 1).Value = Application.Transpose(result2)
    Call ChartNew2(result2)

End Sub

它出现在最后一个调用过程中,该过程调用图表来绘制数组中的值...第一个调用过程无关紧要,它工作正常。

Sub ChartNew2(result2 As Variant) 
Dim i As Integer
ReDim result2(1 To 4, 1 To 1)
Charts.Add

    For i = LBound(result2, 1) To UBound(result2, 1)
        result2(i, 1) = result2
    Next i
    ActiveChart.ChartType = xlXYScatterSmoothNoMarkers
    ActiveChart.Location Where:=xlLocationAsObject

    With ActiveChart
            .HasTitle = True
            .Axes(xlValue, xlPrimary).HasTitle = True
    End With


    With ActiveChart.Axes(xlValue)
                .HasMajorGridlines = True
    End With

    ActiveChart.HasLegend = False
    ActiveChart.PlotArea.Select

    Selection.Interior.ColorIndex = xlAutomatic
    ActiveChart.Axes(xlValue).Select

    With ActiveChart.Axes(xlValue)

            .MaximumScale = 1

    End With

End Sub

1 个答案:

答案 0 :(得分:0)

位置信息有' Where '参数和'名称'参数。 如果Where等于'xlLocationAsObject',那么Name是必需的。因此,您必须为Location方法调用指定Name。

Sub ChartNew2()

    ' Creates a new chart sheet and returns a Chart object
    Dim newChart As Variant
    Set newChart = ActiveWorkbook.Charts.Add

    With newChart
        ' Name is required if Where is xlLocationAsObject
        .Location Where:=xlLocationAsObject, Name:="Sheet1"
        ' ...
    End With
End Sub