Thread: [Solved] Macro to sort columns
View Single Post
 
Old 08-17-2016, 08:47 AM
SerenityNetworks SerenityNetworks is offline Windows 10 Office 2016
Advanced Beginner
 
Join Date: May 2005
Location: Allen, Texas, USA
Posts: 37
SerenityNetworks
Default Answer

Thank you. Your code snippets got me past my issues with the syntax. The following works.

Code:
Sub DoIt()
    ' Before running macro, select first (leftmost) column to sort.
    Dim Counter As Integer
    Counter = 1
    Do While Counter <= 280 'set number according to columns remaining to sort
        ActiveWorkbook.Worksheets("SortIt").Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("SortIt").Sort.SortFields.Add Key:=ActiveCell.EntireColumn.Cells(1), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        With ActiveWorkbook.Worksheets("SortIt").Sort
            .SetRange Selection.EntireColumn
            .Header = xlGuess
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
        ActiveCell.Offset(0, 1).Range("A1").Select
        Counter = Counter + 1
    Loop
End Sub
Reply With Quote