简体   繁体   English

有条件的多张纸上的平均值

[英]Average across multiple sheets with a condition

My Excel Workbook has several sheets. 我的Excel工作簿有几张纸。 Each sheet has in cell B1 a product name. 每张纸在cell B1都有一个产品名称。

Also, each sheet has in cells B4 to B100 lots of numbers. 此外,每个工作表在cells B4 to B100都有很多数字。

I would like to get the average over all sheets. 我想获得所有表的平均值。 Here is what I have so far: 这是我到目前为止的内容:

=AVERAGE(Sheet1:Sheet20!B4:B100)

Now this works fine, however, I would now also like to have an extra condition: Instead of getting the average over all sheets, I only want to get the average over those sheets which meet a certain product name (cell B1). 现在,这很好用,但是,我现在还想有一个附加条件:我不想获取所有表的平均值,而只想获取满足特定产品名称的那些表的平均值(单元格B1)。 If the product name is different from the specified one, the whole sheet should not be taken into consideration for the average. 如果产品名称与指定名称不同,则平均值不应考虑整张表。

Is there any simple way of doing this? 有没有简单的方法可以做到这一点?

This performs the average if the B1 contains Apfel : 如果B1包含Apfel,则执行平均值:

Sub AboveAverage()
    Dim zum As Double, kount As Long
    Dim i As Long, r As Range, tAv As Double
    zum = 0
    kount = 0
    For i = 1 To 20
        With Sheets("Sheet" & i)
        If .Range("B1") = "Apfel" Then
            For j = 4 To 10000
                v = .Cells(j, "B").Value
                If v <> "" Then
                    zum = zum + v
                    kount = kount + 1
                End If
            Next j
        End If
        End With
    Next i
    tAv = zum / kount
    MsgBox tAv
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM