Sort Columns Titles

orlmor

New Member
Joined
Apr 1, 2016
Messages
3
I have a macro who reads CSV files (22 in this test, but they are more) and put data starting on column 9 ("I").
I sort those columns using copy paste transpose, but I don't like this, I'm looking for a better way to sort the titles.
My sort code is:
Code:
Sub SortTitles()
    Range("I1:AD1").Copy
    Range("AF1").PasteSpecial Paste:=xlPasteAll, _
        Operation:=xlNone, SkipBlanks:=False, Transpose:=True
    With ActiveWorkbook.Worksheets("Global").Sort
        .SortFields.Clear
        .SortFields.Add Key:=Range("AF1:AF22"), SortOn:=xlSortOnValues, _
            Order:=xlAscending, DataOption:=xlSortNormal
        .SetRange Range("AF1:AF22")
        .Header = xlNo
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Selection.Copy
    Range("I1").PasteSpecial Paste:=xlPasteAll, _
        Operation:=xlNone, SkipBlanks:=False, Transpose:=True
    Columns("AF").Delete xlToLeft
End Sub
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Hi orlmor,

just so you'd know: excel offers a functionality to sort columns. It's under the sort feature => options => left to right.

I ran this throught the VBA recorder and got this:

Code:
Sub Macro1()
'
' Macro1 Macro

    ActiveWorkbook.Worksheets("Blad1").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Blad1").Sort.SortFields.Add Key:=Range("B2:D2"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Blad1").Sort
        .SetRange Range("B2:D5")
        .Header = xlYes
        .MatchCase = False
        .Orientation = [B]xlLeftToRight[/B]
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,219
Messages
6,129,577
Members
449,519
Latest member
Rory Calhoun

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