VBA - 日期和时间不等式

时间:2016-06-23 13:35:53

标签: excel vba

我有一个excell电子表格,其日期和时间数据格式为(2016-05-14 10:34:28.0)。我似乎无法在相同格式的两个单元格之间创建一系列不等式。我从未遇到过具有日期和时间的单元格,也不知道在VBA中给出它们的变量值的类型。我的目标是使用不等式比较一系列具有相同日期/时间格式的列,并确定到期日。

For i = 4 To numrows

start_time_day = DateValue(WorksheetFunction.Text(updater.Cells(i, 12), "dd/mm/yyyy"))
start_time_time = TimeValue(WorksheetFunction.Text(updater.Cells(i, 12), "hh:mm:ss"))
latest_due_day = DateValue(WorksheetFunction.Text(updater.Cells(i, 21), "dd/mm/yyyy"))
latest_due_time = TimeValue(WorksheetFunction.Text(updater.Cells(i, 21), "hh:mm:ss"))
latest_commit_day = DateValue(WorksheetFunction.Text(updater.Cells(i, 22), "dd/mm/yyyy"))
latest_commit_time = TimeValue(WorksheetFunction.Text(updater.Cells(i, 22), "hh:mm:ss"))
order_taken_day = DateValue(WorksheetFunction.Text(updater.Cells(i, 10), "dd/mm/yyyy"))
order_taken_time = TimeValue(WorksheetFunction.Text(updater.Cells(i, 10), "hh:mm:ss"))
compdate_day = DateValue(WorksheetFunction.Text(updater.Cells(i, 1), "dd/mm/yyyy"))
compdate_time = TimeValue(WorksheetFunction.Text(updater.Cells(i, 1), "hh:mm:ss"))
arrival_day = DateValue(WorksheetFunction.Text(updater.Cells(i, 16), "dd/mm/yyyy"))
arrival_time = TimeValue(WorksheetFunction.Text(updater.Cells(i, 16), "hh:mm:ss"))

servreqdesc = updater.Cells(i, 17)                                        'PULL INFO FROM UPDATE FILE
restake = updater.Cells(i, 30)
onproj = updater.Cells(i, 31)
workloc = updater.Cells(i, 35)
staked = updater.Cells(i, 6)
mdcomp = updater.Cells(i, 29)
compstatus = updater.Cells(i, 20)
start_time = start_time_day + start_time_time
latest_due = latest_due_day + latest_due_time
latest_commit = latest_commit_day + latest_commit_time
order_taken = order_taken_day + order_taken_time
compdate = compdate_day + compdate_time
arrival = arrival_day + arrival_time




'----------------------------Date Missed-----------------------------------
ElseIf (servreqdesc = "FIELD\MISSDIG\ELEC\M" Or servreqdesc = "FIELD\MISSDIG\GAS\M") Then
    ElseIf (restake = "Y" Or servreqdesc = "FIELD\MISSDIG\GAS\EM" Or servreqdesc = "FIELD\MISSDIG\ELEC\E") Then
        If (start_time = "(null)" And latest_due < order_taken) Then
            em_time = order_taken
            ElseIf (start_time = "(null)" And latest_due > order_taken) Then
                em_time = latest_due
                ElseIf start_time > order_taken Then
                        em_time = start_time
                        Else
                        em_time = order_taken
                    End If

1 个答案:

答案 0 :(得分:0)

日期只是数字。

  • 1天= 1
  • 1小时= 1/24
  • 1分钟= 1/24/60
  • 1秒= 1/24/60/60
  • 值1 = 1/1/1900
  • -1导致#####,因为不允许使用否定

Here is a list of VBA Date Functions