Custom sort macro

manby7

New Member
Joined
Oct 12, 2015
Messages
2
Hi

I'm having some problems with a custom sort in a macro I've made.

Below is the extract of the specific piece of code, the error occurs on the penultimate line where it tries to apply the sort.

This part of the code is right at the end of the macro (it's rather large hence not posting all of it). The weird thing is that if I run the macro without the sort, and then run the sort as a separate macro it works without a problem. I thought it might be something to do with having automatic calculations turned off whilst running but this doesn't seem to be the case.

Any help would be very much appreciated! I'm sure it's something obvious I'm missing...

Code:
    Cells.Select
    ActiveWorkbook.Worksheets("Console").sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Console").sort.SortFields.Add Key:=Range("A1:A1048576"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, CustomOrder:= _
        "Custom 1,Custom 2,Custom 3,Custom 4,Custom 5,Custom 6" _
        , DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Console").sort.SortFields.Add Key:=Range( _
        "H1:H1048576"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    ActiveWorkbook.Worksheets("Console").sort.SortFields.Add Key:=Range( _
        "CF1:CF1048576"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
        :=xlSortNormal
    ActiveWorkbook.Worksheets("Console").sort.SortFields.Add Key:=Range( _
        "CG1:CG1048576"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
        :=xlSortNormal
    ActiveWorkbook.Worksheets("Console").sort.SortFields.Add Key:=Range( _
        "CH1:CH1048576"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
        :=xlSortNormal
    ActiveWorkbook.Worksheets("Console").sort.SortFields.Add Key:=Range( _
        "CI1:CI1048576"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
        :=xlSortNormal
    ActiveWorkbook.Worksheets("Console").sort.SortFields.Add Key:=Range( _
        "CJ1:CJ1048576"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
        :=xlSortNormal
    ActiveWorkbook.Worksheets("Console").sort.SortFields.Add Key:=Range( _
        "CK1:CK1048576"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
        :=xlSortNormal
    ActiveWorkbook.Worksheets("Console").sort.SortFields.Add Key:=Range( _
        "CL1:CL1048576"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
        :=xlSortNormal
    ActiveWorkbook.Worksheets("Console").sort.SortFields.Add Key:=Range( _
        "CM1:CM1048576"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
        :=xlSortNormal
    ActiveWorkbook.Worksheets("Console").sort.SortFields.Add Key:=Range( _
        "CN1:CN1048576"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
        :=xlSortNormal
    ActiveWorkbook.Worksheets("Console").sort.SortFields.Add Key:=Range( _
        "CO1:CO1048576"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
        :=xlSortNormal
    ActiveWorkbook.Worksheets("Console").sort.SortFields.Add Key:=Range( _
        "CP1:CP1048576"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
        :=xlSortNormal
    ActiveWorkbook.Worksheets("Console").sort.SortFields.Add Key:=Range( _
        "CQ1:CQ1048576"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
        :=xlSortNormal
    ActiveWorkbook.Worksheets("Console").sort.SortFields.Add Key:=Range( _
        "CR1:CR1048576"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
        :=xlSortNormal
    ActiveWorkbook.Worksheets("Console").sort.SortFields.Add Key:=Range( _
        "CS1:CS1048576"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
        :=xlSortNormal
    With ActiveWorkbook.Worksheets("Console").sort
        .SetRange Columns("A:CV")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Just to update, I think the problem was that it was running out of memory trying to apply the filter. I've set it up to now run the sort on re-opening the workbook after the initial macro has been run, probably not the best solution but it will do for now.
 
Upvote 0

Forum statistics

Threads
1,214,583
Messages
6,120,377
Members
448,955
Latest member
BatCoder

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