excel crashing when saving after custom sort

rhombus4

Well-known Member
Joined
May 26, 2010
Messages
586
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have 2 Custom Sorts which both work, but when I run either Code below and then save the workbook excel crashes. Just closes without any message. It will also close other workbooks if I have them open

VBA Code:
Sub Custom_Sort1()
Dim ws As Worksheet
Set ws = ActiveWorkbook.Worksheets("Sheet1")
Dim rgSort As Range
Set rgSort = ws.Range("A1:Q50")
Dim rgKey As Range
Set rgKey = ws.Range("L1")

Dim sCustomList(1 To 4) As String
sCustomList(1) = "Cat": sCustomList(2) = "Dog": sCustomList(3) = "Bird": sCustomList(4) = "Ape"

 Application.AddCustomList ListArray:=sCustomList

ws.Sort.SortFields.Clear
rgSort.Sort Key1:=rgKey, Order1:=xlAscending, Header:=xlGuess, _
    OrderCustom:=Application.CustomListCount + 1, MatchCase:=False, _
    Orientation:=xlTopToBottom, DataOption1:=xlSortNormal

Application.DeleteCustomList Application.CustomListCount
Set ws = Nothing

End Sub


VBA Code:
Sub Custom_Sort2()
Dim ws As Worksheet
Set ws = ActiveWorkbook.Worksheets("Sheet1")
Dim rgSort As Range
Set rgSort = ws.Range("A1:Q50")
Dim rgKey As Range
Set rgKey = ws.Range("L1")

Dim xyz As Variant
xyz = Array("Cat", "Dog", "Bird", "Ape")

Application.AddCustomList ListArray:=xyz

ws.Sort.SortFields.Clear
rgSort.Sort Key1:=rgKey, Order1:=xlAscending, Header:=xlGuess, _
    OrderCustom:=Application.CustomListCount + 1, MatchCase:=False, _
    Orientation:=xlTopToBottom, DataOption1:=xlSortNormal

Application.DeleteCustomList Application.CustomListCount
Set ws = Nothing

End Sub
 

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.
Why not actually use the Sort object which allows you to specify the sort order directly? For example:

VBA Code:
Sub Custom_Sort2()
Dim ws As Worksheet
Set ws = ActiveWorkbook.Worksheets("Sheet1")
Dim rgSort As Range
Set rgSort = ws.Range("A1:Q50")
Dim rgKey As Range
Set rgKey = ws.Range("L1")

With ws.Sort
   With .SortFields
      .Clear
      add Key:=rgKey, Order:=xlAscending, Customorder:="Cat,Dog,Bird,Ape", DataOption:=xlSortNormal
   End With
.Setrange rgsort
.Header= xlGuess
.Orientation = xlTopToBottom
end with
Set ws = Nothing

End Sub
 
Upvote 0
Why not actually use the Sort object which allows you to specify the sort order directly? For example:

VBA Code:
Sub Custom_Sort2()
Dim ws As Worksheet
Set ws = ActiveWorkbook.Worksheets("Sheet1")
Dim rgSort As Range
Set rgSort = ws.Range("A1:Q50")
Dim rgKey As Range
Set rgKey = ws.Range("L1")

With ws.Sort
   With .SortFields
      .Clear
      add Key:=rgKey, Order:=xlAscending, Customorder:="Cat,Dog,Bird,Ape", DataOption:=xlSortNormal
   End With
.Setrange rgsort
.Header= xlGuess
.Orientation = xlTopToBottom
end with
Set ws = Nothing

End Sub


Had to use .add and then .apply although it sorted the header row as well
 
Upvote 0
Thanks

I actually changed the range and header no, but I'm assuming both would work

PS Rory: Why not actually use the Sort object which allows you to specify the sort order directly?
Didn't realise you could do it that way, When I searched most people added custom list sorted then deleted the Custom list.

Also I did find an older thread of yours where you mentioned to clear sort field before deleting the custom list. So when I added it at the end of the sort part it stopped crashing. Not sure why it crashes in the first place???

VBA Code:
ws.Sort.SortFields.Clear
rgSort.Sort Key1:=rgKey, Order1:=xlAscending, Header:=xlGuess, _
    OrderCustom:=Application.CustomListCount + 1, MatchCase:=False, _
    Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
ws.Sort.SortFields.Clear


VBA Code:
Sub Custom_Sort3()
Dim ws As Worksheet
Set ws = ActiveWorkbook.Worksheets("Sheet1")

Dim rgSort As Range
Set rgSort = ws.Range("A2:Q50")
Dim rgKey As Range
Set rgKey = ws.Range("L2")

With ws.Sort
   With .SortFields
      .Clear
    .Add Key:=rgKey, Order:=xlAscending, CustomOrder:="Cat,Dog,Bird,Ape", DataOption:=xlSortNormal
   End With
.SetRange rgSort
.Header = xlNo
.Orientation = xlTopToBottom
.Apply
End With
Set ws = Nothing

End Sub
 
Upvote 0
Probably because you've got it sorting using a list that no longer exists. Using the Sort object is better for this sort of thing, in my opinion.
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,194
Members
453,021
Latest member
pingpong7117

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