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.

[TABLE="width: 758"]
<colgroup><col><col><col span="5"></colgroup><tbody>[TR]
[TD]UNIT[/TD]
[TD]TYPE[/TD]
[TD]10/1/17[/TD]
[TD]10/1/17[/TD]
[TD]10/2/17[/TD]
[TD]10/2/17[/TD]
[TD]10/3/17[/TD]
[/TR]
[TR]
[TD]12V1223[/TD]
[TD]BOX
[/TD]
[TD]CUST 1[/TD]
[TD]CUST 1[/TD]
[TD]CUST 2[/TD]
[TD]CUST 3[/TD]
[TD]CUST 4[/TD]
[/TR]
[TR]
[TD]15V1245[/TD]
[TD]BOX[/TD]
[TD]CUST 1[/TD]
[TD]CUST 1[/TD]
[TD]CUST 2[/TD]
[TD]CUST 3[/TD]
[TD]CUST 4[/TD]
[/TR]
[TR]
[TD]16V1250[/TD]
[TD]BOX[/TD]
[TD]CUST 1[/TD]
[TD]CUST 1[/TD]
[TD]CUST 2[/TD]
[TD]CUST 3[/TD]
[TD]CUST 4[/TD]
[/TR]
[TR]
[TD]15F1244[/TD]
[TD]FLATBED[/TD]
[TD]CUST 1[/TD]
[TD]CUST 1[/TD]
[TD]CUST 2[/TD]
[TD]CUST 4[/TD]
[TD]CUST 4[/TD]
[/TR]
[TR]
[TD]7751408[/TD]
[TD]FLATBED[/TD]
[TD]CUST 1[/TD]
[TD]CUST 1[/TD]
[TD]CUST 2[/TD]
[TD]CUST 3[/TD]
[TD]CUST 4[/TD]
[/TR]
[TR]
[TD]7751458[/TD]
[TD]REEFER[/TD]
[TD]CUST 1[/TD]
[TD]CUST 1[/TD]
[TD]CUST 2[/TD]
[TD]CUST 3[/TD]
[TD]CUST 4[/TD]
[/TR]
[TR]
[TD]142525[/TD]
[TD]SAD[/TD]
[TD]CUST 1[/TD]
[TD]CUST 1[/TD]
[TD]CUST 2[/TD]
[TD]CUST 3[/TD]
[TD]CUST 4[/TD]
[/TR]
[TR]
[TD]7751404[/TD]
[TD]SAD[/TD]
[TD]CUST 1[/TD]
[TD]CUST 1[/TD]
[TD]CUST 2[/TD]
[TD]CUST 3[/TD]
[TD]CUST 4[/TD]
[/TR]
[TR]
[TD]133515[/TD]
[TD]TAD[/TD]
[TD]CUST 1[/TD]
[TD]CUST 1[/TD]
[TD]CUST 2[/TD]
[TD]CUST 3[/TD]
[TD]CUST 4[/TD]
[/TR]
[TR]
[TD]163536[/TD]
[TD]TAD[/TD]
[TD]CUST 1[/TD]
[TD]CUST 1[/TD]
[TD]CUST 2[/TD]
[TD]CUST 3[/TD]
[TD]CUST 4[/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
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,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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