Link to home
Start Free TrialLog in
Avatar of Ian Bell
Ian BellFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Automatic sort formula

HI
Is there a way to sort a block of data automatically from one part
of the sheet to another.
I have attached a spreadsheet which was sorted manually
To be sorted on column C
Thanks
Ian
automatic-sort.xlsx
Avatar of Shums Faruk
Shums Faruk
Flag of India image

Please try below code:
Sub SortData()
Application.ScreenUpdating = False
Dim Ws As Worksheet
Dim LR As Long
Dim Rng As Range
Set Ws = Worksheets("Sheet1")
Set Rng = Ws.Range("A1:D12")
Rng.Copy
Ws.Range("A17").PasteSpecial xlPasteValues
Application.CutCopyMode = False

LR = Ws.Range("A" & Rows.Count).End(xlUp).Row

Ws.Sort.SortFields.Clear
    Ws.Sort.SortFields.Add Key:=Range("C17:C" & LR), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
    With Ws.Sort
        .SetRange Range("A17:D" & LR)
        .Header = xlNo
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
Ws.Range("A17").Select
Application.ScreenUpdating = True
End Sub

Open in new window

Above code is with copying the data and sorting.

If you just want to sort then try below:
Sub SortData()
Application.ScreenUpdating = False
Dim Ws As Worksheet
Dim LR As Long
Dim Rng As Range
Set Ws = Worksheets("Sheet1")
LR = Ws.Range("A" & Rows.Count).End(xlUp).Row

Ws.Sort.SortFields.Clear
    Ws.Sort.SortFields.Add Key:=Range("C17:C" & LR), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
    With Ws.Sort
        .SetRange Range("A17:D" & LR)
        .Header = xlNo
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
Ws.Range("A17").Select
Application.ScreenUpdating = True
End Sub

Open in new window

SOLUTION
Avatar of Subodh Tiwari (Neeraj)
Subodh Tiwari (Neeraj)
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Ian Bell

ASKER

Great effort can I ask you to look at the attached and see if it can be improved
Cheers
Ian
autosortmacro.xlsx
Ian,

Please try attached just for sort and I noticed there are some blank cells in BI to BP which where copy pasted, but counting those cells.
I added a macro to clean up those cells and sort as per BM.
Let me know the sheet name and range, where you are copying from Master, I will edit the code.
automatic-sort_V2.xlsm
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks guys for your help.
Very happy indeed
Ian
You're welcome Ian! Glad I was able to help you.