VBA Excel Sort

david francisco
david francisco used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Roy CoxGroup Finance Manager
Commented:
An example workbook would be helpful

Author

Commented:
I unfortunately do not have access to the original workbook on this PC
Martin LissOlder than dirt
Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
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.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Martin LissOlder than dirt
Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
Here's a copy of that workbook. Note that sheet 'FUN1 (2)' is a backup copy of the unsorted data.
29070277.xlsm
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.

Author

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
Martin LissOlder than dirt
Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
Once again when I run your code it works correctly for me.
Martin LissOlder than dirt
Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
Ejgil is correct. If I manually type in the 'dates' in a column formatted as Text, the sorting does not work properly.

Author

Commented:
Fair enough, I will try to trouble shoot. Thanks for the support!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial