VBA Excel Sort

I am attempting to have a vba code check the dates in column b and arrange them from soonest to latest. The code I have so far is
    Range("B1:B37").Select
    ActiveWorkbook.Worksheets("FUN1").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("FUN1").Sort.SortFields.Add Key:=Range("B1"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("FUN1").Sort
        .SetRange Range("A1:C10")
        .Header = xlNo
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With

Open in new window


And this code seems to sort the data, but not always in order. An example of the data I am working with is:

RED                11/28/2017                7.99
BLUE              11/25/2017               10.99            
BLUE              11/26/2017               10.99
RED                12/15/2017                7.99
ORANGE        11/30/2017               13.99
GREEN           12/28/2017                5.99


And the desired output is:

BLUE              11/25/2017               10.99
BLUE              11/26/2017               10.99
RED                11/28/2017                7.99
ORANGE        11/30/2017               13.99
RED                12/15/2017                7.99
GREEN           12/28/2017                5.99
david franciscoAsked:
Who is Participating?
 
Ejgil HedegaardConnect With a Mentor Commented:
If the dates are text, and not real dates, the sort will be wrong.
Select the dates in column B, and use "Text to columns" on the data tab.
Then try again.

You can also make a formula in column D
=ISNUMBER(B1)
Should return True.
If not use above.
0
 
Roy CoxConnect With a Mentor Group Finance ManagerCommented:
An example workbook would be helpful
0
 
david franciscoAuthor Commented:
I unfortunately do not have access to the original workbook on this PC
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
Martin LissConnect With a Mentor Older than dirtCommented:
I created a workbook that included the unsorted data you show. When I ran your code it produced the desired output so something else must be going on.
0
 
Martin LissConnect With a Mentor Older than dirtCommented:
Here's a copy of that workbook. Note that sheet 'FUN1 (2)' is a backup copy of the unsorted data.
29070277.xlsm
0
 
david franciscoAuthor Commented:
When I apply another line, it will refresh like this:

 RED                11/28/2017                7.99
 BLUE              11/25/2017               10.99            
 BLUE              11/26/2017               10.99
 RED                12/15/2017                7.99
 ORANGE        11/30/2017               13.99
 RED                12/30/2017                7.99
 GREEN           12/6/2017                5.99
 ORANGE        12/15/2017               13.99

The last red in the sequence is not being placed as the last item
0
 
Martin LissConnect With a Mentor Older than dirtCommented:
Once again when I run your code it works correctly for me.
0
 
Martin LissConnect With a Mentor Older than dirtCommented:
Ejgil is correct. If I manually type in the 'dates' in a column formatted as Text, the sorting does not work properly.
0
 
david franciscoAuthor Commented:
Fair enough, I will try to trouble shoot. Thanks for the support!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.