VBA code for summing values of multiple categories in same column

FrenchCelt

Board Regular
Joined
May 22, 2018
Messages
214
Office Version
  1. 365
Platform
  1. Windows
Hello, I'm trying to build a macro that will sum the values from Column E based on multiple categories in Column D. Column D is comprised of various job functions (e.g. RECEIVE PALLET, CUTTER, and LETDOWN REACH) and Column E is comprised of the quantity completed for those job functions. In doing this manually, I filter by job function and sum the number of quantity completed for that function, but I would prefer to run a macro to do this for me. Does anyone have any suggestions? I figure all I need is a template for one of the job functions like RECEIVE PALLET and then I can customize for the rest of them.
 
See if this macro does what you want. It will calculate for all job functions.
Code:
Sub SumCells()
    With Application
         .Calculation = xlCalculationManual
         .ScreenUpdating = False
    End With
    Dim LastRow As Long
    LastRow = Sheets("cognos").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Dim x As Long: x = 26
    Dim rngUniques As Range, JF As Range
    Range("L26:M" & LastRow).ClearContents
    Range("D1:D" & LastRow).AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=Range("D1:D" & LastRow), Unique:=True
    Set rngUniques = Range("D2:D" & LastRow).SpecialCells(xlCellTypeVisible)
    If Sheets("cognos").FilterMode Then Sheets("cognos").ShowAllData
    For Each JF In rngUniques
        Range("A1:J" & LastRow).AutoFilter Field:=2, Criteria1:=">" & Date - 45
        Range("A1:J" & LastRow).AutoFilter Field:=4, Criteria1:=JF
        If Range("A2", Cells(Rows.Count, "A").End(xlUp)).SpecialCells(xlCellTypeVisible).Cells(1, 1).Row > 1 Then
            If WorksheetFunction.CountA(Range("E2:E" & LastRow).SpecialCells(xlCellTypeVisible)) > 0 Then
                Range("L" & 25 + rngUniques.Count) = "New Hire"
                Range("L" & 26 + rngUniques.Count) = "Job Function"
                Range("M" & 26 + rngUniques.Count) = "Qty Complete"
                Cells(Rows.Count, "L").End(xlUp).Offset(1, 0) = JF
                Cells(Rows.Count, "M").End(xlUp).Offset(1, 0) = WorksheetFunction.Sum(Range("E2:E" & LastRow).SpecialCells(xlCellTypeVisible))
            End If
        End If
        If ActiveSheet.AutoFilterMode Then ActiveSheet.AutoFilterMode = False
        Range("A1:J" & LastRow).AutoFilter Field:=2, Criteria1:="<=" & Date - 45
        Range("A1:J" & LastRow).AutoFilter Field:=4, Criteria1:=JF
        If Range("A2", Cells(Rows.Count, "A").End(xlUp)).SpecialCells(xlCellTypeVisible).Cells(1, 1).Row > 1 Then
            If WorksheetFunction.CountA(Range("E2:E" & LastRow).SpecialCells(xlCellTypeVisible)) > 0 Then
                Range("L" & x) = JF
                Range("M" & x) = WorksheetFunction.Sum(Range("E2:E" & LastRow).SpecialCells(xlCellTypeVisible))
                x = x + 1
            End If
        End If
        If ActiveSheet.AutoFilterMode Then ActiveSheet.AutoFilterMode = False
    Next JF
    With Application
         .Calculation = xlCalculationAutomatic
         .ScreenUpdating = True
    End With
End Sub
 
Last edited:
Upvote 0

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
See if this macro does what you want. It will calculate for all job functions.
Code:
Sub SumCells()
    With Application
         .Calculation = xlCalculationManual
         .ScreenUpdating = False
    End With
    Dim LastRow As Long
    LastRow = Sheets("cognos").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Dim x As Long: x = 26
    Dim rngUniques As Range, JF As Range
    Range("L26:M" & LastRow).ClearContents
    Range("D1:D" & LastRow).AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=Range("D1:D" & LastRow), Unique:=True
    Set rngUniques = Range("D2:D" & LastRow).SpecialCells(xlCellTypeVisible)
    If Sheets("cognos").FilterMode Then Sheets("cognos").ShowAllData
    For Each JF In rngUniques
        Range("A1:J" & LastRow).AutoFilter Field:=2, Criteria1:=">" & Date - 45
        Range("A1:J" & LastRow).AutoFilter Field:=4, Criteria1:=JF
        If Range("A2", Cells(Rows.Count, "A").End(xlUp)).SpecialCells(xlCellTypeVisible).Cells(1, 1).Row > 1 Then
            If WorksheetFunction.CountA(Range("E2:E" & LastRow).SpecialCells(xlCellTypeVisible)) > 0 Then
                Range("L" & 25 + rngUniques.Count) = "New Hire"
                Range("L" & 26 + rngUniques.Count) = "Job Function"
                Range("M" & 26 + rngUniques.Count) = "Qty Complete"
                Cells(Rows.Count, "L").End(xlUp).Offset(1, 0) = JF
                Cells(Rows.Count, "M").End(xlUp).Offset(1, 0) = WorksheetFunction.Sum(Range("E2:E" & LastRow).SpecialCells(xlCellTypeVisible))
            End If
        End If
        If ActiveSheet.AutoFilterMode Then ActiveSheet.AutoFilterMode = False
        Range("A1:J" & LastRow).AutoFilter Field:=2, Criteria1:="<=" & Date - 45
        Range("A1:J" & LastRow).AutoFilter Field:=4, Criteria1:=JF
        If Range("A2", Cells(Rows.Count, "A").End(xlUp)).SpecialCells(xlCellTypeVisible).Cells(1, 1).Row > 1 Then
            If WorksheetFunction.CountA(Range("E2:E" & LastRow).SpecialCells(xlCellTypeVisible)) > 0 Then
                Range("L" & x) = JF
                Range("M" & x) = WorksheetFunction.Sum(Range("E2:E" & LastRow).SpecialCells(xlCellTypeVisible))
                x = x + 1
            End If
        End If
        If ActiveSheet.AutoFilterMode Then ActiveSheet.AutoFilterMode = False
    Next JF
    With Application
         .Calculation = xlCalculationAutomatic
         .ScreenUpdating = True
    End With
End Sub

Brilliant! Thanks!
 
Upvote 0

Forum statistics

Threads
1,216,111
Messages
6,128,898
Members
449,477
Latest member
panjongshing

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top