从未来12小时到期的数据列表中排序

时间:2016-05-29 14:24:28

标签: excel-vba vba excel

我有一个数据列表,其中包含“Due”列,格式为mm / dd / yyyy hh:mm。我希望对此数据进行排序,以仅列出具有特定列的行,这些行将在接下来的12小时内到期。

1 个答案:

答案 0 :(得分:0)

在将其更改为适合您的电子表格后,查看这是否符合您的要求。

此外,这样做,您必须手动运行此宏以查看基于当前时间的更新排序。您必须将此宏设置为在某个时间间隔过后运行 - 比如每小时一次。这可能包括编写一行代码,如Application.OnTime Now + TimeValue("00:00:01"), "test",如果你想每1分钟运行一次宏。我会让你试验一下。

Public Sub test()

Dim list() As String

RightNow = Now

For i = 0 To 11 'iterate loop 12 times (includes 0) for each of the next 12 hours.
    ReDim Preserve list(i)
    list(i) = Format(DateAdd("h", 1, RightNow), "m/dd/yyyy hh:mm")
    RightNow = DateAdd("h", 1, RightNow)
Next

On Error Resume Next 'used for the sake of the next line if there are not filters and all data is already being shown - otherwise the next line would cause an error.
ActiveSheet.ShowAllData 'removes all filters in activesheet, so that next line will be able to delete the 5th custom list (if there is only one custom list).

Application.DeleteCustomList (5) 'delete old list array (assumes that this array's custom list index is always 5.

Application.AddCustomList Array(list()) 'add array, list, as custom list that can be used to sort data.

ActiveWorkbook.Worksheets("Sheet1").AutoFilter.Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").AutoFilter.Sort.SortFields.Add Key:=Range("A:A"), SortOn:=xlSortOnValues, Order:=xlAscending, CustomOrder:=5, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet1").AutoFilter.Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With

End Sub