“无法获取WorksheetFunction类的Slope属性”错误

时间:2017-10-05 20:29:40

标签: excel excel-vba vba

Table

Sub GraphData()
Dim GraphStart As Integer
Dim GraphEnd As Integer
Dim TimeRange As Range
Dim AssayRange As Range
Dim LastRow As Integer
Dim AssayTime As Date
Dim k As Integer
Dim m As Integer

LastRow = ActiveWorkbook.Worksheets("RawData").Range("B15").Value + 17

For k = 18 To LastRow
    If ActiveWorkbook.Worksheets("RawData").Range("H" & k).Value < 2 Then
       GraphStart = k + 1
    End If
Next

For m = 18 To LastRow
    If ActiveWorkbook.Worksheets("RawData").Range("H" & m).Value < 32 Then
        GraphEnd = m
    End If
Next

Set TimeRange = Application.Range(Cells(GraphStart, "F"), Cells(GraphEnd, "F"))
Set AssayRange = Application.Range(Cells(GraphStart, "H"), Cells(GraphEnd, "H"))


ActiveWorkbook.Worksheets("Assay Result").Range("D31").Value = Application.WorksheetFunction.Slope(AssayRange, TimeRange)

ActiveWorkbook.Worksheets("Assay Result").Range("D32").Value = (Application.WorksheetFunction.Correl(AssayRange, TimeRange)) ^ 2

ActiveWorkbook.Worksheets("Assay Result").Range("D33").Value = Round(Application.WorksheetFunction.Min(AssayRange), 2) & " to " & Round(Application.WorksheetFunction.Max(AssayRange), 2)

End Sub

我相信以前有人有这个问题。错误是不一致的,有时会发生。

  1. 计算斜率的数据范围正在变化。所以我为x和y系列设置了两个数据范围变量:TimeRange和AssayRange
  2. TimeRange数据从F18列到F列末尾,分析范围从H18列到H末尾。
  3. 只有H列&gt; 2和&lt; 32中的数据才会被选入范围并用于计算。
  4. F和H列中的数据是十进制数据类型。 有关错误来自哪里的任何想法?

1 个答案:

答案 0 :(得分:0)

试试这个。

Sub GraphData()
Dim GraphStart As Integer
Dim GraphEnd As Integer
Dim TimeRange As Range
Dim AssayRange As Range
Dim LastRow As Integer
Dim AssayTime As Date
Dim k As Integer
Dim m As Integer
Dim Ws As Worksheet, toWs As Worksheet
Dim Wf As WorksheetFunction

Set Wf = WorksheetFunction
Set Ws = Worksheets("RawData")
Set toWs = Worksheets("Assay Result")

    With Ws
        LastRow = .Range("B15").Value + 17

        For k = 18 To LastRow
            If .Range("H" & k).Value < 2 Then
               GraphStart = k + 1
            End If
        Next
        For m = 18 To LastRow
            If .Range("H" & m).Value < 32 Then
                GraphEnd = m
            End If
        Next

        Set TimeRange = .Range("F" & GraphStart, "F" & GraphEnd)
        Set AssayRange = TimeRange.Offset(, 2)
    End With
    With toWs
        .Range("d31") = Wf.Slope(AssayRange, TimeRange)
        .Range("d32") = Wf.Correl(AssayRange, TimeRange) ^ 2
        .Range("d33") = Round(Wf.Min(AssayRange), 2) & " to " & Round(Wf.Max(AssayRange), 2)
    End With

End Sub