Rearrange columns based on number at top

Nick70

Active Member
Joined
Aug 20, 2013
Messages
306
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have a set of data in range(B1:E20).
In row (B1:E1) there are numbers 1 to 4 in this order 2,1,4,3.
I would like to rearrange the columns so that column B is the one which has number 1 then column C has number 2 then column D has number 3 and column E has number 4.
Basically the columns have to be 'sorted' in ascending order based on their numbers in row 1.

How do I do that?

Thanks,
Nic
:unsure:
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
That worked fine thanks.
It creates a new set of data with correct order.

If there is a VBA code to actually move position of existing set of columns this would also be helpful.

Thanks,
Nic
 
Upvote 0
VBA code as per your request :

VBA Code:
Sub SortByColumnNo()
    With ActiveWorkbook.Worksheets("Sheet1").Sort
        .SetRange Range("B1:E20")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlLeftToRight
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub
 
Upvote 0
Solution
Have you tried the data sort "options" of "Left to Right":

1714660784211.png
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,241
Members
452,622
Latest member
Laura_PinksBTHFT

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top