Sorting Data

FALLSILENT

New Member
Joined
Oct 5, 2017
Messages
9
I'm trying to use this table to feed to a second sheet that will break down how many times customers had each unit, and how many times of what type. Everything I've done has resulted in counting every instance of a name. Sometimes a customer has a unit for a few days, sometimes just a day. I tried a pivot table, but if CUST 1 had the unit first, every customer after fell into a sub-group of that first.

UNITTYPE10/1/1710/1/1710/2/1710/2/1710/3/17
12V1223BOX
CUST 1CUST 1CUST 2CUST 3CUST 4
15V1245BOXCUST 1CUST 1CUST 2CUST 3CUST 4
16V1250BOXCUST 1CUST 1CUST 2CUST 3CUST 4
15F1244FLATBEDCUST 1CUST 1CUST 2CUST 4CUST 4
7751408FLATBEDCUST 1CUST 1CUST 2CUST 3CUST 4
7751458REEFERCUST 1CUST 1CUST 2CUST 3CUST 4
142525SADCUST 1CUST 1CUST 2CUST 3CUST 4
7751404SADCUST 1CUST 1CUST 2CUST 3CUST 4
133515TADCUST 1CUST 1CUST 2CUST 3CUST 4
163536TADCUST 1CUST 1CUST 2CUST 3CUST 4

<colgroup><col><col><col span="5"></colgroup><tbody>
</tbody>
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
I pasted your table above into range A1:G11. This code assumes that there isn't anything below that data, so you will likely need to adjust the ranges and the array that holds the initial table data. Let me know if you have any questions about how to do that. But what this does is to change the way the data is presented so that you can make a pivot table that will give you the results you are looking for.

Code:
Sub Unwind()

Dim AR()
Dim Res(1 To 51, 1 To 4)
Dim Cnt As Long
Dim r As Range
Cnt = 2
AR = Range("A1:G11").Value
Res(1, 1) = "Customer"
Res(1, 2) = "Unit"
Res(1, 3) = "Type"
Res(1, 4) = "Date"
For i = 2 To UBound(AR)
    For j = 3 To UBound(AR, 2)
        Res(Cnt, 1) = AR(i, j)
        Res(Cnt, 2) = AR(i, 1)
        Res(Cnt, 3) = AR(i, 2)
        Res(Cnt, 4) = AR(1, j)
        Cnt = Cnt + 1
    Next j
Next i
Set r = Range("A15").Resize(UBound(Res), 4)
r.Value = Res()

End Sub
 
Last edited:
Upvote 0
When you have your workbook open, Hit Ctrl+F11. This will open the VBA editor. Then hit Ctrl+I+M, or go to Insert-->Module. This will bring up a new blank module. This is where you will paste the code. Then, you can either hit F5 to run the code, or run it from the workbook itself by hitting alt+F8 and selecting the module you want to run. I would suggest doing this on a test copy of your workbook because there is no Undo when running code like this.
 
Upvote 0
This is an optimized version of the code. With your original sheet activated, run the following code. It will add a sheet titled "Results" and it will dump your pivot table ready data into that sheet.

Code:
Sub Unwind()Dim ws As Worksheet
Dim AR()
Dim Res()
Dim Cnt As Long
Dim r As Range
Dim d As Range
Dim LR As Long


LR = Range("A" & Rows.Count).End(xlUp).Row
Cnt = 2
Set d = Range("C1:G" & LR)
ReDim Res(1 To d.Cells.Count, 1 To 4)
Set d = Range("A1").CurrentRegion
AR = d.Value


Res(1, 1) = "Customer"
Res(1, 2) = "Unit"
Res(1, 3) = "Type"
Res(1, 4) = "Date"
For i = 2 To UBound(AR)
    For j = 3 To UBound(AR, 2)
        Res(Cnt, 1) = AR(i, j)
        Res(Cnt, 2) = AR(i, 1)
        Res(Cnt, 3) = AR(i, 2)
        Res(Cnt, 4) = AR(1, j)
        Cnt = Cnt + 1
    Next j
Next i


Set ws = ActiveWorkbook.Sheets.Add(After:=Sheets(ActiveWorkbook.Sheets.Count))
ws.Name = "Results"


Set r = ws.Range("A1").Resize(UBound(Res), 4)
r.Value = Res()


End Sub
 
Upvote 0
I can't get this to work, I'm sure it'll work, I just need to figure out what I'm doing wrong on my end. Ctrl-F11 just opens a Macro Sheet for me.
 
Upvote 0
That should be Alt F11 not Ctrl F11
 
Upvote 0
Did you paste the code lrobbo314 supplied into the module?
 
Upvote 0

Forum statistics

Threads
1,214,944
Messages
6,122,391
Members
449,080
Latest member
Armadillos

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