如果行中的值满足特定条件,如何对列中的值求和?

时间:2019-05-06 15:51:48

标签: excel vba

如果同一行中的值> x,则我尝试对列中的值求和。 X是一个任意值。

当前,代码会重复累加同一列。

我尝试使用ActiveCell.Row函数,但这取决于我选择的行,并且我希望它自动更新。

下面的代码检查第3行末尾的值是否符合条件。如果这样做,则应将单元格C3中的值添加到变量ThresholdCount。

    Option Explicit

    Sub Test()

    Dim ws As Worksheet 'declare your worksheets

    Set ws = ThisWorkbook.Sheets("Arrivals") 'like this you will refer to it using ws

    Dim cel As Range, unit As Range
    Dim ParcelCount As Long, LowerParcelCount As Long, ThresholdCount As Long

    With ws 'you could also use With ThisWorkbook.Sheets("Arrivals") if you are not using that sheet anymore
        ParcelCount = .Range("BW8").Value
        LowerParcelCount = .Range("BW5").Value
        ThresholdCount = 0

        For Each cel In .Range("BQ3:BQ78")
            If cel.Value > LowerParcelCount Then
                For Each unit In .Range("C3:C78")
                    ThresholdCount = ThresholdCount + unit.Value
                Next unit
            End If
        Next cel
        .Range("BS16") = ThresholdCount
    End With

End Sub

代码应检查第3行末尾的值是否符合条件。如果是这样,则应将单元格C3中的值添加到变量ThresholdCount。然后,它应检查第4行末尾的值是否符合条件。如果是这样,则应将单元格C4中的值添加到变量ThresholdCount。

如果范围BQ3:BQ78中的值满足条件,iT会继续在C列中添加值。

0 个答案:

没有答案