Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

VBA for checking Missing and Repeat Time

0

 I need a help to make a macro to find missing time and repeated time from a time period 9:00 am to 9:00 Pm.

macro should show the repeated time and missing time

sample file attached

Answer
Discuss

Discussion

Hello john666 and welcome to the forum,

I had a look at your sample file and need a bit more information before I work on a solution.

In your file you have 2:51 PM – 3:14 PM (row 32) highlighted but neither of those times are repeated. You have 1:40 PM – 2:18 PM boldened but neither of those times are repeated. Start Times 4:50 PM, 1:40 PM, and 7:20 PM are repeated in the End Time column. Are those the type of repeats you want to highlight? In your file no time entry in the Start column is repeated in the Start column, and no time entry in the End column is repeated in the End column. Is it possible a time entry could be repeated in the Start column or End column?  

Rows 5, 7, 9, 11, 12, 14, 22, 23, 27, and 30 are missing time entries but are not highlighted. Are these the type of “missing time” that you want to highlight? Or are you looking to highlight rows with a Start and no End, and an End but no Start? In your sample file both Start and End have a time entry, or both are blank.

Please clarify which type of repeats and missing time you want flagged and how you want them flagged/identified.

Cheers   :-)
WillieD24 (rep: 723) Aug 11, '25 at 11:03 am
Thank you for your response.  I just given that excel sheet for just show define column and row where data coming in the sheet. Don’t consider any highlighted Columns and Bold Letters. That is just showing for break times. Repeated times need to be highlighted.
Ie 1:40 pm to 2:30 PM  
Suppose if any row there is 1:40 PM to 2:30 PM time shown and then another row there is 1:40 PM to 3:40 PM time shown to be highlighted.
Work Star Time may be 9:00 Am or 9:15 AM and it will end at 8:00 PM and 1 hour break for Lunch. So Sum of time  should be 10 hours.  So may be in some columns
 1.       9:15 AM – 10:00 AM
 2.      10:01 AM – 10:20 AM
 3.       10:26 AM – 11:45AM
 4.       11:46 AM – 12:35 PM
 5.       12:36 PM – 1:35 PM
 6.       10:50 AM – 11:10 AM  
 Here is one issue this work start at 10:26 am and ends on 11:45am below column another work start at 10:50 AM to 11:10 AM so time repeated here.
So it should be highlighted because I want it and make it as below
10:26 AM – 10:49 AM
11:11 AM – 11:45 AM
 I just like to check the continuity of the time without time repeated.
Also there is one work stop at 10:20 am and another work stark at 10:26 am this also need to be notified by macro. see above 2 & 3 number given
john666 (rep: 2) Aug 12, '25 at 8:51 am
Thanks for updating. I’m still a bit confused.
Using your first example of 1:40 to 2:30 and 1:40 to 3:40. Do you want the 1:40 - 2:30 highlighted or 1:40 - 3:40 highlighted or both highlighted?
In your second example of 10:26 – 11:45 and 10:50 – 11:10, which time span should be highlighted? Or both?
Also, do you want Stop time of 10:20 and Start time of 10:26 both highlighted?
And lastly, are you wanting the macro to make changes to the time values entered?

Cheers   :-)
WillieD24 (rep: 723) Aug 12, '25 at 11:40 am
Thank you for your reply.

all difference should need to highlight or copy that row to a blank space.
Overlaping time is good to highlighted with one colour.
ie Overlaping time or repeated time as follow need to highlighted with one colour
11:46 AM – 12:35 PM
10:50 AM – 11:10 AM  

Missing Times need to highlighted with one colour
Ie 10:01 AM - 10:20 AM
     10:26
work start at 10:01 and end with 10:20 then again start at 10:26 so there is 6 minutes missing .
john666 (rep: 2) Aug 13, '25 at 12:51 am
@John
Thanks for responding well to Willie's questions. I think he'll provide a good Answer (solution) and hopefully you will reward his efforts later by Selecting his Answer.

