Dear Internet Explorer user: Your browser is no longer supported

Please switch to a modern browser such as Microsoft Edge, Mozilla Firefox or Google Chrome to view this website's content.

# Rearrange Excel columns via Visual Basic

A simple VBA script for rearranging the order of columns in an Excel spreadsheet.

An Excel spreadsheet for which the columns need to be rearranged.

The following Visual Basic for Applications (VBA) script will rearrange them in the order Header 6, Header 2, Header 1, Header 4, Header 5 and Header 3. Simply change the parameters in the code to suit the spreadsheet that you’re working on. Unlisted or missing columns will be ignored.

Sub Reorder_Columns()
Dim ColumnOrder As Variant, ndx As Integer
Dim Found As Range, counter As Integer
counter = 1

Application.ScreenUpdating = False

For ndx = LBound(ColumnOrder) To UBound(ColumnOrder)
Set Found = Rows("1:1").Find(ColumnOrder(ndx), LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False)
If Found.Column <> counter Then
Found.EntireColumn.Cut
Columns(counter).Insert Shift:=xlToRight
Application.CutCopyMode = False
End If
counter = counter + 1
End If
Next ndx
Application.ScreenUpdating = True
End Sub

The result of this script is as shown:

Columns rearranged in an Excel spreadsheet

## Activating the script

### Step 1

Open the Excel sheet that needs to be modified.

### Step 2

Press Alt + F11 or otherwise go to the Developer ribbon and choose the “Visual Basic” icon if the Developer Tab is enabled.

### Step 3

In the Visual Basic for Applications toolbar, go to Insert > Module

Inserting a module in Visual Basic for Applications.

Insert the above code into the module.

### Step 4

Press F5 to run the macro and execute the code.

17 responses to “Rearrange Excel columns via Visual Basic”

On 10 May 2017, Michael Pluck wrote:

Hmmm. For some reason, when I try to run the code I am getting a compile error and my column names are in red text. The only thing that I changed in the above code is my column names. Does the name of my spreadsheet or workbook matter?

On 10 May 2017, Michael Pluck wrote:

Never mind. I changed my column names to header 1, etc. and it worked. Thank you very much for the code.

If you are going to publish my comments, please replace my last name to the initial “P.”

On 31 July 2017, Adam Dimech wrote:

Hello Michael, please refer to the Editorial Policy. Comments will be published as they’re received. If you don’t wish for your surname to be published then don’t supply it.

On 23 June 2017, RobertSF wrote:

Thank you! This was a great example. It was easy to understand and was just what I was looking for.

On 9 March 2018, Nagesh wrote:

Working Great, thank you so much:)

On 12 April 2018, Praveen wrote:

Thanks a lot,Really Helfulll

On 1 June 2018, Kaushik shetty wrote:

Hello sir , this code helped me lot ,it reduced my 30 min of Daly work .

On 14 June 2018, NACHO wrote:

Thanks! Saved me a lot of time.

On 17 July 2018, Prafull wrote:

Thank you very much!

On 4 September 2018, KevinYen wrote:

It really helps! Thank you:D

On 6 September 2018, KevinYen wrote:

On 11 September 2018, Carol wrote:

This worked for all the columns that were formatted as text. However, it ignore the columns that were formatted as Number (Accounting). Can you please add/include the Number format in the script.

On 6 June 2019, ABSExcel wrote:

Really Nice code!!!

On 8 July 2019, natalie wrote:

Works great, but I get a sytax error, I’m wanting to rearrange 100 – 120 columns, is there a limit?

Thanks!

On 4 April 2020, Dylan wrote:

Great code, thank you! Can you describe how to rename the columns after moving, please?

Best,
Dylan

On 3 November 2020, bill Simpson wrote:

Doesnt work. Nothing at all happens. Nothing. Not even an error. I think its missing some descriptive steps, more detailed example would be useful.

On 12 November 2020, Sam wrote:

Thank you *so* much for sharing this! It totally automated a 111 column rearrangement flawlessly and made out reporting lives *so* much better. 10/10 would recommend again!

To any having problems:
Make sure the number of headers listed in the macro module and that are on your sheet match.
You can insert a row at the top and add “header 1”, “header 2”, “… 3”, “etc.”, (also see above) and run the macro.
You can copy your headers and replace “header 1”, “header 2”, “… 3”, “etc.” in the module, with the actual header titles you have on the sheet and run the macro.

<a href="" title=""> <b> <blockquote cite=""> <code> <em> <i> <q cite=""> <strike> <strong>