【问题标题】:"Unable to get the Slope property of the WorksheetFunction class " Error“无法获取 WorksheetFunction 类的 Slope 属性”错误
【发布时间】:2017-10-07 00:43:18
【问题描述】:

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 列末尾,Assay Range 从 H18 列到 H 末尾。
  3. 只有 H 列 >2 和
  4. F 和 H 列的数据是十进制数据类型。 关于错误来自哪里的任何想法?

【问题讨论】:

  • 你的代码很难理解。我想我需要用工作表上的数据来解释意图。
  • 此数据来自反应测量。每 5 秒记录一次测量值。记录的值在 H 列中转换为产品浓度。经过时间在 F 列中计算。数据来自第 18 列。从上一步开始,我计算了单元格 B15 上数据的行号,用于计算 LastRow 。
  • 我不知道在哪里添加图片。有人可以帮忙吗?
  • 使用帮助。介绍添加图片的方法。
  • 谢谢 Dy.Lee。图片已加载。请把“表”放在上面。这是表格的截图。

标签: excel vba


【解决方案1】:

试试这个。

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

【讨论】:

  • 谢谢。现在可以了。我会尝试找出我的旧编码有什么问题。
  • Cells(GraphStart, "F") 不正确。应该是 ws.Cells(GraphStart, "F")
  • 我只看到了这个。非常感谢你,Dy.Lee。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-22
  • 2013-10-17
  • 2015-06-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多