Link to home
Start Free TrialLog in
Avatar of Gordon Hughes
Gordon HughesFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Grouping and totalling in Excel

Is there a system way of adding a line after each group of cost center codes and totalling across the spreadsheet
See attached spreadsheet
The spreadsheet is compiled from data in a database and could alter each run
Maintenance-Costs-Summery-V1.xlsx
Avatar of Rob Henson
Rob Henson
Flag of United Kingdom of Great Britain and Northern Ireland image

Look at using the Subtotal feature on the Data ribbon.

The data will need headers added so that the wizard can identify columns.

Thanks
Rob
The spreadsheet is compiled from data in a database and could alter each run

Because of this, you should not alter the DB extract with formulas.

So make the DB extract a sheet that you can download at will.
Then make a report sheet with formulas to extract the data and sum them.

That is how I would approach this problem. It will take you a bit of time to do this.
File attached with Subtotals added.

The subtotal wizard normally adds grouping so that you can expand or collapse groups or to a particular level but this doesn't seem to have happened on this occasion, don't know why.
Copy-of-Maintenance-Costs-Summery-V.xlsx
ASKER CERTIFIED SOLUTION
Avatar of Rob Henson
Rob Henson
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Here is VBA approach if you prefer:
Sub GroupAndSum()
Dim Ws As Worksheet, RepWs As Worksheet
Dim LR As Long, LC As Long, RowIndex As Long, FinalRow As Long
Set Ws = Worksheets("Sheet1")
Set RepWs = Worksheets("Report")
Application.ScreenUpdating = False
RepWs.Columns("A:G").EntireColumn.Delete
LR = Ws.Cells(Rows.Count, "B").End(xlUp).Row
LC = Ws.Cells(3, Columns.Count).End(xlToLeft).Column
Ws.Range(Cells(3, 2), Ws.Cells(3, LC)).Copy
RepWs.Range("A1").PasteSpecial xlPasteValues
Ws.Range("B3:B" & LR).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=RepWs.Range("A1"), Unique:=True
RepWs.Activate
FinalRow = RepWs.Range("A" & Rows.Count).End(xlUp).Row
For RowIndex = 2 To FinalRow
    cformula = WorksheetFunction.SumIf(Ws.Range("B:B"), RepWs.Cells(RowIndex, "A").Value, Ws.Range("C:C"))
    dformula = WorksheetFunction.SumIf(Ws.Range("B:B"), RepWs.Cells(RowIndex, "A").Value, Ws.Range("D:D"))
    eformula = WorksheetFunction.SumIf(Ws.Range("B:B"), RepWs.Cells(RowIndex, "A").Value, Ws.Range("E:E"))
    fformula = WorksheetFunction.SumIf(Ws.Range("B:B"), RepWs.Cells(RowIndex, "A").Value, Ws.Range("F:F"))
    gformula = WorksheetFunction.SumIf(Ws.Range("B:B"), RepWs.Cells(RowIndex, "A").Value, Ws.Range("G:G"))
    hformula = WorksheetFunction.SumIf(Ws.Range("B:B"), RepWs.Cells(RowIndex, "A").Value, Ws.Range("H:H"))
    RepWs.Cells(RowIndex, "B").Value = cformula
    RepWs.Cells(RowIndex, "C").Value = dformula
    RepWs.Cells(RowIndex, "D").Value = eformula
    RepWs.Cells(RowIndex, "E").Value = fformula
    RepWs.Cells(RowIndex, "F").Value = gformula
    RepWs.Cells(RowIndex, "G").Value = hformula
Next RowIndex
RepWs.Range("A1:G1").Font.Bold = True
With RepWs.Range("A1:G1")
    .HorizontalAlignment = xlCenter
    .VerticalAlignment = xlCenter
End With
RepWs.Range("B2:G" & FinalRow).NumberFormat = "#,##0.00"
RepWs.Columns.AutoFit
RepWs.Range("A1").Select
Application.ScreenUpdating = True
End Sub

Open in new window

In attach just click Group & Sum, your expected result would be in Report Sheet.
Maintenance-Costs-Summery-V3.xlsm
Avatar of Gordon Hughes

ASKER

The pivot table option is the best for me
Thanks to all for your input
Gordon