Forum Discussion

John5's avatar
John5
Copper Contributor
Dec 28, 2025
Solved

Move repeating columns into rows

Hello guys,   I have a set of data that looks like this: Name Hours Date Hours Date Hours Date John 3 1-Jan 4 5-Jan     Ann 4 4-Jan 2 8-Jan 2 9-Jan   Each Hours d...
  • m_tarler's avatar
    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 Sub

    If 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).

Resources