@Willie
I'll leave you to answer this one, given you're collecting the necessary clarifications. I'll take a look occasionally, just in case you don't solve but won't given an Answer unless you don't succeed (which I doubt). Good luck. 
John_Ru (rep: 6792) Aug 13, '25 at 2:01 am
@john666,
Thanks for the update. Unfortunately I won't be able to work on this until next week. How many rows will this table have? Or is there no limit? To highlight each overlapping time span with a different colour would be quite complicate and something I am not going to attempt. I am modestly confident I can come up with a solution for you.

@John_Ru
Thanks again for props. ;-)   Since it will be a few days before I can get back to this, if you have a solution then have it.

Cheers   :-)
WillieD24 (rep: 723) Aug 13, '25 at 12:16 pm
All overlapping time span with one colour and all missing time with one colour
john666 (rep: 2) Aug 14, '25 at 1:07 am
@john666,
Thanks for that; that is what I was planning on doing.
I'll check back next week.

Chers   :-)
WillieD24 (rep: 723) Aug 14, '25 at 9:20 am
Please see my Amswer and revised file.
John_Ru (rep: 6792) Aug 18, '25 at 4:03 pm
@john666

I have come up with a solution for you  --  see below.

Cheers   :-)
WillieD24 (rep: 723) Aug 18, '25 at 5:28 pm
John666- please review the Answers from Willie and me.

We have each spend some hours trying to solve your problem so might expect the courtesy of a reply. Thanks in advance.
John_Ru (rep: 6792) Aug 20, '25 at 8:18 am
Add to Discussion

Answers

0
Selected Answer

John

In the attached revised file, you'll see I have:

  1. added new Sheet 2 (currently blank but with some headings) and
  2. on Sheet 1...
    • added a new column for "Optional task name" (and a formula to write "Break" in column 4)
    • made some text changes (in red).
    • added a blue button captioned "Check for missing or overlapping times"

If (with macros enabled) you click that button, it will - on Sheet 2- sort the breaks and tasks in ascending order and add gap/ overlap comments (in columns E and F) plus colours. Hopefully this sorted list is more visual and so easier to understand.

Clicking that button runs this code (with explanatory comments):

Option Base 1
Dim Times As Variant, p As Long

Sub GetTimes()
    Dim FrstRw As Long, LstRw As Long, n As Long

    ' Declare first row for times and reset p
    FrstRw = 4
    p = 0

    'Get last used row in column B (2)
    With Sheet1
        LstRw = .Cells(.Rows.Count, 2).End(xlUp).Row
        ' Size array (with maximum rows, 4 columns)
        ReDim Times(LstRw - FrstRw + 1, 4) As Variant

        ' read from first to last row and add any time pairs to array
        For n = FrstRw To LstRw
        ' check if there's a time in columns 2 and 3
            If .Cells(n, 2).Value > 0 And .Cells(n, 3).Value > 0 Then
                ' if so, add values to array but first...
                ' ...increment array size counter
                p = p + 1
                Times(p, 1) = .Cells(n, 2)
                Times(p, 2) = .Cells(n, 3)
                Times(p, 3) = .Cells(n, 4)
                Times(p, 4) = .Cells(n, 5)
            End If
        Next n
        ' trim array, retaining values (transposing twice)
        Times = WorksheetFunction.Transpose(Times)
        ReDim Preserve Times(4, p)
        Times = WorksheetFunction.Transpose(Times)

    End With


    'paste sorted array
    With Sheet2
        ' clear any data and colours (except row 1)
        .UsedRange.Offset(1, 0).ClearContents
        .UsedRange.Offset(1, 0).Interior.ColorIndex = xlNone
        .Cells(2, 1).Resize(p, 4).Value = Times
        ' sort by start time
        .UsedRange.Offset(1, 0).Sort _
            Key1:=.Cells(1, 1), Order1:=xlAscending, Key2:=.Cells(1, 2), Order2:=xlAscending, Header:=xlNo
        ' add sorted data to array
        Times = .UsedRange.Offset(1, 0)

        ' use sub to find time gaps
        Call FindTimeGaps

        ' use sub to find overlapped tasks
        Call FindOverlaps

        ' resize columns
        .Columns("A:F").AutoFit
        ' switch to results
        .Activate
        ' create borders
        .UsedRange.Borders.LineStyle = xlContinuous
    End With

    MsgBox p & " time pairs sorted by start (then end) times- see sheet 2"

