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?
 

Some videos you may like

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
52,833
Office Version
  1. 365
Platform
  1. Windows
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
 
Solution

Alex Piotto

Board Regular
Joined
Jul 5, 2016
Messages
82
Office Version
  1. 2007
Platform
  1. Windows
Well... you are the Man! Thanks
I forgot to close the door at the end... :)
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
52,833
Office Version
  1. 365
Platform
  1. Windows
You're welcome & thanks for the feedback.
 

Watch MrExcel Video

Forum statistics

Threads
1,122,354
Messages
5,595,672
Members
414,008
Latest member
zcaa0g

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