Excel VBA - 如果值存在> x,加上总和,但需要注意

时间:2017-04-25 15:50:24

标签: excel vba excel-vba

我上周使用以下代码协助搜索表格中的时间值大于91分钟,忽略第一个实例,然后对运行总计中的后续实例求和。我需要调整代码,以便在第一个实例大于95分钟时,忽略"忽略第一个实例"声明和总和。

目前的代码如下:

Dim ttlBTimes As Variant
Dim ttlBThrshldTime As Double
Dim ttlBSumResults() As Double
Dim ttlBResultIndex As Long
Dim ttlBi As Long, ttlBj As Long
Dim ttlBFirst As Boolean

ttlBThrshldTime = TimeValue("01:31:00")
ttlBTimes = ActiveSheet.Range("B3:F9").Value
ReDim ttlBSumResults(1 To UBound(ttlBTimes, 1) - LBound(ttlBTimes, 1) + 1, 1 To 1)
For ttlBi = LBound(ttlBTimes, 1) To UBound(ttlBTimes, 1)
    ttlBResultIndex = ttlBResultIndex + 1
    ttlBFirst = True
    For ttlBj = LBound(ttlBTimes, 2) To UBound(ttlBTimes, 2)
        If ttlBTimes(ttlBi, ttlBj) > ttlBThrshldTime Then
            If ttlBFirst Then
                ttlBFirst = False
            Else
                ttlBSumResults(ttlBResultIndex, 1) = ttlBSumResults(ttlBResultIndex, 1) + ttlBTimes(ttlBi, ttlBj) - ttlBThrshldTime
            End If
        End If
    Next ttlBj
Next ttlBi
ActiveSheet.Range("G3").Resize(UBound(ttlBSumResults, 1)).Value = ttlBSumResults

1 个答案:

答案 0 :(得分:0)

试试这个,95是01:35

Dim ttlBTimes As Variant
Dim ttlBThrshldTime As Double
Dim ttlBSumResults() As Double
Dim ttlBResultIndex As Long
Dim ttlBi As Long, ttlBj As Long
Dim ttlBFirst As Boolean

ttlBThrshldTime = TimeValue("01:35:00")
ttlBTimes = ActiveSheet.Range("B3:F9").Value
ReDim ttlBSumResults(1 To UBound(ttlBTimes, 1) - LBound(ttlBTimes, 1) + 1, 1 To 1)
For ttlBi = LBound(ttlBTimes, 1) To UBound(ttlBTimes, 1)
    ttlBResultIndex = ttlBResultIndex + 1
    For ttlBj = LBound(ttlBTimes, 2) To UBound(ttlBTimes, 2)
        If ttlBTimes(ttlBi, ttlBj) > ttlBThrshldTime Then
                ttlBSumResults(ttlBResultIndex, 1) = ttlBSumResults(ttlBResultIndex, 1) + ttlBTimes(ttlBi, ttlBj) - ttlBThrshldTime
        End If
    Next ttlBj
Next ttlBi
ActiveSheet.Range("G3").Resize(UBound(ttlBSumResults, 1)).Value = ttlBSumResults
excel vba excel-vba