Count and Subtotal ColA based on ColB Then Total

LambVBA

New Member
Joined
Mar 28, 2014
Messages
16
Load
Order
12345
ABC123
23456
ABC000
34567
ABC000
45678
XYZ123

<tbody>
</tbody>

Hi everyone. I'm looking for an easy way to create a new couple of columns with the Distinct order # and next to it the total number of Loads. See example above and then the expected output below. Anyone know??

Load Order Distinct Order Number of Loads
12345 ABC123 ABC123 1
23456 ABC000 ABC000 2
34567 ABC000 XYZ123 1
45678 XYZ123
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
LambVBA,

1. What version of Excel and Windows are you using?

2. Are you using a PC or a Mac?


Sample raw data:


Excel 2007
ABCD
1LoadOrder
212345ABC123
323456ABC000
434567ABC000
545678XYZ123
6
Sheet1


After the macro:


Excel 2007
ABCD
1LoadOrderDistinct OrderNumber of Loads
212345ABC123ABC1231
323456ABC000ABC0002
434567ABC000XYZ1231
545678XYZ123
6
Sheet1


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

1. Copy the below code
2. Open your NEW workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.

Code:
Sub GetUniquesCount()
' hiker95, 10/30/2014, ME815188
Dim c As Range
With CreateObject("Scripting.Dictionary")
  .CompareMode = vbTextCompare
  For Each c In Range("B2", Range("B" & Rows.Count).End(xlUp))
    If c <> "" Then
      If Not .Exists(c.Value) Then
        .Add c.Value, 1
      Else
        .Item(c.Value) = .Item(c.Value) + 1
      End If
    End If
  Next
  Range("C1").Resize(, 2).Value = Array("Distinct Order", "Number of Loads")
  Range("C2").Resize(.Count, 2) = Application.Transpose(Array(.keys, .Items))
  Columns("C:D").AutoFit
End With
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

Then run the GetUniquesCount macro.
 
Upvote 0
I'm looking for an easy way ...
Have a look at Excel's in-built Pivot table feature, on the Insert ribbon tab.
It is designed exactly for that sort of requirement.

Excel Workbook
ABCDEF
1LoadOrderCount of Load
212345ABC123OrderTotal
323456ABC000ABC0002
434567ABC000ABC1231
545678XYZ123XYZ1231
6Grand Total4
PT
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,085
Messages
6,128,732
Members
449,465
Latest member
TAKLAM

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