在Excel中如何按组求和

时间:2018-10-11 13:17:33

标签: excel excel-vba excel-formula

这有点棘手。当值最大为500-800时,我想在同一组内进行多个求和(需要决定)。从第7到28行,我的产品类别为B1-2-2。在E列中,我正在说明我的工作。

在对D7:D14求和时,我得到595。然后我重新开始,对行求和,直到得到738,它仍然是同一类别(B1-2-2)。

从类别B1-1-2中可以看到,值大于500-800,并且它们仅返回自己的值。

我正在寻找一种开始的方式。 非常感谢

picture

3 个答案:

答案 0 :(得分:0)

似乎您正在尝试通过对相似产品类别的产品进行分组来求和,然后对其进行过滤以查看值是否大于500-800。

解决此问题的一种方法是创建一个数据透视表,其中您的两行将是产品类别,然后是product,然后您的值将是合计列。然后,您可以将结果复制并粘贴到另一个表中,然后可以按总数> 500-800进行过滤。

答案 1 :(得分:0)

旋转后,您将需要创建一个新列。因此,对于总计的每个总和,您都可以使用查找来创建范围。公式为=LOOKUP(A1,{0,"0-500";501,"500-800";10000,"800+"})

enter image description here

答案 2 :(得分:0)

VBA脚本无法完成任务

Sub calculateSum()
 Dim rng As Range
 Dim lastRow As Long, category As String, cost As Long, totalCost As Long, nextCost As Long
 Dim i As Integer, j As Integer
 
 Sheets(5).Activate
   
 lastRow = Range("A" & Rows.Count).End(xlUp).Row

 For i = 2 To lastRow
    category = Cells(i, 2)
    cost = Cells(i, 4)
    totalCost = totalCost + cost
    Cells(i, 5) = 0
    'Case : 1 Cost more than 800
    If totalCost > 800 Then
        Cells(i, 5) = cost
        totalCost = 0
    Else
    
        
        'Case : 2 Cost is less than 800
       
        If category = Cells(i + 1, 2) Then
           If (totalCost + Cells(i + 1, 4)) > 800 Then
           Cells(i, 5) = totalCost
           totalCost = 0
           End If
        Else
          Cells(i, 5) = totalCost
          totalCost = 0
        End If
        
    
    End If
    
 Next i

End Sub

相关问题