TOCOL

Short answer

Flatten a grid into one column

TOCOL(array, [ignore], [scan_by_column])
Excel version
2024+
Also searched as
unpivot formula, flatten range to column, budget matrix to list, grid to list, torow, convert table to one column

What it returns

TOCOL reads every cell of a range and returns them as a single column. A budget grid of 28 lines by 12 months becomes one column of 336 values.

By default it reads row by row: all twelve months of the first line, then all twelve of the second, and so on. Set scan_by_column to TRUE and it reads column by column instead: every line for January, then every line for February.

TOROW is the same function with a row as output. TOROW(array, [ignore], [scan_by_column]) takes the same arguments and returns one long row.

Neither returns labels on its own. Flattening the values is one step. Building matching columns for line and month is the part that makes the result useful, and it is covered in the worked example.

Arguments that matter

ArgumentRequiredWhat it does
arrayYesThe grid to flatten. Budget!D13:O40.
ignoreNo0 keeps everything (default). 1 skips blanks. 2 skips errors. 3 skips blanks and errors.
scan_by_columnNoFALSE reads across each row first (default). TRUE reads down each column first.

ignore is the dangerous one. Skipping blanks in the value column shortens it. If the line and month columns were built without the same skip, every row after the first blank pairs a value with the wrong label. The list still looks fine.

scan_by_column has to match across every column you build. Mix a by-row value column with a by-column label column and the pairing is wrong from the second row onward.

In a financial model

The budget usually arrives as a grid. Lines run down, months run across, which is right for a presentation tab and what time runs left to right asks for. A database load, a variance table keyed on line and month, or a SUMIFS that reads budget alongside actuals all want a list: one row per line per month.

That reshaping is an unpivot. The play on transforming in Power Query, not in cells is right that Power Query does it well for a source file. TOCOL does it for a grid that lives in the workbook and changes while you work, such as the budget the FP&A team is still editing. The list updates as they type.

Once the budget is a list with the same columns as the actuals extract, VSTACK can put the two in one block with a Type column, and every report reads one dataset.

TOROW shows up less. Its main use is turning a small block into a header row, such as the selected cost centers from a two-column picker. Unlike TRANSPOSE, it can skip blanks with ignore.

Worked example

A budget tab Budget with line items in B13:B40, month-end dates in D10:O10, and values in D13:O40.

The flat list, three columns, in A5 of a tab BudgetList:

=LET(
  lines, Budget!B13:B40,
  months, Budget!D10:O10,
  vals, Budget!D13:O40,
  lineCol, TOCOL(IF(SEQUENCE(1, COLUMNS(months)), lines)),
  monthCol, TOCOL(IF(SEQUENCE(ROWS(lines)), months)),
  valCol, TOCOL(vals),
  list, HSTACK(lineCol, monthCol, valCol),
  FILTER(list, valCol <> 0))

The two IF and SEQUENCE expressions broadcast the labels to the size of the grid: lines repeated across twelve columns, months repeated down 28 rows. TOCOL then reads all three grids in the same order, so row n of each column refers to the same cell. FILTER drops zero rows at the end, once, from all three columns together.

Grid (source)Jan-26Feb-26
Salaries48,00048,000
Rent12,0000
Output: LineMonthValue
Salaries31-Jan-2648,000
Salaries28-Feb-2648,000
Rent31-Jan-2612,000

Rent in February was zero, so it is filtered out. A SUMIFS keyed on line and month now reads the budget the same way it reads the ledger:

=SUMIFS(INDEX(BudgetList!A5#, 0, 3), INDEX(BudgetList!A5#, 0, 1), $B12, INDEX(BudgetList!A5#, 0, 2), D$10)

Old way vs new way

Before TOCOL, the formula unpivot was INDEX with INT and MOD, filled down 336 rows:

=INDEX(Budget!$D$13:$O$40, INT((ROWS($1:1)-1)/12)+1, MOD(ROWS($1:1)-1, 12)+1)

The new version of the value column:

=TOCOL(Budget!D13:O40)

The INT and MOD version hard-codes the width, has to be filled to the right length, and breaks when a month is added. TOCOL reads the grid’s own shape.

Where it breaks

Values sit against the wrong line items partway down the list. ignore was set to 1 on the value column but not on the label columns, so the value column is shorter and every row after the first blank is shifted. Fix: flatten all three columns with the default ignore of 0, then remove empty rows with one FILTER across the stacked result.

Lines and months stop matching after the first row. One column was built with scan_by_column set to TRUE and another without. Fix: use the same scan order for every TOCOL in the formula.

TOCOL returns a column of zeros at the end. The value range extends past the last line item into empty rows, and blanks flatten to 0. Fix: size the range to the grid, or filter out zeros as in the worked example.

HSTACK returns #N/A in the bottom rows. The label grids and the value grid are different sizes, because lines covers 28 rows and vals covers 30. Fix: define all three ranges from the same rows and columns, ideally with LET so each is written once.

Months come through as five-digit numbers. The dates flattened correctly, but the output column took General format. Fix: format the month column as a date.

TOCOL or TOROW returns #NAME?. The file opened in Excel 2021 or earlier. Fix: if the model must travel, unpivot in Power Query, which older versions can refresh.

Related functions

  • SUMIFS. Reads the flattened list by line and month, the same way it reads the ledger.
  • VSTACK. Stack the budget list under the actuals extract once both share the same columns.
  • SEQUENCE. Broadcasts labels to the size of the grid inside the unpivot.
  • FILTER. Removes zero or blank rows from the finished list, all columns at once.
  • LET. Keeps the three range definitions in one place, per LET instead of repeating a fragment.
  • UNIQUE. Pull the distinct lines or months back out of the list for a check.
  • TRANSPOSE. Swaps rows and columns without flattening. Use it to turn a column into a row when there are no blanks to skip.

All functions