Weird Sorting Problem

Alex Piotto

Board Regular
Joined
Jul 5, 2016
Messages
82
Office Version
  1. 2007
Platform
  1. Windows
Ciao brava gente!
I am experiencing a very strange behavior sorting a few columns of data, by date. Real dates, not text.
The script below from the macro recorder seems ok to me.
VBA Code:
ActiveWorkbook.Worksheets("HOME").Sort.SortFields.Add Key:=Range("B13"), _
        SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("HOME").Sort
        .SetRange Range("B13:J10000")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
The sorting happen, but after several new insertion of rows and data, sorted every time, I get this
show1.jpg


full of levels. Tens of them! After saving the file and reopening I get this message:
show2.jpg


Never seen before... what is happening?
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
When using SortFields to need to clear them every time you run the code like
VBA Code:
   With ActiveWorkbook.Worksheets("HOME").Sort
      .SortFields.Clear
      .SortFields.Add Key:=Range("B13"), _
        SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
      .SetRange Range("B13:J10000")
      .Header = xlYes
      .MatchCase = False
      .Orientation = xlTopToBottom
      .SortMethod = xlPinYin
      .Apply
    End With
The other option is to use the Range.sort method instead
VBA Code:
With ActiveWorkbook.Worksheets("HOME")
   .Range("B13:J10000").Sort key1:=.Range("B13"), order1:=xlDescending, Header:=xlYes
End With
 
Upvote 0
Solution
Well... you are the Man! Thanks
I forgot to close the door at the end... :)
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,221
Messages
6,123,701
Members
449,117
Latest member
Aaagu

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