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

使用VBA DateDiff比较多个日期,输出差异,然后将输出与另一个单元格进行比较,vba,excel,Vba,Excel,我有一个电子表格,用于记录提供服务的多次/日期 在电子表格中,我感兴趣的是比较第9行、第BA-BB列、第BC-BD列、第BE-BF列、第BG-BH列、第BI-BJ列、第BK-BL列、第BM-BN列、第BO-BP列、第BQ-BR列中每一行的时间(分钟)。然后,我想添加日期之间的所有总差异,最后将总差异与AF9进行比较(如果已填充或单元格为空AG9) 我希望宏在所有行之间循环,为工作表末尾的每行生成一个总单位(BU列) 电子表格的目的是检查填充在AF或AG中的值是否正确,如果我们要计算时间差并转换为

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

在电子表格中,我感兴趣的是比较第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分钟)


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

我喜欢你的尝试,你走对了方向。下面是经过测试的示例代码,我认为它将为您提供您正在寻找的答案。祝你好运,快乐

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

他们是真的约会时间吗?您的示例数据不支持这一事实。一个可能是,另一个几乎可以肯定是看起来像datetime的文本。是的,它们是实时的。当电子表格提供给我时,他们会提供日期和时间,但如果是BST或GMT时间。老实说,我想我错过了第一次约会的格林尼治标准时间,这可能就是它们看起来像两种不同格式的原因。删除GMT/BST我可以用正则表达式轻松地完成。还有一点我不太明白。嗨,杰米,谢谢你的回答。非常感谢。当我运行它时,虽然它为这一行提供了“objectrequired”:rowSum=rowSum+DateDiff(“n”,rng.Value2(1,nDx),rng.Value2(1,nDx+1))-是因为我需要为总和设置输出单元格吗?是因为没有定义“rng”,请尝试
单元格(rngRow.row,nDx)改为.value2
。@rademicha同志,我用你的修正案来替换法典中的什么内容?我已尝试将您的修订输入为
rowSum=rowSum+DateDiff(“n”,Cells(rngRow.Row,nDx)。Value2(1,nDx),Cells(rngRow.Row,nDx)。Value2(1,nDx+1))
您好,抱歉,注意到rng.Value2应该是rngRow.Value2--mybad@JamieRiis这是一种享受,谢谢。我只需要将范围更改为动态范围,因为每个月的行数都会更改,但这应该不会太困难。主要问题按预期进行。再次感谢你的帮助。非常感谢!