Sorting data takes a long time for large amount of data. any shortcuts

MeghaJ

Board Regular
Joined
Jul 2, 2009
Messages
102
Dear Friends,

Please find the code for sorting data:

Code:
Sub SortData()
Dim nLastRow, Rownum As Integer
nLastRow = 0
Rownum = 0
Worksheets("Sheet2").Activate
Sheets("Sheet2").Select
nLastRow = Range("A" & Rows.Count).End(xlUp).Row

ActiveWorkbook.Worksheets("Sheet2").Sort.SortFields.clear
ActiveWorkbook.Worksheets("Sheet2").Sort.SortFields.Add Key:=Range("B:B") _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Sheet2").Sort.SortFields.Add Key:=Range("D:D") _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Sheet2").Sort.SortFields.Add Key:=Range("E:E") _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet2").Sort
    .SetRange Rows("1:" & nLastRow)
    .Header = xlGuess
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
End Sub

I have around 50,000 rows of data and need to sort this data in 3 levels.
This takes a lot of time.

Please tell me how this can be achieved in a shorter duration.

Thanks
megha
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Does manually sorting by those levels take a similarly long amount of time? In other words, is it the sorting that's taking a long time or is it something from the code?

Here's a standard set of sorting code that I use very frequently (on large tables similar to yours, usually 80K records by 160 columns). Feel free to use/adapt if you suspect your code is causing the slow down. It assumes your table includes column A, titles in row 1.
Code:
'Sort my data dammit
 ActiveWorkbook.Worksheets("Sheet2").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet2").Sort.SortFields.Add Key:=Range("B1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Sheet2").Sort.SortFields.Add Key:=Range("D1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Sheet2").Sort.SortFields.Add Key:=Range("E1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        
    With ActiveWorkbook.Worksheets("Sheet2").Sort
        .SetRange Range("A1").CurrentRegion
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With

If its' the sort itself that's causing the problem: It's probably being caused by formulas in your data range. One thing you can do, is test clearing the formulas (via copy/paste as values, perhaps leaving the first formula row for convenience later) and then trying the sort. Often formulas will cause slowdown as the sort triggers a recalculate.
 
Upvote 0

Forum statistics

Threads
1,223,268
Messages
6,171,099
Members
452,379
Latest member
IainTru

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