VBA Sort causes Excel to crash

TH_AjeTs

New Member
Joined
Apr 27, 2016
Messages
7
VBA Code:
Sub AutoSort2()
'
' AutoSort Macro
' This will sort by the list of topics
'
    Dim dynlist As Variant
    dynlist = Application.Transpose(Sheet2.ListObjects("table2").ListColumns(2).DataBodyRange)
   
    ActiveTable = ActiveSheet.Range("b15").ListObject.Name
    ActiveSheet.ListObjects(ActiveTable).Sort.SortFields.Clear
    Application.AddCustomList listarray:=dynlist
    ActiveSheet.ListObjects(ActiveTable).Sort.SortFields. _
        Add2 Key:=ActiveSheet.ListObjects(ActiveTable).ListColumns(7).DataBodyRange, SortOn:=xlSortOnValues, Order:=xlAscending, _
        CustomOrder:=Application.CustomListCount, _
        DataOption:=xlSortNormal
    With ActiveSheet.ListObjects(ActiveTable).Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Application.DeleteCustomList Application.CustomListCount
   
   ActiveSheet.Calculate
   Sheet2.Calculate
   Sheet5.Calculate
   Sheet6.Calculate

End Sub

First off, I'm seriously noob at VBA, I just google stuff and put various solutions together to achieve what I want.

I just got off microsoft support which help pin-point that the VBA is the issue. Because when I try to manually sort, nothing goes wrong. But if I should use the macro, then try to save afterwards, Excel would crash.

Somebody save me pls. Thanks
 
Last edited by a moderator:

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Welcome to the forum! :)

Try adding:

Code:
ActiveSheet.ListObjects(ActiveTable).Sort.SortFields.Clear

before the line that deletes the custom list you created.
 
Upvote 0
Solution
Thanks everyone. I think I finally figured it out. Here's the modified code:

Sub AutoSort2()
'
' AutoSort Macro
' This will sort by the list of topics
'
Dim dynlist As Variant
Dim ActiveTable2 As Variant

Let dynlist = Application.Transpose(Sheet2.ListObjects("table2").ListColumns(2).DataBodyRange)
Let ActiveTable2 = ActiveSheet.Range("b15").ListObject.Name

ActiveSheet.ListObjects(ActiveTable2).Sort.SortFields.Clear
Application.AddCustomList listarray:=dynlist
ActiveSheet.ListObjects(ActiveTable2).Sort.SortFields. _
Add2 Key:=ActiveSheet.ListObjects(ActiveTable2).ListColumns(7).DataBodyRange, SortOn:=xlSortOnValues, Order:=xlAscending, _
CustomOrder:=Application.CustomListCount, _
DataOption:=xlSortNormal
With ActiveSheet.ListObjects(ActiveTable2).Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
ActiveSheet.ListObjects(ActiveTable2).Sort.SortFields.Clear

ActiveSheet.Calculate
Sheet2.Calculate
Sheet5.Calculate
Sheet6.Calculate

Application.DeleteCustomList listNum:=Application.CustomListCount

End Sub

I narrowed it down to the application.deletecustomlist and after some googling, I found out that just specifying with listNum:=5 worked.
So in summary, I modified
Application.DeleteCustomList Application.CustomListCount
to
Application.DeleteCustomList listNum:=Application.CustomListCount
 
Upvote 0
Welcome to the forum! :)

Try adding:

Code:
ActiveSheet.ListObjects(ActiveTable).Sort.SortFields.Clear

before the line that deletes the custom list you created.
This suggestion was also necessary to the final solution. Thanks so much!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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