Roggie

New Member
Joined
Mar 7, 2018
Messages
4
I recorded a macro to sort my worksheet by certain colors and I noticed the range is hardcoded into the script. Since my range will change with each export, is there a way to edit the ranges to reflect the whatever the size of the worksheet is?

Code:
Sub SORT2()
'
' SORT2 Macro
'
' Keyboard Shortcut: Ctrl+j
'
    ActiveWorkbook.Worksheets("Candidates|All Attributes").SORT.SortFields.Clear
    ActiveWorkbook.Worksheets("Candidates|All Attributes").SORT.SortFields.Add( _
        Range("[B]A2:A322[/B]"), xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue. _
        Color = RGB(146, 208, 80)
    ActiveWorkbook.Worksheets("Candidates|All Attributes").SORT.SortFields.Add( _
        Range("[B]A2:A322[/B]"), xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue. _
        Color = RGB(155, 194, 230)
    With ActiveWorkbook.Worksheets("Candidates|All Attributes").SORT
        .SETRANGE Range("[B]A1:AJ322[/B]")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Maybe...

Code:
Sub SORT2()
    '
    ' SORT2 Macro
    '
    ' Keyboard Shortcut: Ctrl+j
    '
    lr = Worksheets("Candidates|All Attributes").Cells.Find("*", , xlValues, , xlByRows, xlPrevious).Row

    ActiveWorkbook.Worksheets("Candidates|All Attributes").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Candidates|All Attributes").Sort.SortFields.Add( _
            Range("A2:A" & lr), xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue. _
            Color = RGB(146, 208, 80)
    ActiveWorkbook.Worksheets("Candidates|All Attributes").Sort.SortFields.Add( _
            Range("A2:A" & lr), xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue. _
            Color = RGB(155, 194, 230)

    With ActiveWorkbook.Worksheets("Candidates|All Attributes").Sort
        .SetRange Range("A1:AJ" & lr)
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,691
Messages
6,126,220
Members
449,303
Latest member
grantrob

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