rhombus4
Well-known Member
- Joined
- May 26, 2010
- Messages
- 586
- Office Version
- 365
- 2016
- Platform
- 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