Custom sort not working correctly on larger file

Bassie

Board Regular
Joined
Jan 13, 2022
Messages
66
Office Version
  1. 2019
Platform
  1. Windows
Hi,

Is there a way to make custom sort work correctly on larger files? I created a custom list but both in VBA and in regular excel it does not work correctly on larger files, while it does work perfectly fine on smaller files.
Is this a known issue and is there a way to resolve this?

Current VBA code:
VBA Code:
MPListGL = "IT, DE, NL, UK, SE"
vSortlistGL = Array(MPListGL)
Application.AddCustomList ListArray:=vSortlistGL
ActiveSheet.UsedRange.sort key1:=GL, Header:=xlYes, ordercustom:=Application.CustomListCount + 1

Kind regards,
Bassie
 
Try out the following on a copy of your file. Assumes column A holds the values to sort on. It inserts a column in B:B, evaluates the first 2 characters from column A, sorts on column B, then deletes column B. I tested it on data with 1M rows & 90 columns. It took a while (30 seconds+) but it did seem to work.

EDITED
VBA Code:
Option Explicit
Sub Bassie()
    Application.ScreenUpdating = False
    Dim t As Double: t = Timer
    Dim ws As Worksheet, rng As Range
    Set ws = Worksheets("Sheet1")   '<~~ change to actual sheet name
   
    Dim LRow As Long
    LRow = ws.Cells.Find("*", , xlFormulas, , xlByRows, xlPrevious).Row
   
    With ws.Range("B1")
        .EntireColumn.Insert
        .Value = "temp"
    End With
   
    With Range("B2:B" & LRow)
        .Value = Evaluate("Left(" & .Offset(, -1).Address(, , , 1) & ",2)")
    End With
   
        Set rng = ws.Range("A1").CurrentRegion
        Application.AddCustomList ListArray:=Array("IT", "DE", "NL", "UK", "SE")
        With ws.Sort
            .SortFields.Clear
            .SortFields.Add2 Key:=ws.Range("B1"), _
            CustomOrder:=Application.CustomListCount
            .SetRange rng
            .Orientation = xlTopToBottom
            .Apply
        End With
    ws.Range("B1").EntireColumn.Delete
   
    MsgBox Timer - t
    Application.ScreenUpdating = True
End Sub
That works wonders, thank you so much!! I dont mind that it takes 30 seconds as this macro runs ones a week for a total of like 4 mins and I honestly think you cant make it fast than this on files that large. Again thank you :D

Regards
Bassie
 
Upvote 0

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

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