End Sub

The lines in bold above call other procedures; this one find gaps between successive tasks:

Private Sub FindTimeGaps()
    Dim LstTime As Variant, n As Long

    'set last time
    LstTime = "21:00:00"

    With Sheet2
        'check first entry
        If .Cells(2, 1) > CDec(TimeValue("09:00:00")) Then
                .Cells(2, 5) = "Possible gap from 09:00 AM"
                ' make columns 1 and 5 yellow
                .Cells(1, 2).Interior.ColorIndex = 6
                .Cells(2, 5).Interior.ColorIndex = 6
        End If

        'loop down rows
        For n = 2 To p
            If CDec(.Cells(n + 1, 1)) > CDec(.Cells(n, 2) + TimeValue("00:01") * 1.0001) Then
                .Cells(n, 5) = "Possible gap from " & Format(.Cells(n, 2) + TimeValue("00:01"), "hh:mm AM/PM") & " to " & Format(.Cells(n + 1, 1) - TimeValue("00:01"), "hh:mm AM/PM")
                ' make columns 2 and 5 yellow
                .Cells(n, 2).Interior.ColorIndex = 6
                .Cells(n, 5).Interior.ColorIndex = 6
            End If
        Next n
        ' check last row
        If CDec(.Cells(n, 2)) < CDec(TimeValue(LstTime)) Then
            .Cells(n, 5) = "Gap from " & Format(.Cells(n, 2) + TimeValue("00:01"), "hh:mm AM/PM") & " to " & Format(LstTime, "hh:mm AM/PM")
            .Cells(n, 2).Interior.ColorIndex = 6
            .Cells(n, 5).Interior.ColorIndex = 6
        End If
    End With

End Sub

This detects the overlaps:

Private Sub FindOverlaps()

    With Sheet2
        'loop down rows
        For n = 1 To p
            'check array for later overlaps
            For m = 1 To p
                 If m <> n And ((Times(m, 1) < Times(n, 1) And Times(m, 2) > Times(n, 2)) _
                    Or (Times(m, 1) >= Times(n, 1) And Times(m, 2) <= Times(n, 2)) _
                    Or (Times(m, 1) < Times(n, 2) And Times(m, 2) > Times(n, 2))) _
                    Then
                    .Cells(m + 1, 6) = "Overlaps with task starting " & Format(Times(n, 1), "hh:mm AM/PM")
                    ' make columns 1 and 6 orange
                    .Cells(m + 1, 1).Interior.ColorIndex = 46
                    .Cells(m + 1, 6).Interior.ColorIndex = 46
'                    ' clear gap comment and colour
'                    .Cells(m + 1, 5) = ""
'                    .Cells(m + 1, 2).Interior.ColorIndex = xlNone
'                    .Cells(m + 1, 5).Interior.ColorIndex = xlNone
                End If
                ' stop if this start exceeds task n end
                If CDec(Times(m, 1)) > CDec(Times(n, 2)) + TimeValue("00:01") Then
                    Exit For
                End If
            Next m
        Next n

    End With

End Sub

where the key bit is an "If" test to detect the three possible overlap conditions:

  1. task finishes within another task
  2. task starts and finishes within another task
  3. task starts within another task.

If you make the tasks on Sheet 1 longer (or smaller) to create overlaps (or gaps), you will need to click the button again to see new results.

Hope this solves your problem. If so, please be sure to mark this Answer as Selected.

Discuss

Discussion

John

Nice solution. I took a slightly different approach which looks at all four possible overlap conditions and lists the pair of overlapping time spans in a table and indicates which type of overlap they are.

Cheers   :-)
WillieD24 (rep: 723) Aug 18, '25 at 5:31 pm
@Willie - sounds good, nice work!

