Excel VBA 确定卡车装运量

Excel VBA Determining Mass of Truck Shipments

我有一个系统,其中有一个卡车衡的数据列表,读取秤上卡车的重量。该数据范围从 -30,000 磅左右(由于秤已去皮但没有卡车)到 40,000 磅(满载且已去皮的卡车)。我的任务是确定通过卡车离开我们工厂的总重量。问题是有些日子只有几辆卡车离开我们的工厂,而其他的则有十几辆离开,重量都略有不同。

这些权重的图形看起来像锯齿图案。它基本上是一个负值(由于皮重),当卡车驶上秤时,它会迅速达到大约零,然后慢慢增加到最终重量。达到最终重量后,随着卡车开走,重量很快回到负值。

我关于如何解决这个问题的想法是寻找数据小于零的位置和 return 传感器在零之间的最大重量。如果最大重量高于某个噪声滤波器值(例如 5000 磅),则将最大重量添加到某个计数器。理论上,还不错,实际上,有点不合我意。

这是我到目前为止的代码,因为我知道我需要展示我到目前为止的努力。我建议忽略它,因为它主要是重新开始工作几天后的失败启动。

Public Function TruckLoad(rngData As Range)
Dim intCount As Integer
intCount = 0
For Each cell In rngData
    intCount = intCount + 1
Next cell
Dim n As Integer
n = 1
Dim x As Integer
x = 1
Dim arr() As Double
For i = 1 To intCount
    If rngData(i, 1) < 0 Then
        arr(n) = x
        n = n + 1
        x = x + 1
    Else
        x = x + 1
    End If
Next
TruckLoad = arr(1)

End Function

如果有人能给我关于如何进行的建议,那将是非常有价值的。我不是最基础的计算机程序员。

编辑:抱歉,我一开始就应该说这个。我不能 post 完整的原始样本数据,但我可以 post 一张图表的照片。在某种程度上我不能 post 公开(并不是说你可以对数据做任何特别邪恶的事情,这是公司规定)。

www.imgur.com/a/LGQY9

我对数据的理解与罗宾的评论一致。有几种方法可以解决这个问题。我编写了一个函数循环遍历数据范围以查找数据集中的 'next zero',并计算当前行与 'next zero' 所在行之间的最大值。如果最大值是高于噪声过滤器的值,该值将添加到 运行 总数中。

Option Explicit

Private Const NOISE_FILTER As Double = 5000

Public Function TruckLoad(rngData As Range) As Double

    Dim r As Integer
    Dim runningTruckLoad As Double
    Dim maxLoadReading As Double
    Dim nextZeroRow As Integer

    For r = 1 To rngData.Rows.Count

        nextZeroRow = FindNextZeroRow(r, rngData)

        maxLoadReading = Application.WorksheetFunction.Max(Range(rngData.Cells(r, 1), rngData.Cells(nextZeroRow, 1)))

        If maxLoadReading > NOISE_FILTER Then
            runningTruckLoad = runningTruckLoad + maxLoadReading
        End If

        r = nextZeroRow 'skip the loop counter ahead to our new 0 row

    Next r

    TruckLoad = runningTruckLoad

End Function

Private Function FindNextZeroRow(startRow As Integer, searchRange As Range) As Integer

    Dim nextZeroRow As Range

    Set nextZeroRow = searchRange.Find(0, searchRange.Rows(startRow))

    If nextZeroRow.Row < startRow Then 'we've hit the end of the data range
        FindNextZeroRow = startRow
    ElseIf nextZeroRow.Value <> 0 Then  'we've found a data point with a zero in it, not interested in this row
        FindNextZeroRow = FindNextZeroRow(nextZeroRow.Row, searchRange)
    Else
        FindNextZeroRow = nextZeroRow.Row 'we've found our next zero data point
    End If

End Function