I would like to easily transform table data to a different table format without using any pivot table. I would like to do this with excel VBA, so with the press of a button I could get the desired result, but I don't know enough how to code this. Any help is appreciated.
Column E to Q contains the sizes (36 untill 48)
Please see screenshot below which shows example of input data and output table below:
I have this code, but it doesn't quite do yet what I want:
Sub TESTexample()
Dim lastrow As Integer
lastrow = Cells(Rows.Count, 1).End(xlUp).Row
Dim lastcol As Integer
lastcol = Cells(1, Columns.Count).End(xlToLeft).Column
Dim r As Integer
Dim c As Integer
For r = lastrow To 2 Step -1
For c = lastcol To 3 Step -1If Cells(r, c) <> "" Then
Rows(r + 1).Insert
Cells(r + 1, 1) = Cells(r, 1)
Cells(r + 1, 2) = Cells(r, c)
Cells(r, c).Clear
Else: Rows(r).Delete
End If
Next
Next
End Sub
EDIT: I found the solution, following code works:
Sub Button1_Click()
Dim lastrow As Integer
lastrow = Cells(Rows.Count, 1).End(xlUp).Row
Dim lastcol As Integer
lastcol = Cells(1, Columns.Count).End(xlToLeft).Column
Dim r As Integer
Dim c As Integer
For r = lastrow To 2 Step -1
For c = lastcol To 7 Step -1
If Cells(r, c) <> "" Then
Rows(r + 5).Insert
Cells(r + 5, 1) = Cells(1, c)
Cells(r + 5, 2) = Cells(r, 1)
Cells(r + 5, 3) = Cells(r, 2)
Cells(r + 5, 4) = Cells(r, 3)
Cells(r + 5, 5) = Cells(r, 5)
Cells(r + 5, 6) = Cells(r, c)
Cells(r, c).Clear
'Else: Rows(r).Delete
End If
Next
Next
End Sub
Kind regards, PJ