Forum Discussion

fakewen's avatar
fakewen
Copper Contributor
Apr 24, 2023

What's the easiest way to split a list into a specific number of rows and columns?

For example I have a column with the numbers 1-20, and I want to split it into 3 rows by 4 columns, then the reminder of the rows with 3 columns, while maintaining a horizontal order:

 

1234
5678
9101112
131415
161718
1920 

 

Except the list would be much longer and need to adjust the number of rows and columns constantly. In situations like this, would it be easier to write a macros, or use multiple OFFSET formula (but have to adjust the formula each time)?

Thanks in advance

  • Lorenzo's avatar
    Lorenzo
    Silver Contributor
    =IFNA(
      VSTACK(
        WRAPROWS(TAKE(A1:A20,12),4),
        WRAPROWS(DROP(A1:A20,12),3)
      ),
      ""
    )
  • Lorenzo's avatar
    Lorenzo
    Silver Contributor

    fakewen 

    Another option with Power Query with your values formatted as Table (named Table1 below)

     

    let
        Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
        FromRows = (list as list, columns as number) as table =>
            Table.Combine(
                List.Transform(
                    List.Split(list, columns),
                    each Table.FromRows({_})
                )
            ),
    
        Result = Table.Combine(
            {
                FromRows(Table.FirstN(Source, 12)[Values], 4),
                FromRows(Table.Skip(Source, 12)[Values], 3)
            }
        )
    in
        Result

    Benefit: the blank cells are really empty (null)

Resources