基于日期时间间隔查询

时间:2016-05-25 00:52:21

标签: google-sheets

我想根据他的开始和结束日期时间找出在特定时间内可用的驱动程序。

我有两个工作表。

Worksheet1显示日期时间内驱动程序的开始和结束时间:

Function LabelToArray(LblCollection As Variant) As Variant()

    Dim myid As String
    Dim mycolumn As String
    Dim myrow As String
    Dim intLastRow As Integer
    Dim intLastCol As Integer
    Dim myRows As Variant
    Dim myColumns() As String

    ' Create first row
    ReDim myRows(0)

    ' For every child object in the collection
    For Each mychld In LblCollection

        ' Only execute for labels
        If mychld.Type = "GuiLabel" Then

            ' Get column and row of current label
            myid = Split(mychld.ID, "[")(4)
            myid = Mid(myid, 1, Len(myid) - 1)
            mycolumn = Split(myid, ",")(0)
            myrow = Split(myid, ",")(1)

            ' New row ? Reset column counter, set row counter and redim the array of rows
            ' This will fail spectacularly if SAP stop giving rows in numerical order
            If myrow > intLastRow Then intLastCol = 0: intLastRow = myrow: ReDim myRows(myrow)

            ' Reset or resize this line's columns array
            If intLastCol = 0 Then
                ' Set filled out 'myColumns' array into correct 'myRows' object
                If myrow <> 0 Then myRows(myrow - 1) = myColumns
                ' testing, print last column in this row
                If myrow <> 0 Then Debug.Print myColumns(UBound(myColumns))
                ' testing, print first column in this row
                If myrow <> 0 Then Debug.Print myColumns(0)
                ' testing, print number of columns
                If myrow <> 0 Then Debug.Print UBound(myColumns)
                ' Reset myColumns array because we're on a new line
                ReDim myColumns(0)
            Else
                ReDim Preserve myColumns(intLastCol)
            End If

            myColumns(intLastCol) = mychld.Text

            intLastCol = intLastCol + 1

            'Debug.Print mycolumn & "," & myrow

        End If

    Next

    ' Copy last row into array
    myRows(myrow) = myColumns

    ' SEEMS TO WORK FINE UP TO THIS POINT !

    Debug.Print UBound(myRows)
    Debug.Print myRows(0)(0)  'THIS LINE FAILS, TYPE MISMATCH (run-time error 13) ! I also tried cstr(myRows(0)(0))

    LabelToArray = myRows

End Function

Worksheet2显示游览的开始日期

Driver   | Start Time       | End Time
------------------------------------------
Driver 1 | 25-05-2015 09:00 | 25-05-2015 15:00
Driver 2 | 25-05-2015 15:00 | 25-05-2015 21:00
Driver 2 | 26-05-2015 09:00 | 26-05-2015 15:00
Driver 1 | 26-05-2015 12:00 | 26-05-2015 17:00

我想在工作表2中查询工作表1中的哪个驱动程序在巡视开始时可用。

Tour 1 | 25-05-2015 11:00
Tour 2 | 25-05-2015 16:00
Tour 3 | 25-05-2015 17:00
Tour 4 | 26-05-2015 09:00

但是我知道我需要使用timedate(获取开始和结束时的实际时间),我需要查询一个间隔。

1 个答案:

答案 0 :(得分:2)

如果您想使用日期和时间,则需要datetime而不是date。并且由于存在间隔,因此应该对列B进行另一次比较。查询字符串(为了便于阅读而显示为换行符)将是

"select A 
   where B <= datetime '" & text(A2, "yyyy-MM-dd hh:mm:ss") & "' 
     and C >= datetime '" & text(A2, "yyyy-MM-dd hh:mm:ss") & "'"

这可确保A2中的日期时间介于B列和C列的日期时间之间。