简体   繁体   中英

Using VBA DateDiff to compare multiple Dates, output the difference and then compare output against another cell

I've got a spreadsheet which is used to record multiple times/dates where services were rendered.

In the spreadsheet the columns I'm interested in comparing start at row 9, column BA-BB, BC-BD, BE-BF, BG-BH, BI-BJ, BK-BL, BM-BN, BO-BP, BQ-BR for each of the rows in minutes. I then want to add all the total differences between the dates and finally compare that total with with AF9 if populated or if that cell is blank AG9.

I want the Macro to loop through all the rows producing a total units for each row at the end of the sheet (Column BU)

The purpose of the spreadsheet is to check that the value populated in either AF or AG is in fact correct if we were to work out the difference in times and convert to units anyway.

What I've been working on so far is:

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

The above is my shoddy attempt at amending some logic someone else provided on the forums to a similar question. I don't want to compare specific dates I enter though as the dates will all be different throughout the cells.

I've never used this type of function before in VBA so not really sure on how to go about changing it to suit my needs. If I can manage to loop through of of the start/end times I can probably work out how to loop through additional columns and compare against another 2 columns after that.

Some sample date is:

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

Difference = (In minutes)

Compare Difference to:

Total Units(Column AF) = 600(this is 600 minutes)

Sorry that this is such a long question. I'm just really stuck with getting started on this problem

I like your attempt, you are on the right track. Below is tested sample code, which I think will provide you with the answer you're seeking. Good luck and happy coding

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM