VBA for sorting

vedderman

New Member
Joined
Nov 2, 2016
Messages
27
Hello,
Wondering of someone can help me clean this up a little and make it dynamic as the used range is not always going to be ending at Z281. I've tried other code found on this site, but none of it seemed to work with my sheet. Data in rows 1-2 is a title and statistics, row 3 contains header.

Code:
    Range("W4").Select    ActiveWorkbook.Worksheets("Data").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Data").Sort.SortFields.Add Key:=Range("W4"), _
        SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
        xlSortTextAsNumbers
    With ActiveWorkbook.Worksheets("Data").Sort
        .SetRange Range("A3:Z281")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Does every row of data have column Z populated?
If so, then try something like this:
Code:
    Dim lRow as Long
    Dim rng as Range

'   Find last row with data in column Z
    lRow = Cells(Rows.Count,"Z").End(xlUp).Row
'   Set range to sort
    Set rng = Range("A3:Z" & lRow)

    ActiveWorkbook.Worksheets("Data").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Data").Sort.SortFields.Add Key:=Range("W4"), _
        SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
        xlSortTextAsNumbers
    With ActiveWorkbook.Worksheets("Data").Sort
        .SetRange[COLOR=#ff0000] rng[/COLOR]
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
 
Last edited:
Upvote 0
Column Z is populated with data to the end of the range, so this solution works well. Thank you.
Should have mentioned this is one step in a much larger string of code so getting this to work held me up for the last two hours. Much appreciated.
 
Upvote 0
You are welcome!

If column Z could be blank for some rows, then all you would have to do is choose some column that does not have any blanks in the data and change it here:
Code:
lRow = Cells(Rows.Count,"[COLOR=#ff0000][B]Z[/B][/COLOR]").End(xlUp).Row
 
Upvote 0

Forum statistics

Threads
1,213,494
Messages
6,113,972
Members
448,537
Latest member
Et_Cetera

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