Hi,
I've written below code to help transpose data tables according to attached picture, mainly because I love PTs and they are much more powerful when using only one column for data.
However, I'm pretty sure the code is suboptimal, mainly because I transcribed what I'd do manually instead of thinking of a more elegant solution.
How would you do it?
Best
I've written below code to help transpose data tables according to attached picture, mainly because I love PTs and they are much more powerful when using only one column for data.
However, I'm pretty sure the code is suboptimal, mainly because I transcribed what I'd do manually instead of thinking of a more elegant solution.
How would you do it?
Best
VBA Code:
Sub Verticalize_data()
Dim i As Integer, j As Integer, k As Integer, n As Integer
Dim rowmovefirst As Integer, rowmovelast As Integer, colcopyfirst As Integer, colcopynb As Integer, rowheaders As Integer, colvalheader As Integer
Application.ScreenUpdating = False
If MsgBox("This macro will run on the active worksheet, are you willing to continue?", 4) <> 6 Then Exit Sub
If MsgBox("Please make sure you allocated one blank column to receive the verticalized header value", 1) <> 1 Then Exit Sub
colvalheader = InputBox("Please give the rank/number of said column, eg 3 for col C", "Which column is that?")
rowheaders = InputBox("Please give the rank/number of said row", "Which row contains headers for the data?")
rowmovefirst = InputBox("Please give the rank/number of said row", "Which is the first row of the data table to modify?")
rowmovelast = InputBox("Please give the rank/number of said row", "Which is the last row of the data table to modify?")
colcopyfirst = InputBox("Please give the rank/number of said column, eg 3 for col C", "Which is the first column containing data to verticalize?")
colcopynb = InputBox("Please give the number of data columns we're turning into data lines", "How many columns are we copying in a single one")
For i = rowmovelast To rowmovefirst Step -1
For j = 0 To colcopynb - 2 Step 1
ActiveSheet.Rows(i + 1).Insert
Next j
For k = 0 To colcopynb - 1 Step 1
Cells(i + k, colvalheader).Value = Cells(rowheaders, colcopyfirst + k).Value
Cells(i + k, colcopyfirst).Value = Cells(i, colcopyfirst + k).Value
For n = 1 To colvalheader - 1 Step 1
Cells(i + k, n).Value = Cells(i, n).Value
Next n
Next k
Next i
Application.ScreenUpdating = True
End Sub