VBA子程序(宏)获取一个范围内的正值和和的数量?

时间:2016-12-12 13:54:23

标签: vba excel-vba macros excel

有人可以帮我解决我的VBA任务吗?写这样的子程序对我来说有点复杂。我需要一个子程序,它从任何大小的范围中找到正值(开始单元格被称为"开始")并将正值的总和和数量打印到称为" sum&#的单元格中34;和"数字"。

enter image description here

1 个答案:

答案 0 :(得分:1)

试试这个:

Sub sum_positive()
Dim area
Dim total As Double
Dim quantity As Integer

'Select the range of the area you want to test
Range("begin").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select

area = Selection

'Do a loop for each item you select to discover who is positive
For Each n In area
    If n > 0 Then
        total = total + n
        quantity = quantity + 1
    End If
Next n

'Return the values
Range("J1").Value = total

Range("L1").Value = quantity

End Sub