Link to home
Start Free TrialLog in
Avatar of SunnyX
SunnyX

asked on

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 !
ASKER CERTIFIED SOLUTION
Avatar of Rgonzo1971
Rgonzo1971

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SunnyX
SunnyX

ASKER

many thxs !