Sorting Issue - not working as expected

kweaver

Well-known Member
Joined
May 12, 2009
Messages
2,940
Office Version
  1. 365
I have a simple sheet that looks like this:

Sort1.jpg


more data

Sort2.jpg


And, I'm using the following code within other code in VBA:

Code:
 With ActiveSheet.Sort
     .SortFields.Add Key:=Range("J1"), Order:=xlAscending
     .SortFields.Add Key:=Range("C1"), Order:=xlAscending
     .SetRange Range("A1:V" & LRX)
     .Header = xlYes
     .Apply
End With

LRX is 39 in the sample case above.

I want the data to be ordered by DATE (J column), then ordered by the values in the C column, but I'm getting this strange result:

Sort3.jpg


Even when I do the reverse sort keys, I still get a mix up of the data.

Sort4.jpg
 
Last edited:

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
I can't see anything strange about your result, it sorted by date & then by col C.

I would also suggest adding this line
Code:
 With ActiveSheet.Sort
     [COLOR=#ff0000].SortFields.Clear[/COLOR]
     .SortFields.Add Key:=Range("J1"), Order:=xlAscending
Otherwise you could end up with the situation where the workbook needs to be repaired.
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,771
Members
452,353
Latest member
strainu

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