Option Explicit
Sub ReorgData()
' hiker95, 01/07/204
' http://www.mrexcel.com/forum/excel-questions/747957-dataset-organisation.html
Dim oa As Variant, wr As Variant
Dim r As Long, lr As Long, rr As Long, i As Long, n As Long
Application.ScreenUpdating = False
With ActiveSheet
If .Range("B1") <> "WholesaleKP" Then
MsgBox "The 'active sheet' cell 'B1' does not contain 'WholesaleKP' - macro terminated!"
Exit Sub
End If
lr = .Cells(Rows.Count, 1).End(xlUp).Row
oa = .Range("A1:D" & lr)
.Columns(4).ClearContents
n = Application.Ceiling(lr, 4)
ReDim wr(1 To n / 4, 1 To 11)
With .Range("D2:D" & lr)
.FormulaR1C1 = "=LEFT(RC[-3],4)"
.Value = .Value
End With
For r = 2 To lr
n = Application.CountIf(.Columns(4), .Cells(r, 4).Value)
i = i + 1
For rr = r To r + n - 1
If InStr(.Cells(rr, 1), "Q1") Then
wr(i, 1) = .Cells(rr, 4)
wr(i, 2) = .Cells(rr, 2)
wr(i, 7) = .Cells(rr, 4)
wr(i, 8) = .Cells(rr, 3)
ElseIf InStr(.Cells(rr, 1), "Q2") Then
wr(i, 1) = .Cells(rr, 4)
wr(i, 3) = .Cells(rr, 2)
wr(i, 7) = .Cells(rr, 4)
wr(i, 9) = .Cells(rr, 3)
ElseIf InStr(.Cells(rr, 1), "Q3") Then
wr(i, 1) = .Cells(rr, 4)
wr(i, 4) = .Cells(rr, 2)
wr(i, 7) = .Cells(rr, 4)
wr(i, 10) = .Cells(rr, 3)
ElseIf InStr(.Cells(rr, 1), "Q4") Then
wr(i, 1) = .Cells(rr, 4)
wr(i, 5) = .Cells(rr, 2)
wr(i, 7) = .Cells(rr, 4)
wr(i, 11) = .Cells(rr, 3)
End If
Next rr
r = r + n - 1
Next r
.Range("A1:D" & lr) = oa
End With
If Not Evaluate("ISREF(Results!A1)") Then Worksheets.Add().Name = "Results"
With Sheets("Results")
.UsedRange.ClearContents
.Cells(1, 1).Resize(, 11) = Array("WholesaleKP", "Q1", "Q2", "Q3", "Q4", "", "RepairsKP", "Q1", "Q2", "Q3", "Q4")
.Cells(2, 1).Resize(UBound(wr, 1), UBound(wr, 2)) = wr
.Range("B2:E" & UBound(wr, 1)).NumberFormat = "_(* #,##0_);_(* (#,##0);_(* ""-""??_);_(@_)"
.Range("H2:K" & UBound(wr, 1)).NumberFormat = "_(* #,##0_);_(* (#,##0);_(* ""-""??_);_(@_)"
.Columns.AutoFit
.Activate
End With
Application.ScreenUpdating = True
End Sub