Excel 如何仅计算特定单元格中的平均值?

Excel 如何仅计算特定单元格中的平均值?,excel,vba,Excel,Vba,我有一张根据产品代码分类汇总的清单。我试图通过此函数进行平均,但最终选择了“0.0”值,这会对平均值产生不利影响。我需要排除它们。另外,我想知道是否可以进行LastRow计数,直到它以cell.Style=“Note”格式到达第一个单元格 可以使用数组公式 选项显式 次级测试平均值() 暗淡的迈塞尔山脉 变暗rng0 As范围 变暗rngTemp作为范围 作为射程的暗变焦 我想我会坚持多久 最后一排一样长 设置rng0=范围(“S:S”)。特殊单元格(xlCellTypeVisible)'列“S

我有一张根据产品代码分类汇总的清单。我试图通过此函数进行平均,但最终选择了“0.0”值,这会对平均值产生不利影响。我需要排除它们。另外,我想知道是否可以进行LastRow计数,直到它以cell.Style=“Note”格式到达第一个单元格

可以使用数组公式
选项显式
次级测试平均值()
暗淡的迈塞尔山脉
变暗rng0 As范围
变暗rngTemp作为范围
作为射程的暗变焦
我想我会坚持多久
最后一排一样长
设置rng0=范围(“S:S”)。特殊单元格(xlCellTypeVisible)'列“S”中的所有可见单元格'
LastRow=范围(“N”和Rows.Count).End(xlUp).Row'最后一项的行号
'循环通过列“S”中的每个可见单元格'
'我们使用“注释”样式连接所有范围
对于rng0中的每个迈塞尔
如果MyCell.Style=“Note”,则
如果不是,那么rngNotes什么都不是
设置rngNotes=Union(rngNotes,MyCell)
其他的
设置rngNotes=MyCell
如果结束
如果结束
'一旦我们超过最后一项(当前单元格行>最后一行)
'添加此单元格,使其具有“注释”样式
如果MyCell.Row>LastRow,则
如果不是,那么rngNotes什么都不是
设置rngNotes=Union(rngNotes,MyCell)
其他的
设置rngNotes=MyCell
如果结束
退出
如果结束
下一个迈塞尔
我们有一系列带有“Notes”风格的独立单元格
这就是为什么我问所有的“音符”是否至少有一行分开
不过,我们不能像往常一样在它的细胞中循环
相反,我们会在每个区域进行循环
'范围内至少有两个单元格(第一个“Note”单元格和“LastRow+1”单元格)
对于i=1到rngNotes.Areas.Count-1
'当前范围介于当前“注释”样式单元格和下一个“注释”样式单元格或“LastRow+1”单元格之间
设置rngTemp=范围(rngNotes.区域(i).单元(1).偏移量(1,0)_
rngNotes.面积(i+1).单元格(1).偏移量(-1,0))
“因为我们不能平均排除单元格,所以我们使用自定义公式
'由1和0组成的数组(其中0是未填满的条件,不等于“0”)
'乘以项目范围中的值数组
'然后除以不等于“0”的值的数目
rngNotes.Areas(i).Cells(1).FormulaArray=_
=总和((“&rngTemp.Address&“0)*”&rngTemp.Address&“)/SUM(“&rngTemp.Address&“0)*”1)
接下来我
端接头

我没有测试它,因为我没有任何合适的数据,但它在理论上应该可以工作。

注释行后面是否总是至少有一个非注释行?是的,每个独特的产品都有一个“注释”行。甚至一个产品本身也有“注释”行。萨缪尔巴伦补充道:“我认为这会让事情变得更容易。”
  Sub Average_Completed()

    Dim MyCell As Range
    Dim rng0, rng1 As Range
    Set rng0 = Range("R:R").SpecialCells(xlCellTypeVisible)


    LastRow = .Range("N" & .Rows.Count).End(xlUp).Row
    'is it possible to set LastRow to first cell with Cell.Style = "Note"?

       For Each MyCell In rng0
         If Cell.Style = "Note" Then
              For i = MyCell To LastRow
                MyCell.Application.WorksheetFunction.AverageIf _
               MyCell.Offset(-4, 0).Value = i, rng0.Value)
         End If
       Next Cell

    End Sub
Option Explicit

Sub TestingAverage()

    Dim MyCell As Range
    Dim rng0 As Range
    Dim rngTemp As Range
    Dim rngNotes As Range
    Dim i As Long
    Dim LastRow As Long

    Set rng0 = Range("S:S").SpecialCells(xlCellTypeVisible) ' All visible cells in column "S"
    LastRow = Range("N" & Rows.Count).End(xlUp).Row ' Row number of the last item

    ' Looping through each visible cell in columns "S"
    ' We concatenate all ranges with "Notes" style
    For Each MyCell In rng0
        If MyCell.Style = "Note" Then
            If Not rngNotes Is Nothing Then
                Set rngNotes = Union(rngNotes, MyCell)
            Else
                Set rngNotes = MyCell
            End If
        End If
        ' Once we are past last item (current cell row > last row)
        ' add this cell like it has "Notes" style
        If MyCell.Row > LastRow Then
            If Not rngNotes Is Nothing Then
                Set rngNotes = Union(rngNotes, MyCell)
            Else
                Set rngNotes = MyCell
            End If
            Exit For
        End If
    Next MyCell
    ' We got a range of separate cells with "Notes" style
    ' This is why I asked if all "Notes" are parted with at least 1 row
    ' We can't loop through it's cells as usual though

    ' We loop through each of it's areas instead
    ' We have at least two cells in range (first "Note" cell and the "LastRow+1" cell)
    For i = 1 To rngNotes.Areas.Count - 1
        ' Current range is between current "Notes" style cell and next "Notes" style cell OR the "LastRow+1" cell
        Set rngTemp = Range(rngNotes.Areas(i).Cells(1).Offset(1, 0), _
                            rngNotes.Areas(i + 1).Cells(1).Offset(-1, 0))
        ' Since we can't exclude cells in AVERAGE we use custom formula instead
        ' Array of 1s and 0s (where 0s are unfullfilled condition not to be equal "0")
        ' is multiplied by array of values in item range
        ' it is then divided by number of values not equal "0"
        rngNotes.Areas(i).Cells(1).FormulaArray = _
            "=SUM((" & rngTemp.Address & "<>0)*" & rngTemp.Address & ")/SUM((" & rngTemp.Address & "<>0)*1)"
    Next i


End Sub