Duplicate Names Between Two Date Ranges

Viper7513

New Member
Joined
Aug 30, 2010
Messages
15
Good afternoon all,


I am trying to discover a way of highlighting a name (in this example in Column C) if the person has a task that clashes between dates.
So in the table below, Person A has a task running from 01/01/2022 until 01/03/2022 (ROW 1), but they also have a second task that has a date which falls within this range already (ROW 4 - 27/02/2022 - 01/04/2022) and so therefore, Person A cannot be assigned to a this task. So if they are selected, I need the cell to be highlighted.


Any assistance on this would be greatly appreciated.

TASK START DATE (Column A)TASK END DATE (Column B)NAME (Column C)
ROW 101/01/202201/03/2022PERSON A
ROW 202/02/202213/02/2022PERSON B
ROW 301/02/202205/03/2022PERSON C
ROW 427/02/202201/04/2022PERSON A
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
If you're okay with VBA macros, here's what I came up with for the worksheet's code. If you don't know where to put it, right-click the worksheet tab and choose View Code. You can change the vbYellow to another color if that's not your choice.

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim FoundCell As Range
    Dim LastCell As Range
    Dim FirstAddr As String
    Dim PersonTasks() As Date
    Dim pCount As Integer
    Dim i As Integer
    Dim j As Integer
    Dim TaskConflict As Boolean
    
    'Unhighlight Col C in case selected cell(s) do not create conflict
    With Range("C:C").Cells.Interior
        .Pattern = xlNone
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
    
    'Check to see if only 1 cell is selected
    If Target.Cells.Count = 1 Then
        'Is the selected cell in Col C and not blank?
        If Target.Column = 3 And Target.Value <> "" Then
        
            '*************
            '* Find all cells in Col C with person's name
            '*   (based on Chip Pearson's Find code)
            '*************
            Set LastCell = Target
            Set FoundCell = Range("C:C").Find(what:=Target.Value, after:=LastCell)
            pCount = -1
            
            If Not FoundCell Is Nothing Then
                FirstAddr = FoundCell.Address
            End If
            Do Until FoundCell Is Nothing
                Set FoundCell = Range("C:C").FindNext(after:=FoundCell)
                '*************
                '* Fill start and end dates into PersonTasks array for each found cell
                '* to be compared later
                '*************
                pCount = pCount + 1
                ReDim Preserve PersonTasks(1, pCount)
                PersonTasks(0, pCount) = FoundCell.Offset(0, -2).Value
                PersonTasks(1, pCount) = FoundCell.Offset(0, -1).Value
                If FoundCell.Address = FirstAddr Then
                    Exit Do
                End If
            Loop
            
            'Compare the start/end times for each task row to make sure start and end dates
            ' don't come between any other task dates
            For i = 0 To pCount - 1
                For j = 1 To pCount
                    If PersonTasks(0, i) > PersonTasks(0, j) And PersonTasks(0, i) < PersonTasks(1, j) _
                      Or PersonTasks(1, i) > PersonTasks(0, j) And PersonTasks(1, i) < PersonTasks(1, j) Then
                        TaskConflict = True
                        Exit For
                    End If
                Next j
            Next i
            
            'If there is a conflict, then re-find all person's cells and highlight
            If TaskConflict Then
                Set FoundCell = Range("C:C").Find(what:=Target.Value, after:=LastCell)
                Do Until FoundCell Is Nothing
                    Set FoundCell = Range("C:C").FindNext(after:=FoundCell)
                    FoundCell.Interior.Color = vbYellow
                    If FoundCell.Address = FirstAddr Then
                        Exit Do
                    End If
                Loop
            End If
        End If
    End If
End Sub
 
Upvote 0
Another option with conditional formatting.
+Fluff 1.xlsm
ABC
1TASK START DATETASK END DATENAME
201/01/202201/03/2022PERSON A
302/02/202213/02/2022PERSON B
401/02/202205/03/2022PERSON C
527/02/202201/04/2022PERSON A
601/05/202201/07/2022PERSON A
701/01/202203/02/2022PERSON C
8
9
Data
Cells with Conditional Formatting
CellConditionCell FormatStop If True
C2:C13Expression=OR(COUNTIFS($C$2:$C2,C2,$B$2:$B2,">="&A2,$A$2:$A2,"<"&A2),COUNTIFS($C$2:$C2,$C2,$A$2:$A2,"<="&$B2,$B$2:$B2,">"&$B2))textNO
 
Upvote 0
Solution

Forum statistics

Threads
1,215,056
Messages
6,122,907
Members
449,096
Latest member
dbomb1414

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top