在文本中按日期对列进行排序

时间:2016-05-20 17:28:00

标签: excel-vba sorting vba excel

我拥有什么:
在Excel sheet1中,我有一个表格,其中包含A列中的日期和列B中的公司名称。

我想要的是什么:
我希望在sheet2中删除重复项的简短列表,其中包含日期和公司名称,但我不能这样做(月份在btw中间)。

我的解决方案不完整:
我的工作是创建一个列C,它是列AB的连接,然后我创建了一个宏来复制粘贴我的列C,同时删除重复项。这一步可能是可以避免的,我也欢迎其他解决方案。

我的解决方案不完整:

Date   and company name
02/02/2016 Company A
02/03/2016 Company C
04/03/2016 Company B
05/01/2016 Company D
05/01/2016 Company E
05/02/2016 Company F
05/04/2016 Company Z
06/04/2016 Company Y
07/03/2016 Company M
12/01/2016 Company Q

我想要的是什么:

我希望按日期按实际顺序排序,但我的排序失败,因为它无法识别日期!

Date and   company name
05/01/2016 Company D
05/01/2016 Company E
12/01/2016 Company Q
02/02/2016 Company A
05/02/2016 Company F
02/03/2016 Company C
04/03/2016 Company B
07/03/2016 Company M
05/04/2016 Company Z
06/04/2016 Company Y

如何在Excel-VBA中自动对列表进行排序?

我正在使用@Gary的学生代码稍作修改,以删除重复内容并转移到其他表单上 Unique list from dynamic range table with possible blanks

Sub Macro1_The_Sequel()
    Dim rng As Range

    Sheets("Sheet1").Range("C:C").Copy 'modified here to copy column C
    Sheets("Sheet2").Range("A1").PasteSpecial Paste:=xlPasteValues
    Sheets("Sheet2").Range("A:A").RemoveDuplicates Columns:=1, Header:=xlYes
    Set rng = Sheets("Sheet2").Range("A2:A" & Rows.Count)
    With Sheets("Sheet2").Sort
        .SortFields.Clear
        .SortFields.Add Key:=rng, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        .SetRange rng
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Call Kleanup
End Sub

Sub Kleanup()
    Dim N As Long, i As Long

    With Sheets("Sheet2")
        N = .Cells(Rows.Count, "A").End(xlUp).Row
        For i = N To 1 Step -1
            If .Cells(i, "A").Value = " " Then 'modified here because I needed to clean the rows that had space  because of my concatenate
                .Cells(i, "A").Delete shift:=xlUp
            End If
        Next i
    End With
End Sub

0 个答案:

没有答案