Thanks for reminding John that he should select one of our solutions if they suit (I didn't since I wasn't sure you'd post an. Answerr). 
John_Ru (rep: 6792) Aug 18, '25 at 5:48 pm
Thanks for selecting my Answer, John. To be honest, both Willie and I thought you might not respond (sadly too many people don't). 

In your comment to Willie, you said abiut my code "There is some correction, I will discuss it tomorrow". Please note that if that means discussing with me, kindly note that I have a very busy day tomorrow so will take some time to respond. 
John_Ru (rep: 6792) Aug 21, '25 at 10:08 am
Hi John,
Nice job scoring another win.
I took a closer look at your solution and noticed what I consider a flaw. If a row is missing a Start Time or End Time entry then that row is not included in the sorted results. This results in a missing Start or End time not being flagged/highlighted. It's for this reason that I chose to not incorporate sorting in my solution.

Cheers   :-)
WillieD24 (rep: 723) Aug 21, '25 at 12:45 pm
@Willie -  thanks but "another win" suggests we're competing - I don't see it that way. Agreed that I didn't highlight any missing start or end times but the user didn't ask for that and I did say my solution worked with "time pairs" (so a start and an end). If either is missing, a larger gap would be indicated (and so need investigating I guess).. That said, it might be his discussion point. 
John_Ru (rep: 6792) Aug 21, '25 at 7:32 pm
@John_Ru,
Sorry for phrasing my comment like that. I don't see it a a competition. We are just two people who get pleasure from helping others.

Cheers   :-)
WillieD24 (rep: 723) Aug 21, '25 at 11:37 pm
@Willie - nicely put ;that is just how I see it.
John_Ru (rep: 6792) Aug 22, '25 at 1:20 am
Add to Discussion
0

@ john666,

I've put together a file that I think answers your questions.

Your first request was to have a macro to highlight entries which overlap in some way. There are 4 different types of overlap (I have explained this in the file). On the sheet "Revision" I have added a table which lists the pairs of time spans which overlap and which type of overlap it is.

Your second request was to have a macro to find cells missing a time entry. I have included one in the attached file, but in the attached file I have taken care of this using Conditional Formatting. If you delete a time entry the cell will turn purple. Likewise, if you enter a time value the cell fill goes away. 

There is also a "Reset" button which removes highlighting from the time columns and clears the "Overlap" table so you can do a re-check after any changes are made.

John_Ru has also provided an answer. After you look at both of them, please mark the one you like best as Selected.

Cheers   :-)

Discuss

Discussion

@Willie - just tried your file but was puzzled. You have a button captioned "Find Missing Time Entries" (which it does) but I thought the user wanted to find gaps in the time periods e.g. in the  Discussion under the Question, john666 says:

"Missing Times need to highlighted with one colour Ie 10:01 AM - 10:20 AM      10:26 work start at 10:01 and end with 10:20 then again start at 10:26 so there is 6 minutes missing ."

That said, I wouldn't spend any more time refining your Answer- sadly it looks like this user may not be responding.
John_Ru (rep: 6792) Aug 20, '25 at 8:26 am
@John_Ru
I interpreted "missing time" to be  a row missing either a Start time or an End time. The macro behind the button does this, but as noted in the text box beside the button I chose to highlight the empty cells using conditional formatting.

As for spending more time on this, that won't happen unless we get some feedback from john666. Sadly, I agreewith you that it seems john666 (like some other recent posters) isn't going to respond.

Cheers   :-)
WillieD24 (rep: 723) Aug 20, '25 at 11:16 am
Thanks for Helping .
Code from @john_Ru is suitable for me. 
Thank you so much for helping me
Very correct code for me.
Thanks so much
john666 (rep: 2) Aug 21, '25 at 3:57 am
thanks for Helping .
Code from @john_Ru is suitable for me. 
Thank you so much for helping me
Very correct code for me.
Thanks so much
Thanks both of you 
john666 (rep: 2) Aug 25, '25 at 10:48 am
@john666 - thank you for your feedback 
@Willie - it's nice to see a user being grateful for the efforts we take to solve problems. Your solutions always offer alternative ways of cracking a problem and provoke thinking. 
John_Ru (rep: 6792) Aug 25, '25 at 1:05 pm
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login