使用VBA DateDiff比较多个日期,输出差异,然后与另一个单元格比较输出

时间:2018-08-18 14:12:39

标签: excel vba excel-vba

我有一个电子表格,用于记录提供服务的多次/日期。

在电子表格中,我要比较的列从第9行开始,列BA-BB,BC-BD,BE-BF,BG-BH,BI-BJ,BK-BL,BM-BN,BO- BP,BQ-BR以分钟为单位。然后,我想添加日期之间的所有总计差,最后将该总计与AF9(如果已填充或该单元格为空白AG9)进行比较。

我希望宏循环遍历所有行,从而为工作表末尾(BU列)的每一行产生总计单位

电子表格的目的是,如果我们要计算出时间差并转换为单位,则检查AF或AG中填充的值实际上是否正确。

到目前为止,我一直在努力:

Sub CalculateDate()
Dim Result, RowNo As Long
Dim FirstDate, SecondDate As Date
Dim ws As Worksheet

 Set DateCompare = ActiveWorkbook.Sheets("Master")

Set DateCompareRng = Support.Range("BA2", Support.Cells(Rows.Count, "BA").End(xlUp).Offset(0, 18))

Set DateCompareArr = DateCompareRng.Value2

RowNo = 1

Do Until DateCompare.Cells(RowNo, 1) = ""

FirstDate = DateCompare.Cells(RowNo, 1)
SecondDate = DateCompare.Cells(RowNo, 2)

 DateCompareArr(FirstDate, 3) = DateDiff("m",   FirstDate, SecondDate)


RowNo = RowNo + 1

Loop

End Sub

以上是我在修改论坛上其他人提供的一些逻辑以解决类似问题方面的拙劣尝试。我不想比较输入的特定日期,因为整个单元格中的日期都不同。

我以前从未在VBA中使用过这种类型的功能,因此不确定如何进行更改以适应我的需求。如果我可以设法遍历开始/结束时间,那么我大概可以算出如何遍历其他列,然后与其他两列进行比较。

一些采样日期是:

    Start 1      |       Start 2
23/03/2018 12:00 | 2018-03-23 16:00 GMT

差异=(以分钟为单位)

比较差异为:

总单位(AF列)= 600(这是600分钟)

很抱歉,这是一个很长的问题。我只是真的开始着手解决这个问题

1 个答案:

答案 0 :(得分:1)

我喜欢您的尝试,您步入正轨。下面是经过测试的示例代码,我认为它将为您提供所需的答案。祝你好运,编码愉快

Public Sub CalculateDate()
    'While I don't recommend hard coding the start and end of your range
    'for this example, I thought it would simplify things.
    'Start of the range is the first cell, which in your example seems
    'like BA9
    Const RANGE_START As String = "BA9"

    'End of the range is the last cell in right most column, which
    'in your example was BR.  I chose the 18th row, but you could
    'make it whatever you need
    Const RANGE_END As String = "BR18"

    'Declare a worksheet variable as you've done
    'And set it to the worksheet in the ActiveWorkbook as you've done
    Dim ws As Worksheet
    Set ws = ActiveWorkbook.Sheets("Master")

    'Declare the range that contains the values you need to sum
    Dim rngToSum As Range

    'And set it to the range in the WorkSheet
    'In this case the range will be
    'ws.Range("BA9:BR18")
    Set rngToSum = ws.Range(RANGE_START & ":" & RANGE_END)

    'Declare an index to be used in the for loop below
    'as we loop through each column in the row the
    'code is summing
    Dim nDx As Integer

    'Declare a range for the row to be worked on
    Dim rngRow As Range
    'Declare a string value that will hold the
    'output range(row, cell)
    Dim outStr As String
    'Declare an output range variable
    Dim outRng As Range

    'Declare a variable to hold the summation of the
    'row values you want to add together
    Dim rowSum As Long

    'Outter loop to loop through each of the rows in the
    'defined range
    For Each rngRow In rngToSum.Rows
        'Initialize/Reinitialize the rowSum to 0 for each row
        rowSum = 0
        'Inner loop to loop throug all the columns in the range
        'you want to add together
        For nDx = 1 To rngToSum.Columns.Count Step 2
            'NOTE--> DateDiff uses lower case N for minutes, not lower case M
            'I noticed that in your sample code
            rowSum = rowSum + DateDiff("n", rngRow.Value2(1, nDx), rngRow.Value2(1, nDx + 1))
        Next
        'Completed adding all the columns together
        'Assign the outPut row, cell for the output Range
        'The formula below will concatenate the
        'letter A with the current row number
        'For example if the current row number is 9
        'outStr will equal A9
        outStr = "A" & rngRow.Row
        'I always use Value2 since it is faster than the
        'Text or Value properties of the range object
        ws.Range(outStr).Value2 = rowSum
    Next

End Sub