# How to Sum by Group in Excel

Sometimes we may have the requirement that subtotal data by group in a table. To just subtotal data in a column, we can use SUM function directly; for data in a column, amount data in another column, we can use SUMIF function to subtotal data. But to subtotal data in group, we need to classify data by group firstly, then subtotal data which belongs to the same group. Above all, to subtotal data by group, we need to use formula based on IF function and SUMIF function. IF function is used for confirm ‘if value in a cell equals to its cell above’, if yes, then SUMIF function will be triggered and returns the corresponding total amount. This tutorial will help you to know the method with simple description, screenshots and explanation clearly. You can refer to below example for understanding well.

See example below:

## 1. Sum by Group Using IF and SUMIF Functions

You can see that fruits are classified to different groups, if you want to subtotal data by group, do you know the proper formula in this instance?

In C2, enter the formula:

`` =IF(A2=A1," ",SUMIF(\$A\$2:\$A\$13,A2,\$B\$2:\$B\$13)).``

In this instance, we use IF function to return the subtotal value. We know that for IF function, =IF(logical_test, [value if true], [value if false]). In this formula, we use A2=A1 to classify data, if the two values are the same, nothing will be returned due to they belong to the same group, if they are different, that means a new group is detected, the SUMIF function will be used here.

And for SUMIF function =SUMIF(range, criteria, [sum range]), in this instance, we need to subtotal data by group, \$A\$2:\$A\$3 is an absolute range for criteria, each data in column A is a criteria, \$B\$2:\$B\$3 is an absolute range for matching values, so if we enter SUM(\$A\$2:\$A\$3,A2, \$B\$2:\$B\$3) here, excel will search A2 value ‘Apple’ in \$A\$2:\$A\$3, and find all ‘Apple’ in this range, then subtotal all matching values in \$B\$2:\$B\$3, then return SUMIF value in C2 finally.

Click Enter to get returned value. We can see that 400 is correct.

Drag down the cell to copy formula. We can see that subtotal is recorded in the first row of each group. That’s because in this row, the data in column A is different with its cell above, a new group is found.

Notes:

1. In this instance, it is easy to locate the beginning data and ending data in a column, if there are multiple rows in the table and you are hard to find the ending data, you can use the full column as reference. Then formula will update to =IF(A2=A1,” “,SUMIF(A:A,A2,B:B)), it does work.

2. If you want to record subtotal in the last tow of each group, just enter the formula in the last row of the first group, and copy cell for the following cells.

## 2. Sum by Group Using VBA Code

Now, let’s explore the second method—a more advanced approach involving VBA code.

Press Alt + F11 to open the Visual Basic for Applications (VBA) editor.

Right-click on any item in the Project Explorer.

Choose “Insert” and then “Module.”

Copy and paste the provided VBA code into the module.

``````Sub SumByGroup()
Dim lastRow As Long
Dim currentGroup As Variant
Dim sumValue As Double
Dim i As Long

' Assuming your groups are in column A and values in column B
lastRow = Cells(Rows.Count, 1).End(xlUp).Row

' Initialize sumValue
sumValue = 0

' Loop through each row
For i = 2 To lastRow
' Check if the group changes
If Cells(i, 1).Value <> currentGroup Then
' If yes, output the sumValue and reset for the new group
If i <> 2 Then Cells(i - 1, 3).Value = sumValue
currentGroup = Cells(i, 1).Value
sumValue = 0
End If

' Add the value to sumValue
sumValue = sumValue + Cells(i, 2).Value
Next i

' Output the sumValue for the last group
Cells(lastRow, 3).Value = sumValue
End Sub

``````

Close the VBA editor and run the macro using Alt + F8.

Select the macro named “SumByGroup” and click “Run

This code efficiently sums data by group using a macro.

## 3. Video: Sum by Group

This Excel Video tutorial where we explore two efficient methods: one using a formula with IF and SUMIF functions, and the other employing VBA code.

## 4. Related Functions

• Excel SUMIF Function
The Excel SUMIF function sum the numbers in the range of cells that meet a single criteria that you specify. The syntax of the SUMIF function is as below:=SUMIF (range, criteria, [sum_range])…
• Excel SUM function
The Excel SUM function will adds all numbers in a range of cells and returns the sum of these values. You can add individual values, cell references or ranges in excel.The syntax of the SUM function is as below:= SUM(number1,[number2],…)…
• Excel IF function
The Excel IF function perform a logical test to return one value if the condition is TRUE and return another value if the condition is FALSE. The IF function is a build-in function in Microsoft Excel and it is categorized as a Logical Function.The syntax of the IF function is as below:= IF (condition, [true_value], [false_value])….