VBA Excel: sorting data in chronological order

Let say there is database in the sheet from row A to row F with some lines that are duplicated.


Before applying macros :
96.18      96.18      96.15      96.15      5620      5/12/2015 13:12
96.175      96.185      96.17      96.18      3904      5/12/2015 13:13
96.15      96.175      96.15      96.17      6790      5/12/2015 13:14
96.16      96.17      96.15      96.15      4803      5/12/2015 13:15
96.14      96.165      96.13      96.16      5560      5/12/2015 13:16
96.12      96.14      96.11      96.14      4051      5/12/2015 13:17
96.105      96.13      96.105      96.12      5312      5/12/2015 13:18
96.06      96.1      96.06      96.1      4202      5/12/2015 13:19
96.08      96.08      96.03      96.05      4969      5/12/2015 13:20
96.09      96.09      96.07      96.0701      1801      5/12/2015 13:21
96.11      96.115      96.09      96.09      7311      5/12/2015 13:22
96.11      96.11      96.11      96.11      1550      5/12/2015 13:23
96.1      96.11      96.09      96.105      9401      5/12/2015 13:24

96.08      96.08      96.03      96.05      4969      5/12/2015 13:20
96.09      96.09      96.07      96.0701      1801      5/12/2015 13:21
96.11      96.115      96.09      96.09      7311      5/12/2015 13:22
96.11      96.11      96.11      96.11      1550      5/12/2015 13:23
96.1      96.11      96.09      96.105      9401      5/12/2015 13:24

96.11      96.12      96.09      96.095      3902      5/12/2015 13:25
96.11      96.12      96.1      96.11      3100      5/12/2015 13:26
96.11      96.11      96.1      96.11      4065      5/12/2015 13:27
96.15      96.15      96.1101      96.1101      4917      5/12/2015 13:28
96.14      96.14      96.135      96.14      2000      5/12/2015 13:29
96.11      96.135      96.11      96.13      6704      5/12/2015 13:30
96.115      96.13      96.104      96.104      3803      5/12/2015 13:31
96.105      96.12      96.105      96.1138      5170      5/12/2015 13:32
96.12      96.135      96.12      96.12      6859      5/12/2015 13:33
96.14      96.14      96.12      96.121      2650      5/12/2015 13:34
96.12      96.15      96.12      96.14      6507      5/12/2015 13:35
96.13      96.13      96.12      96.12      2200      5/12/2015 13:36


With experts help,  I wrote VBA macros that removes these duplication in data.

Sub RemoveDupes()
Dim lngLastRow As Long

With ActiveSheet
    lngLastRow = ActiveSheet.UsedRange.Rows.Count
    .Columns("A:A").Select
    .Sort.SortFields.Clear
    .Sort.SortFields.Add Key:=Range("A1"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With .Sort
        .SetRange Range("A1:F" & lngLastRow)
        .Header = xlNo
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    
    .Columns("A:F").Select
    .Range("A1:F" & lngLastRow).RemoveDuplicates Columns:=Array(1, 2, 3, 4, 5, 6) _
        , Header:=xlNo
    
End With

End Sub

Open in new window



After applying macros :

96.06      96.1      96.06      96.1      4202      5/12/2015 13:19
96.08      96.08      96.03      96.05      4969      5/12/2015 13:20
96.09      96.09      96.07      96.0701      1801      5/12/2015 13:21
96.1      96.11      96.09      96.105      9401      5/12/2015 13:24
96.105      96.13      96.105      96.12      5312      5/12/2015 13:18
96.105      96.12      96.105      96.1138      5170      5/12/2015 13:32
96.11      96.115      96.09      96.09      7311      5/12/2015 13:22
96.11      96.11      96.11      96.11      1550      5/12/2015 13:23
96.11      96.12      96.09      96.095      3902      5/12/2015 13:25
96.11      96.12      96.1      96.11      3100      5/12/2015 13:26
96.11      96.11      96.1      96.11      4065      5/12/2015 13:27
96.11      96.135      96.11      96.13      6704      5/12/2015 13:30
96.115      96.13      96.104      96.104      3803      5/12/2015 13:31
96.12      96.14      96.11      96.14      4051      5/12/2015 13:17
96.12      96.135      96.12      96.12      6859      5/12/2015 13:33
96.12      96.15      96.12      96.14      6507      5/12/2015 13:35
96.13      96.13      96.12      96.12      2200      5/12/2015 13:36
96.14      96.165      96.13      96.16      5560      5/12/2015 13:16
96.14      96.14      96.135      96.14      2000      5/12/2015 13:29
96.14      96.14      96.12      96.121      2650      5/12/2015 13:34
96.15      96.175      96.15      96.17      6790      5/12/2015 13:14
96.15      96.15      96.1101      96.1101      4917      5/12/2015 13:28
96.16      96.17      96.15      96.15      4803      5/12/2015 13:15
96.175      96.185      96.17      96.18      3904      5/12/2015 13:13
96.18      96.18      96.15      96.15      5620      5/12/2015 13:12

As a matter of fact, the macros working correctly. It removes duplication but it also sort the data in some way that destroys chronological order in data.

My question is what should I change in my macros in order that data where sorted in chronological order after all.

Many thanks in advance !
SunnyXAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rgonzo1971Commented:
Hi,

pls try

Sub RemoveDupes()
Dim lngLastRow As Long

With ActiveSheet

    lngLastRow = ActiveSheet.UsedRange.Rows.Count
    .Columns("A:F").Select
    .Range("A1:F" & lngLastRow).RemoveDuplicates Columns:=Array(1, 2, 3, 4, 5, 6) _
        , Header:=xlNo
        

    .Sort.SortFields.Clear
    .Sort.SortFields.Add Key:=Range("F1"), _
    SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With .Sort
        .SetRange Range("A1:F" & lngLastRow)
        .Header = xlNo
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
 
    
End With

End Sub

Open in new window

Regards

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SunnyXAuthor Commented:
many thxs !
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.