Sort columns using variables

lilmisspink07

New Member
Joined
Aug 17, 2011
Messages
4
Hello there,

I need to sort by multiple columns in a worksheet where the worksheet name, column locations and used range will change every time. I have the below code but it's not sorting so I know that I've missed something.... I think it might be the range variables that I set and then use as the keys?
Any advice would be much appreciated!

Code:
[SIZE=2]Dim MACHINE As Range
Set MACHINE = Range(Range("A1:DZ1").Find("MACHINE"), Range("A1:DZ1").Find("MACHINE").EntireColumn)

Dim MESSAGE_ID As Range
Set MESSAGE_ID = Range(Range("A1:DZ1").Find("MESSAGE_ID"), Range("A1:DZ1").Find("MESSAGE_ID").EntireColumn)

With ActiveWorkbook.ActiveSheet.Sort
    With .SortFields
        .Clear
        .Add Key:=MACHINE, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        .Add Key:=MESSAGE_ID, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    End With
    .SetRange Range("A1").CurrentRegion
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With[/SIZE]
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Something needs to be selected:

Rich (BB code):
    Dim MACHINE As Range
    Set MACHINE = Range(Range("A1:DZ1").Find("MACHINE"), Range("A1:DZ1").Find("MACHINE").EntireColumn)
    
    Dim MESSAGE_ID As Range
    Set MESSAGE_ID = Range(Range("A1:DZ1").Find("MESSAGE_ID"), Range("A1:DZ1").Find("MESSAGE_ID").EntireColumn)

    Range("A1").Select

    With ActiveWorkbook.ActiveSheet.Sort
        With .SortFields
            .Clear
            .Add Key:=MACHINE, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
            .Add Key:=MESSAGE_ID, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        End With
        .SetRange Range("A1").CurrentRegion
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
 
Upvote 0

Forum statistics

Threads
1,223,275
Messages
6,171,122
Members
452,381
Latest member
Nova88

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