How to Flip Table in Excel (2 Quick Ways)

Although Excel offers no direct ways to do so, in this article we will demonstrate how to flip data by columns and rows using Excel’s built-in options and functions.


Method 1 – Flipping Table by Columns

Consider the following List of Sales Reps dataset, containing the Names of some sales reps and their Sales in USD. Let’s flip the columns.

 how to flip table in excel

We have used Microsoft Excel 365 version, but the methods should work in most other versions.


1.1 – Using Sort Option

The most common way to flip data is with the Sort option.

Steps:

  • Make a Helper Column and number it serially as shown in the picture below.

Using Sort Option

  • Go to the Data tab >> click the Sort button.

 how to flip table in excel

This opens the Sort window.

  • Tick the My data has headers option.
  • In the Sort by option, select the column heading Helper Column.
  • In the Order field, choose Largest to Smallest option.
  • Click OK.

Sorting Descending Order

This flips the table as shown in the image below.

how to flip table in excel with Sort option


1.2 – Using SORTBY Function

The SORTBY function sorts a range or array in ascending or descending order based on another given range or array.

Steps:

  • In cell E5 enter the formula below:

=SORTBY($B$5:$C$14,ROW(B5:B14),-1)

The B5:C14 cells refer to the Name and Sales values respectively.

Formula Breakdown:

  • SORTBY($B$5:$C$14,ROW(B5:B14),-1) → $B$5:$C$14 is the array argument that refers to the Names and Sales values. ROW(B5:B14) represents the by_array1 argument which returns the row numbers of the Names and Sales values. Lastly, -1 is the optional sort_order1 argument which indicates Descending order.
Note: The SORTBY function is available in Microsoft Excel 365. If you’re using an older version of Excel, then please check the next method.

Utilizing SORTBY Function

The output should look like the screenshot shown below.

how to flip table in excel using SORTBY Function


1.3 – Using INDEX Function

The INDEX function returns a value according to given row and column numbers. Moreover, the INDEX function is compatible with older versions of Excel.

Steps:

  • In cell E5 enter the formula given below:

=INDEX($B$5:$C$14,ROWS(B5:$B$14),COLUMNS($B$5:B5))

The B5:C14 cells refer to the Name and Sales values respectively while the B5:B14 cells indicate the Names.

Formula Breakdown:

  • INDEX($B$5:$C$14,ROWS(B5:$B$14),COLUMNS($B$5:B5)) → the $B$5:$C$14 is the array argument which is the marks scored by the students. ROWS(B5:$B$14) is the row_num argument which indicates the row location. COLUMNS($B$5:B5) is the optional column_num argument that points to the column location.
    • Output → Mary
Note: Make sure to lock the cell references by pressing the F4 key on your keyboard.

Employing INDEX Function

  • Use the Fill Handle tool to copy the formula to the cells below.

Using Fill Handle

  • Select the E5:E14 range.
  • Drag the Fill Handle Tool across to copy the formula into the adjacent cells.

 how to flip table in excel

The table is flipped

how to flip table in excel using INDEX Function


1.4 – Using VBA Code

If you often need to flip the table by columns, use the VBA code below.

Steps:

  • Go to the Developer tab.
  • Click the Visual Basic button.

Applying VBA Code

This opens the Visual Basic Editor in a new window.

  • Go to the Insert tab >> select Module.

Inserting Module

Copy the following code and paste it into the editor window:

Sub Invert_Table_By_Columns()

Dim First_row As Variant
Dim End_row As Variant
Dim First_num As Integer
Dim End_num As Integer

First_num = 1
End_num = Selection.Rows.Count

Do While First_num < End_num
First_row = Selection.Rows(First_num)
End_row = Selection.Rows(End_num)

Selection.Rows(End_num) = First_row
Selection.Rows(First_num) = End_row

First_num = First_num + 1
End_num = End_num - 1

Loop

End Sub

VBA Code

Code Breakdown:

The code is divided into 2 steps.

  • In the first portion, the sub-routine is given a name, Invert_Table_By_Columns().
  • We define the variables First_row, End_row, First_num, and End_num.
  • We assign Variant and Integer data types to these variables respectively.
  • We set First_num to 1 and use Selection.Rows.Count to obtain the End_num.
  • In the second portion, we apply the Do While statement to interchange the first and the last rows.
  • The loop moves to the next row and repeats this until all the rows are flipped.

