How to flatten a table in Excel

In most times of collecting and writing data tasks, you will use a spreadsheet software like Microsoft Excel. And you end up with a two dimensions table where each key has a dedicated column.

Of course, it is a simple table, easy to be read by humans. But if you want to transform it to a dataset or a list of keys/values, to be imported into a database system, then you must convert your 2D table to a flat table.

This tutorial will show you how to flatten a table in Excel, using VBA script :

Let’s take the table below as an example of 2D table :

CountryCapitalRegionPopulation
ChinaBeijingAsia1,411,778,724
IndiaNew DelhiAsia1,379,579,515
United StatesWashington, D.C.Americas332,040,249
IndonesiaJakartaAsia271,350,000
PakistanIslamabadAsia225,200,000

And we want to transform it to a flat table like the table below :

CountryKeyValue
ChinaCapitalBeijing
ChinaRegionAsia
ChinaPopulation1,411,778,724
IndiaCapitalNew Delhi
IndiaRegionAsia
IndiaPopulation1,379,579,515
United StatesCapitalWashington, D.C.
United StatesRegionAmericas
United StatesPopulation332,040,249
IndonesiaCapitalJakarta
IndonesiaRegionAsia
IndonesiaPopulation271,350,000
PakistanCapitalIslamabad
PakistanRegionAsia
PakistanPopulation225,200,000

To do this, open Excel and bring your data, in an existing workbook or it is better to put them in a new worksheet. And follow these steps :

  • Go to the “Developer” tab and click on the “Visual Basic” icon, or just use the keyboard shortcut (Alt + F11).
  • In the opened “Visual Basic for Applications”, go to the menu “Insert”, and select “Module”.
  • Now in the new editor window, just paste this code :
Sub ReversePivotTable()
'   Before running this, make sure you have a summary table with column headers.
'   The output table will have three columns.
    Dim SummaryTable As Range, OutputRange As Range
    Dim OutRow As Long
    Dim r As Long, c As Long

    On Error Resume Next
    Set SummaryTable = ActiveCell.CurrentRegion
    If SummaryTable.Count = 1 Or SummaryTable.Rows.Count < 3 Then
        MsgBox "Select a cell within the summary table.", vbCritical
        Exit Sub
    End If
    SummaryTable.Select
    Set OutputRange = Application.InputBox(prompt:="Select a cell for the 3-column output", Type:=8)
'   Convert the range
    OutRow = 2
    Application.ScreenUpdating = False
    OutputRange.Range("A1:C3") = Array("Column1", "Column2", "Column3")
    For r = 2 To SummaryTable.Rows.Count
        For c = 2 To SummaryTable.Columns.Count
            OutputRange.Cells(OutRow, 1) = SummaryTable.Cells(r, 1)
            OutputRange.Cells(OutRow, 2) = SummaryTable.Cells(1, c)
            OutputRange.Cells(OutRow, 3) = SummaryTable.Cells(r, c)
            OutputRange.Cells(OutRow, 3).NumberFormat = SummaryTable.Cells(r, c).NumberFormat
            OutRow = OutRow + 1
        Next c
    Next r
End Sub

  • Click the “Run” button in the toolbar.
  • A dialog box is opened now to choose a cell in your Excel’s workbook to show the result, go to Excel and choose a cell, whether in the same worksheet of your data or in a new worksheet, then click “OK”.
  • Now in the choosen cell, you will see a result like the following :
Column1Column2Column3
ChinaCapitalBeijing
ChinaRegionAsia
ChinaPopulation1,411,778,724
IndiaCapitalNew Delhi
IndiaRegionAsia
IndiaPopulation1,379,579,515
United StatesCapitalWashington, D.C.
United StatesRegionAmericas
United StatesPopulation332,040,249
IndonesiaCapitalJakarta
IndonesiaRegionAsia
IndonesiaPopulation271,350,000
PakistanCapitalIslamabad
PakistanRegionAsia
PakistanPopulation225,200,000

Leave a Reply