Counting total quantity of same parts

coretex99

New Member
Joined
Feb 25, 2020
Messages
17
Office Version
  1. 2016
Platform
  1. Windows
Hello, I've been tasked with counting the total quantity of items we have, however many in the spreadsheet are duplicates and It cannot be done manually. For example

NumberQty
002/242
1400​
003/20
500​
003/20
690​
004/100
1816​
004/00
9​
004/00
250​
004/00
20000​

I have set it to A-Z on Number, for example 004/00 total is 20259, any advice is muchly appreciated, thanks
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
try with below,
VBA Code:
Sub Unique_Values_Sumif()
'vba run or not confirmation
    Dim chkMsg As String, chkAns As Variant
    chkMsg = "This will generate the unique values/id in new sheet column A & get the total sum of last column of the slected range in new sheet colum B."
    chkAns = MsgBox(chkMsg, vbYesNo)
    Select Case chkAns
        Case vbYes
 'vba run or not confirmation
'--------------------------------------------------------------
'code here

Application.DisplayAlerts = False
Dim tb As Worksheet
Dim tb2 As Worksheet
Dim actvSheet As String

 actvSheet = ActiveSheet.Name
 'Set tb = Sheets("sheet1")
 'Set tb2 = Sheets("sheet2")
 Set tb = Sheets(actvSheet)
 
  'Dim lrow As Long
  Dim rSelection As Range, lColNo As Integer, formulalColNo As Integer, fColNo As Integer, formulafColNo As Integer
  'lrow = tb.Cells(tb.Rows.Count, 1).End(xlUp).Row
  'tb.Range("A2:A" & lrow).Copy tb2.Range("A2")
  'Check that a range is selected
  If Selection.Columns.count < 2 Then
    MsgBox "Please select a range first, you have slected only 1 column, column must be at least 2", vbOKOnly, "Selecton Check"
    Exit Sub
  End If
 
  'Store the selected range
  Set rSelection = Selection
  fColNo = rSelection.Column
  formulafColNo = fColNo - 2
  lColNo = rSelection(rSelection.count).Column
  formulalColNo = lColNo - 2
 
  Set tb2 = Worksheets.Add(Type:=xlWorksheet, After:=Application.ActiveSheet)
  rSelection.Copy tb2.Range("A3")

On Error Resume Next
With tb2
    .Range("A3:A5000").SpecialCells(xlCellTypeBlanks).Delete Shift:=xlShiftUp
    .Range("B1:Z5000").ClearContents
    .Range("A3").CurrentRegion.RemoveDuplicates 1
    With .Range(.Range("B3"), .Range("A3").End(xlDown).Offset(, 1))
        '.FormulaR1C1 = "=SUMIF(Sheet1!C[-1]:C[-1],RC[-1],Sheet1!C:C)"
        .FormulaR1C1 = "=SUMIF('" & actvSheet & "'!C[" & formulafColNo & "]:C[" & formulafColNo & "],RC[-1],'" & actvSheet & "'!C[" & formulalColNo & "]:C[" & formulalColNo & "])"
    End With
     'Delete column
    '.Range("C1:Z1").EntireColumn.Delete
     'Autofit column
    .Columns("A").AutoFit
    .Range("B1").Formula = "=SUM(B3:B5000)"
    .Range("A1").Formula = "=COUNTA(A3:A5000)"
End With
Application.DisplayAlerts = True

Done:
Exit Sub
'code here
'-----------------------------------------------------------------
'vba run or not confirmation
    Case vbNo
        GoTo Quit:
    End Select
Quit:
'vba run or not confirmation
End Sub
 
Upvote 0
try with below,
VBA Code:
Sub Unique_Values_Sumif()
'vba run or not confirmation
    Dim chkMsg As String, chkAns As Variant
    chkMsg = "This will generate the unique values/id in new sheet column A & get the total sum of last column of the slected range in new sheet colum B."
    chkAns = MsgBox(chkMsg, vbYesNo)
    Select Case chkAns
        Case vbYes
'vba run or not confirmation
'--------------------------------------------------------------
'code here

Application.DisplayAlerts = False
Dim tb As Worksheet
Dim tb2 As Worksheet
Dim actvSheet As String

actvSheet = ActiveSheet.Name
'Set tb = Sheets("sheet1")
'Set tb2 = Sheets("sheet2")
Set tb = Sheets(actvSheet)

  'Dim lrow As Long
  Dim rSelection As Range, lColNo As Integer, formulalColNo As Integer, fColNo As Integer, formulafColNo As Integer
  'lrow = tb.Cells(tb.Rows.Count, 1).End(xlUp).Row
  'tb.Range("A2:A" & lrow).Copy tb2.Range("A2")
  'Check that a range is selected
  If Selection.Columns.count < 2 Then
    MsgBox "Please select a range first, you have slected only 1 column, column must be at least 2", vbOKOnly, "Selecton Check"
    Exit Sub
  End If

  'Store the selected range
  Set rSelection = Selection
  fColNo = rSelection.Column
  formulafColNo = fColNo - 2
  lColNo = rSelection(rSelection.count).Column
  formulalColNo = lColNo - 2

  Set tb2 = Worksheets.Add(Type:=xlWorksheet, After:=Application.ActiveSheet)
  rSelection.Copy tb2.Range("A3")

On Error Resume Next
With tb2
    .Range("A3:A5000").SpecialCells(xlCellTypeBlanks).Delete Shift:=xlShiftUp
    .Range("B1:Z5000").ClearContents
    .Range("A3").CurrentRegion.RemoveDuplicates 1
    With .Range(.Range("B3"), .Range("A3").End(xlDown).Offset(, 1))
        '.FormulaR1C1 = "=SUMIF(Sheet1!C[-1]:C[-1],RC[-1],Sheet1!C:C)"
        .FormulaR1C1 = "=SUMIF('" & actvSheet & "'!C[" & formulafColNo & "]:C[" & formulafColNo & "],RC[-1],'" & actvSheet & "'!C[" & formulalColNo & "]:C[" & formulalColNo & "])"
    End With
     'Delete column
    '.Range("C1:Z1").EntireColumn.Delete
     'Autofit column
    .Columns("A").AutoFit
    .Range("B1").Formula = "=SUM(B3:B5000)"
    .Range("A1").Formula = "=COUNTA(A3:A5000)"
End With
Application.DisplayAlerts = True

Done:
Exit Sub
'code here
'-----------------------------------------------------------------
'vba run or not confirmation
    Case vbNo
        GoTo Quit:
    End Select
Quit:
'vba run or not confirmation
End Sub

Wow, that works straight up, thank you very much, amazed by how much code that takes!! Many thanks to you sir
 
Upvote 0
You could also just use a few clicks to implement Excel's built-in Pivot Table feature (on the Insert ribbon tab)

20 09 14.xlsm
ABCDE
1NumberQtySum of Qty
2002/2421400NumberTotal
3003/20500002/2421400
4003/20690003/201190
5004/1001816004/0020259
6004/009004/1001816
7004/00250Grand Total24665
8004/0020000
Total Qty
 
Upvote 0
Another Option is to use Power Query/Get and Transform.

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Qty", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Number"}, {{"Total", each List.Sum([Qty]), type nullable number}})
in
    #"Grouped Rows"

Book6
AB
1NumberTotal
2002/2421400
3003/201190
4004/1001816
5004/0020259
Sheet2
 
Upvote 0

Forum statistics

Threads
1,214,925
Messages
6,122,303
Members
449,078
Latest member
nonnakkong

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