If the data is very large, then you may find this faster.
For the moment, this code puts the results off to the right of the original data, but could be put wherever you want.
I have also made some assumptions - see under the first screen shot below.
Do any testing on a
copy of your workbook.
Code:
Sub Rearrange()
Dim a As Variant, b As Variant
Dim i As Long, j As Long, k As Long
a = Range("A1").CurrentRegion.Value
ReDim b(1 To UBound(a, 1) * UBound(a, 2), 1 To 4)
For i = 3 To UBound(a, 1)
For j = 2 To UBound(a, 2) Step 2
k = k + 1
b(k, 1) = a(i, 1): b(k, 2) = a(1, j): b(k, 3) = a(i, j): b(k, 4) = a(i, j + 1)
Next j
Next i
With Range("A1").Offset(, UBound(a, 2) + 1).Resize(, 4)
.Offset(1).Resize(k).Value = b
.Value = Array("Date", "Product", "Qty", "Value")
.EntireColumn.AutoFit
End With
End Sub
Original data in columns A:E, results of the above code in columns G:J
Excel 2016 (Windows) 32 bit
| A | B | C | D | E | F | G | H | I | J |
---|
Product | | | | | | | | | | |
Product A | | | | | | | | | | |
Product B | | | | | | | | | | |
Product A | | | | | | | | | | |
Product B | | | | | | | | | | |
| | | | | | | | | | |
<colgroup><col style="width: 25pxpx"><col><col><col><col><col><col><col><col><col><col></colgroup><thead>
</thead><tbody>
[TD="align: center"]1[/TD]
[TD="align: right"]Date[/TD]
[TD="align: right"]Product A[/TD]
[TD="align: right"]Product A[/TD]
[TD="align: right"]Product B[/TD]
[TD="align: right"]Product B[/TD]
[TD="align: right"][/TD]
[TD="align: right"]Date[/TD]
[TD="align: right"]Qty[/TD]
[TD="align: right"]Value[/TD]
[TD="align: center"]2[/TD]
[TD="align: right"][/TD]
[TD="align: right"]Qty[/TD]
[TD="align: right"]Value[/TD]
[TD="align: right"]Qty[/TD]
[TD="align: right"]Value[/TD]
[TD="align: right"][/TD]
[TD="align: right"]1/09/2017[/TD]
[TD="align: right"]50[/TD]
[TD="align: right"]1000[/TD]
[TD="align: center"]3[/TD]
[TD="align: right"]1/09/2017[/TD]
[TD="align: right"]50[/TD]
[TD="align: right"]1000[/TD]
[TD="align: right"]40[/TD]
[TD="align: right"]800[/TD]
[TD="align: right"][/TD]
[TD="align: right"]1/09/2017[/TD]
[TD="align: right"]40[/TD]
[TD="align: right"]800[/TD]
[TD="align: center"]4[/TD]
[TD="align: right"]2/09/2017[/TD]
[TD="align: right"]60[/TD]
[TD="align: right"]1200[/TD]
[TD="align: right"]20[/TD]
[TD="align: right"]400[/TD]
[TD="align: right"][/TD]
[TD="align: right"]2/09/2017[/TD]
[TD="align: right"]60[/TD]
[TD="align: right"]1200[/TD]
[TD="align: center"]5[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"]2/09/2017[/TD]
[TD="align: right"]20[/TD]
[TD="align: right"]400[/TD]
[TD="align: center"]6[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
</tbody>
Sheet1
Assumptions:
1. There are no completely empty rows or columns within the data.
2. Original headings occupy 2 rows as shown above.
3. All rows of data occupy the same number of columns. That is, you don't have 'staggered' data like the sample below. (If you do, post back with details and the code can be modified.)
Excel 2016 (Windows) 32 bit
<colgroup><col style="width: 25pxpx"><col><col><col><col><col><col><col></colgroup><thead>
</thead><tbody>
[TD="align: center"]1[/TD]
[TD="align: right"]Date[/TD]
[TD="align: right"]Product A[/TD]
[TD="align: right"]Product A[/TD]
[TD="align: right"]Product B[/TD]
[TD="align: right"]Product B[/TD]
[TD="align: right"]Product C[/TD]
[TD="align: right"]Product C[/TD]
[TD="align: center"]2[/TD]
[TD="align: right"][/TD]
[TD="align: right"]Qty[/TD]
[TD="align: right"]Value[/TD]
[TD="align: right"]Qty[/TD]
[TD="align: right"]Value[/TD]
[TD="align: right"]Qty[/TD]
[TD="align: right"]Value[/TD]
[TD="align: center"]3[/TD]
[TD="align: right"]1/09/2017[/TD]
[TD="align: right"]50[/TD]
[TD="align: right"]1000[/TD]
[TD="align: right"]40[/TD]
[TD="align: right"]800[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: center"]4[/TD]
[TD="align: right"]2/09/2017[/TD]
[TD="align: right"]10[/TD]
[TD="align: right"]800[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: center"]5[/TD]
[TD="align: right"]3/09/2017[/TD]
[TD="align: right"]60[/TD]
[TD="align: right"]1200[/TD]
[TD="align: right"]20[/TD]
[TD="align: right"]400[/TD]
[TD="align: right"]80[/TD]
[TD="align: right"]200[/TD]
</tbody>
Sheet2