Rearrange the excel columns by the columns header name

sonypcg

New Member
Joined
Oct 2, 2017
Messages
8
How could I rearrange the columns by the columns header name.
I need to process a report on weekly basis,the raw worksheet has 23 columns.I abstract a part of them as example.
Raw columns sequence:

QTYPayment TermsContract No.

<tbody>
</tbody>



Desired columns sequence:
Contract No.Payment termsQTY

<tbody>
</tbody>


Any idea how to automatize the columns rearrangement with VBA code?
Many thanks in advance.


:biggrin:
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Re: How to rearrange the excel columns by the columns header name

Try:
Code:
Sub SortCols()
    Dim lColumn As Long
    lColumn = Cells(1, Columns.Count).End(xlToLeft).Column
    Dim LastRow As Long
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Range(Cells(1, 1), Cells(LastRow, lColumn)).Sort Key1:=Range(Cells(1, 1), Cells(1, lColumn)), Order1:=xlAscending, Orientation:=xlLeftToRight
End Sub
 
Upvote 0
Re: How to rearrange the excel columns by the columns header name

Try this for results on sheet2.
Code:
[COLOR="Navy"]Sub[/COLOR] MG02Oct06
[COLOR="Navy"]Dim[/COLOR] Ray [COLOR="Navy"]As[/COLOR] Variant
Ray = Cells(1).CurrentRegion.Resize(, 23)
ReDim nRay(1 To UBound(Ray, 1), 1 To UBound(Ray, 2))
                                                                       '[COLOR="Green"][B]>>Change the columns array Here to what you want<<< At the Moment its columns 10 TO 23 then 1 TO 9[/B][/COLOR]
nRay = Application.Index(Ray, Evaluate("ROW(1:" & UBound(Ray, 1) & ")"), Array(10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 1, 2, 3, 4, 5, 6, 7, 8, 9))
Sheets("Sheet2").Range("A1").Resize(UBound(Ray, 1), UBound(Ray, 2)).Value = nRay
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Last edited:
Upvote 0
Re: How to rearrange the excel columns by the columns header name

Without code you can sort columns just like you can rows.

After you click the Sort Button, and you have your choices of sort by column..
Click the Options button and choose "Sort Left to right"
Then you can sort by the top row.
 
Upvote 0
Re: How to rearrange the excel columns by the columns header name

Hi Mumpls
Many thanks for your prompt help.The code seems to rearrange them in alphabetical order?
Sry may be I misunderstood,could pls give me further instruction?


 
Upvote 0
Re: How to rearrange the excel columns by the columns header name

I'm not sure I understand. What further instruction would you like?
 
Upvote 0
Re: How to rearrange the excel columns by the columns header name

Actually the whole picture would be more complicated than my abstracted columns example,which I am unable to sort it out in alphabetical order with build in sort function
Fulfillment CenterRegionCountryOfficeOrig Contract No.Order NoBSA Line NO.Delivery DetailDelivery IdOrder Line No.Apply LineOrder QtyQtyUnit priceAmountCurrencyAmount USDDelivery BatchProduct CodeItem

<tbody>
</tbody>
 
Upvote 0
Re: How to rearrange the excel columns by the columns header name

Many thanks Mick
Actually I took advantage of the column array in my original code to rearrange columns,however the disadvantage of it is not dynamic.The challenge is that sometime the columns array between raw worksheets varies,but the column headers name are always identical.
 
Upvote 0
Re: How to rearrange the excel columns by the columns header name

The macro I suggested works with the headers you posted in Post #7 and will work any number of columns.
 
Upvote 0
Re: How to rearrange the excel columns by the columns header name

The application of your code together with the abstracted example would be highly appreciated. Sry I can not fully understand the code.
The raw worksheet contain at least 23 columns,some raw files are with even more columns. The columns array is different,only the desired column header name are the same.


Sub SortCols()
Dim lColumn As Long
lColumn = Cells(1, Columns.Count).End(xlToLeft).Column
Dim LastRow As Long
LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Range(Cells(1, 1), Cells(LastRow, lColumn)).Sort Key1:=Range(Cells(1, 1), Cells(1, lColumn)), Order1:=xlAscending, Orientation:=xlLeftToRight
End Sub

<tbody>
</tbody>




<tbody>
</tbody>
 
Upvote 0

Forum statistics

Threads
1,214,947
Messages
6,122,411
Members
449,081
Latest member
JAMES KECULAH

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