通过组循环并添加总和公式

问题描述 投票:0回答:1

我有多个组和子组的结构,其中子组级别可以超过10.类似于此(这是级别4)。

enter image description here

现在我想在每个组中总结或做一些计算,最后我想要第1组中的总数。

我希望总和类似于所示图像中的总和,将添加每个组,然后计算最终值。行或组的数量对我来说是可变的,所以我需要通过VBA添加公式,在我有输入(行和组)之后。

提前致谢。

excel vba excel-vba excel-formula
1个回答
0
投票

这就是我想要的......

    'Update assembly levels
            For i = 10 To 1 Step -1
                Call UpdateAssemblyLevel(RowCount, i)
            Next i

    Sub UpdateAssemblyLevel(RCount As Integer, CurrentLevel As Integer)

    'Adding Formulae for assembly levels

    Dim MassRange As Range
    Dim AssemblyMass As Integer
    Dim MR As Range           
    Set MassRange = Nothing        
    AssemblyMass = 0

        On Error Resume Next
        For i = RCount To 2 Step -1

            If Cells(i, 1).Value = CurrentLevel Then        
                Set MR = Cells(i, 12)
                MsgBox (MR.Value)
                    If Not MassRange Is Nothing Then
                        Set MassRange = Union(MassRange, MR)
                    Else
                        Set MassRange = MR
                    End If

            ElseIf Cells(i, 1).Value = CurrentLevel - 1 Then
                    'Add the Sum here
                    If MassRange Is Nothing Then
                        'Do Nothing
                    Else       
                        Cells(i, 12).Formula = "=SUM(" & MassRange.Address & ")" 'This gives me wrong value then desired output                            
                        Set MassRange = Nothing
                    End If

                AssemblyMass = 0

            Else
                'Do nothing
            End If        

        Next i

    End Sub
© www.soinside.com 2019 - 2024. All rights reserved.