Forum Discussion
Move repeating columns into rows
- Dec 29, 2025
The prior answers show how to restructure the data using Power Query but to your question of keeping the comments that are in the cells when reformatting the structure I'm pretty sure you would have to use a custom macro for that. I created one here and will try to attach a sample file next:
Sub reformat_table() Application.ScreenUpdating = False Dim in_table, out_cell As Range Dim repeating_starting_column, repeating_count As Integer Dim ws As Worksheet Set ws = ActiveSheet Set in_table = ws.Range(InputBox("Select table range with repeating columns", "Input Table Range", Selection.Address)) Set out_cell = ws.Range(InputBox("Select cell for where output should be placed", "Output Table Location")) repeating_starting_column = 0 + InputBox("Column number where repeat begins", "Repeat Start") repeating_count = 0 + InputBox("Number of Columns that repeat", "Repeat Count") in_table.Range(in_table.Cells(0, 0), in_table(0, repeating_starting_column + repeating_count - 2)).Copy (out_cell.Cells(1, 1)) k = 2 For i = 1 To in_table.Rows().Count - 1 For j = repeating_starting_column - 1 To in_table.Columns().Count - 1 Step repeating_count If (in_table.Cells(i + 1, j + 1).Value2 <> "") Then in_table.Range(in_table.Cells(i, 0), in_table(i, repeating_starting_column - 2)).Copy (out_cell.Cells(k, 1)) in_table.Range(in_table.Cells(i, j), in_table(i, j + repeating_count - 1)).Copy (out_cell.Cells(k, 0 + repeating_starting_column)) k = k + 1 End If Next j Next i Application.ScreenUpdating = True End SubIf you highlight the source table before running the macro it will default that address range, then it will ask for the destination (cell), then the column where the first of the repeated columns starts (e.g. 2 in this example), and finally the number of columns that get repeated (e.g. again 2 in this example).
The prior answers show how to restructure the data using Power Query but to your question of keeping the comments that are in the cells when reformatting the structure I'm pretty sure you would have to use a custom macro for that. I created one here and will try to attach a sample file next:
Sub reformat_table()
Application.ScreenUpdating = False
Dim in_table, out_cell As Range
Dim repeating_starting_column, repeating_count As Integer
Dim ws As Worksheet
Set ws = ActiveSheet
Set in_table = ws.Range(InputBox("Select table range with repeating columns", "Input Table Range", Selection.Address))
Set out_cell = ws.Range(InputBox("Select cell for where output should be placed", "Output Table Location"))
repeating_starting_column = 0 + InputBox("Column number where repeat begins", "Repeat Start")
repeating_count = 0 + InputBox("Number of Columns that repeat", "Repeat Count")
in_table.Range(in_table.Cells(0, 0), in_table(0, repeating_starting_column + repeating_count - 2)).Copy (out_cell.Cells(1, 1))
k = 2
For i = 1 To in_table.Rows().Count - 1
For j = repeating_starting_column - 1 To in_table.Columns().Count - 1 Step repeating_count
If (in_table.Cells(i + 1, j + 1).Value2 <> "") Then
in_table.Range(in_table.Cells(i, 0), in_table(i, repeating_starting_column - 2)).Copy (out_cell.Cells(k, 1))
in_table.Range(in_table.Cells(i, j), in_table(i, j + repeating_count - 1)).Copy (out_cell.Cells(k, 0 + repeating_starting_column))
k = k + 1
End If
Next j
Next i
Application.ScreenUpdating = True
End SubIf you highlight the source table before running the macro it will default that address range, then it will ask for the destination (cell), then the column where the first of the repeated columns starts (e.g. 2 in this example), and finally the number of columns that get repeated (e.g. again 2 in this example).
- m_tarlerDec 29, 2025Bronze Contributor
here is the file. (if this message board will allow it)
note the macro is kinda slow but it works (at least for me it did)
- John5Dec 30, 2025Copper Contributor
Hey m_tarler,
Sorry for the late reply. I got caught up in my work and kind of putting my project to the side.
I'm new to this website so I couldn't find where you attach the file 😅 but I tried simulating it myself and it worked wonders!
I haven't tried it in a larger set of data, but the macro's working the way exactly I was looking for.
Much thanks,
John