Should this code be written differently? Takes way longer than sorting each sheet manually

strat919

Board Regular
Joined
May 15, 2019
Messages
54
I have some code to sort all sheets column E low to high. I have 18 sheets each with about a million rows. I can sort one sheet manually and it takes maybe 5 seconds. When I use this code, the 18 sheets take almost 10 min.

I will have any number sheets, depending on previous calculations. In this instance, it is 18.

Also, it takes way more memory than I would expect.

Thanks for any help:)

Code:
Sub SortAllSheets()
   'Descending sort on A:E using column E, all sheets in workbook
   Dim ws      As Worksheet
   For Each ws In Worksheets
      ws.Columns("A:E").Sort Key1:=ws.Columns("E"), Order1:=xlAscending
   Next ws
End Sub
 
Sorry....maybe I was misunderstood.

only a zero distance would show up in the results if there were any duplicates in the AB column
 
Last edited:
Upvote 0

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
AB
142.482220.7458
242.213920.7397
342.914420.6897
442.578121.5803
542.321421.3583
642.914420.6897

<colgroup><col style="width: 25pxpx"><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Sheet1



ABCDE
142.914420.689742.914420.68970
242.213920.739742.482220.745818.54432
342.321421.358342.578121.580321.04423
442.914420.689742.482220.745830.00411
542.321421.358342.213920.739732.49618
642.321421.358342.482220.745833.1735
742.578121.580342.482220.745843.01226
842.914420.689742.213920.739748.47707
942.578121.580342.213920.739749.73902
1042.914420.689742.578121.580350.8234
1142.914420.689742.321421.358353.24988

<colgroup><col style="width: 25pxpx"><col><col><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Results 190727 7.38 am

Row 3 and 6 are duplicates so it showed up one time as 0 distance in results
 
Last edited:
Upvote 0
:confused:
How can there be duplicates in AB ?
- the code should remove duplicates in this line
Code:
[I][COLOR=#006400]'[/COLOR][COLOR=#ff0000]remove duplicate co-ordinates [/COLOR][COLOR=#006400]and place remaining values in array[/COLOR][/I]
    Data.Range("A:B").RemoveDuplicates Columns:=Array(1, 2)
 
Last edited:
Upvote 0
The duplicates should not be "removed" in the code. There will be very few cases where there will exact duplicates...... but I would need to see if there were any..... and they would appear at the top of the results as the the shortest distance being zero distance.

I guess everything would work perfectly if the code simply didn't remove any exact duplicates.

I hope this makes sense to you:)
 
Last edited:
Upvote 0
Why not simply delete the line that removes duplicates ?
 
Upvote 0
I didn't delete the entire line.

It works perfectly now:) Thank you so much for your help Mr Yongle!
 
Upvote 0

Forum statistics

Threads
1,214,552
Messages
6,120,172
Members
448,948
Latest member
spamiki

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