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

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
37,341
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
Welcome to the forum! :)

Try adding:

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

before the line that deletes the custom list you created.
 
Solution

TH_AjeTs

New Member
Joined
Apr 27, 2016
Messages
7
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
 

TH_AjeTs

New Member
Joined
Apr 27, 2016
Messages
7
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!
 

Forum statistics

Threads
1,141,203
Messages
5,704,942
Members
421,372
Latest member
Jamie11

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
Top