VBA Code Explanation

  • Select the B5:C14 range.
  • Click the Macros button.
  • Choose the Invert_Table_By_Columns macro.
  • Click the Run button.

Running VBA Code

The results should look like the screenshot below.

how to flip table in excel with VBA Code

Read More: How to Flip Data Vertically in Excel


Method 2 – Flipping Table by Rows

2.1 – Using Sort Option

Steps:

  • Insert a Helper Column and number it sequentially.

Using Sort Option

  • Go to the Data tab >> click the Sort button.

how to flip table in excel

This opens the Sort wizard.

  • Click the Options button >> choose the Sort left to right option.

Sort by Rows

  • In the Sort by option, select Row 6.
  • In the Order field, choose Largest to Smallest.
  • Click the OK button.

Sort Descending Order

This flips the table as shown in the image below.

how to flip table in excel with Sort Option


2.2 – Using VBA Code

Steps:

  • Follow Steps 1-2 from the previous method to open the Visual Basic editor, insert a new Module and enter the code.
Sub Invert_Table_By_Rows()
 
    Dim r_range As Range
    Dim wk_range As Range
    Dim ar_rng As Variant
    Dim x As Integer, y As Integer, z As Integer
 
    On Error Resume Next
 
    xTitleId = "Invert_Table_Horizontally"
    Set wk_range = Application.Selection
    Set wk_range = Application.InputBox("Select a range of cells", xTitleId, wk_range.Address, Type:=8)
    ar_rng = wk_range.Formula
 
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
 
    For x = 1 To UBound(ar_rng, 1)
        z = UBound(ar_rng, 2)
        For y = 1 To UBound(ar_rng, 2) / 2
            xTemp = ar_rng(x, y)
            ar_rng(x, y) = ar_rng(x, z)
            ar_rng(x, z) = xTemp
            z = z - 1
        Next
    Next
 
    wk_range.Formula = ar_rng
    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic
End Sub

Employing VBA Code

Code Breakdown:

The code is divided into 3 steps.

  • In the first portion, the sub-routine is given a name, Invert_Table_By_Rows().
  • We define the variables and assign Range, Variant and Integer data types respectively.
  • In the second portion, an input box prompts the user to enter the range of cells to flip.
  • In the third portion, we use a nested For Loop to iterate through all the values in the given range and swap their positions one by one.

VBA Code Breakdown

  • Click the Macros button >> choose Invert_Table_By_Rows macro >> click the Run button.

Running VBA Code

  • Select the C4:L5 range of cells.
  • Click the OK button.

Selecting Range

The results should look like the picture shown below.

how to flip table in excel with VBA Code

Read More: How to Flip Data Horizontally in Excel 


Using Transpose Option to Convert Columns to Rows

Excel allows you to convert multiple columns in a table into rows using the Transpose option.

Consider the Marks Distribution of Students dataset shown in the B4:F11 cells below. Let’s transpose it.

Utilizing Transpose Option to Convert Columns to Rows

Steps:

  • Select the entire dataset, in this case, the B4:F11 range.
  • Press CTRL + C on your keyboard.

Copying Dataset

  • In cell B13 press CTRL + ALT + V to open the Paste Special dialog box.

Using Paste Special

  • Select the Transpose option.
  • Click the OK button.

Using Transpose option

The result should look like the image given below.

how to flip table in excel with Transpose option


Download Practice Workbook


Related Article


<< Go Back to Excel Reverse Order | Sort in Excel | Learn Excel

Get FREE Advanced Excel Exercises with Solutions!
Eshrak Kader
Eshrak Kader

Eshrak Kader is a dedicated professional with a BSc. Degree in Naval Architecture and Marine Engineering from Bangladesh University of Engineering and Technology. He boasts a rich background in Microsoft Office Suite, and over the past year, he has authored over 120 insightful articles for the ExcelDemy. Currently, Eshrak leads a team of 5 in the ExcelDemy Video project and enjoys problem-solving and making videos on Excel. Eshrak’s passion for continuous learning underscores his commitment to excellence in... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo