Highlight Duplicates within separate groups of data

Dave T

Board Regular
Joined
Jun 21, 2005
Messages
93
Hello All,

I have just looked through most of the postings I could find on the board regarding VBA to identify duplicates. Virtually all of these use the entire column when checking for duplicates.

I am after code which will identify duplicates in column A within a contiguous range of data.
For example there is a heading in column B and below this photo details will be entered and in the next column the photo number is entered.
Finally a number (for sorting) is entered in column A. The photos will be numbered 1 to whatever. So I need code which will highlight duplicates in that contiguous range of data.
Then there is one or two blank rows before another batch of photo details are entered. This means the same sorting numbers are used and are separated by a blank row or rows.

Can someone help me with some code which will make the fill red and when the duplicated has been corrected the fill colour is removed.

Thanking you in advance.

Regards,
Dave T

DUPLICATES - Many Macros.xls
ABCD
29PN3215
301SOUTHERNAPPROACH9107
313NORTHERNAPPROACH9105
322GENERALUNDERSIDEOFWESTERNSTONEARCH9111
331WESTERNELEVATION9104
343GENERALUNDERSIDE9110
35
36PN7583
371SOUTHERNAPPROACH9111
382NORTHERNAPPROACH9112
391GENERALUNDERSIDE9113
Sheet 9
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Unfortunately your example sheet is not too clear, as for instance the NORTHERN APPROACH is not highlighted as duplicate. What makes it a dupicate? The name or the photo number? And what do the numbers in coulmn A mean? I thought they were going to be sequential, but they are all over the place?

In column D you could enter this formula:
D29: =if(iserror(vlookup(b29,b30:b20000,1)),"","Duplicate")
Copy it down (and up if necessary)
That will show 'Duplicate' in column D next to all entries that have a duplicate entry below it.
You could then set conditional formatting in column A to be red if D contains Duplicate.
 
Upvote 0
Hi,

Try

Code:
Sub kTest()
Dim r As Range, Rng As Range, i As Long
Set Rng = Columns(1).SpecialCells(2, 1)
For Each r In Rng.Areas
    With r
        .Cells(1).Select
        i = .Cells(1).Row
        .FormatConditions.Delete
        .FormatConditions.Add Type:=xlExpression, Formula1:="=COUNTIF(r" & i & "c1:rc1,rc1)>1"
        .FormatConditions(1).Interior.ColorIndex = 3
    End With
Next
End Sub

HTH
 
Last edited:
Upvote 0
My version:

<font face=Courier New><br><SPAN style="color:#00007F">Sub</SPAN> Duplicates_In_Groups()<br>    <SPAN style="color:#00007F">Dim</SPAN> aArea <SPAN style="color:#00007F">As</SPAN> Range<br>    <SPAN style="color:#00007F">Dim</SPAN> mRange <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>    <br>    Application.ScreenUpdating = <SPAN style="color:#00007F">False</SPAN><br>    <SPAN style="color:#00007F">With</SPAN> Columns("A")<br>        .FormatConditions.Delete<br>        For <SPAN style="color:#00007F">Each</SPAN> aArea <SPAN style="color:#00007F">In</SPAN> .SpecialCells(xlCellTypeConstants).Areas<br>            <SPAN style="color:#00007F">With</SPAN> aArea<br>                .Select<br>                mRange = .Offset(-1).Resize(1).Address(1, 1) & ":" & _<br>                    .Offset(-1).Resize(1).Address(0, 0)<br>                .FormatConditions.Add Type:=xlExpression, Formula1:= _<br>                    "=MATCH(A30," & mRange & ",0)"<br>                .FormatConditions(1).Interior.ColorIndex = 3<br>            <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>        <SPAN style="color:#00007F">Next</SPAN> aArea<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>    Application.ScreenUpdating = <SPAN style="color:#00007F">True</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>
 
Last edited:
Upvote 0
Hello sijpie, Krishnakumar and Peter_SSs,

Many thanks to each of you for your replies.
The macro versions was the preferred type of solution I was after.

I agree with sijpie that the image is not very clear. The data actually starts at cell B3 which is the only data entered on that row (it should always be PNxxxx). Bellow cell B3 is the photo details (B4) and C4 down will be the photo number...and so on for an undetermined number of rows and data range sizes.

I tried all of the solutions offered and the one from Krishnakumar works well.

I needed to change the formula provided by sijpie to show the duplicates the way I wanted them to be highlighted.
=IF(ISERROR(VLOOKUP(A3,A4:A20000,1)),"Duplicate","")
If there were more than two duplicates or the range the formula only showed duplicate for the last duplicate entry. If the data was not sorted no duplicates were identified.
I did not use this option as I did not want to add an extra column of data.

I had a problem with the solution from Peter_SSs...
When I tried to run your version I get the following error message:
Run-time error '1004':
Application-defined or object-defined error
When I go to debug the following line of code is highlighted:

Code:
mRange = .Offset(-1).Resize(1).Address(1, 1) & ":" & _
  .Offset(-1).Resize(1).Address(0, 0)

Are able to help me with resolving this error as I am curious to see Peter_SSs solution working.

Regards,
Dave T
 
Upvote 0
I had a problem with the solution from Peter_SSs...
When I tried to run your version I get the following error message:
Run-time error '1004':
Application-defined or object-defined error
When I go to debug the following line of code is highlighted:

Code:
mRange = .Offset(-1).Resize(1).Address(1, 1) & ":" & _
  .Offset(-1).Resize(1).Address(0, 0)

Are able to help me with resolving this error as I am curious to see Peter_SSs solution working.

Regards,
Dave T
Dave

That error will most likely have been triggered by you having data in cell A1 (which we couldn't tell from your original screen shot). Given that your data apparently starts in row 3, try replacing
Code:
With Columns("A")
with
Code:
With Range("A3", Range("A" & Rows.Count).End(xlUp))

If that still doesn't work, could you post a screen shot of, say, A1:C10 of your sheet?
 
Upvote 0
Hello Peter,

I suspect the usual problem is trying to describe what I want to do in writing and the reader trying to interpret my comments. Screen shots probably should be used more often as it is the usual story of "a picture tells a thousand words".

The way the worksheet is used is like this...
  • I enter the plan number of a structure in column B (PNxxx)
  • Below the plan number the photo details taken of that particular structure are listed.
  • In column C I enter the associated photo number against the photo details in column B.
  • After a blank row/s the next plan number and photo details are entered and so on.
  • This is done for each structure inspected during that batch of inspections.
  • At a later date an engineer will then work out the order of the photos by numbering them in column A.
  • I can then convert the data from columns A & B into a CSV file and import the CSV data into another program against each particular structures number.
So back to what I was trying to achieve...

There have been times when the engineer gets lost when numbering the order of the photos in a small contiguous range and doubles up.
So what I was trying to do was have the duplicate numbers highlighted in red whenever they make a mistake within each individual contiguous range.
In the exaggerated screen shot the first entry is the plan number, PN1038, in B3 and the photo data in B4:C7. A duplicate has been entered in A7 and it would be highlighted in red until it is corrected.
The next range of data for PN2430 is yet to be completed, but already A14 has been flagged as a duplicate.

Mr Excel Posting_2.xls
ABCD
1ORDERDESCRIPTION(BRIDGENUMBERMUSTSTARTWITHPN)PHOTOID
2
3PN1038
41GENERALUNDERSIDE8800
52NORTHERNELEVATION8798
63BOLTSMISSING-NORTHERNSIDE8801
71SOUTHERNELEVATION8799
8
9
10PN2430
111GENERALUNDERSIDE8803
124DOWNSTREMELEVATION8806
132D/SELEVATION8805
144SOUTHERNAPPROACH8809
15NORTHERNAPPROACH8810
16
17
18PN5342
19VICTORIANBORDERAPPROACH8810
20KEITHAPPROACH8811
21GENERALUNDERSIDE8812
Sheet1


I have used the following conditional formatting:
Condition 1:
Formula is =$C3<>""
When the photo number is entered into a cell in column C there are borders applied from A to C for that row.
Condition 2:
Formula is =LEFT($B3,2)="PN"
When the PN is entered before structure number that cell data is shown as bold.
I have noted that the macros suggested kill the existing conditional formatting i.e. borders, etc.

Another solution to my problem could be just using another conditional formatting condition that would highlight duplicates when I run the sorting macro that Peter helped me out with on another post.
Formula is =AND($A3<>"",$A3=$A2)
When there is a duplicate in column A either the individual cell or cells A:C would be highlighted red.

Obviously this extra condition would only show duplicates when the data is sorted not as they are entered (I also needed to make this condition 1 to work).
Code:
Option Explicit
Sub Sort_A_to_C()
  With ActiveCell.CurrentRegion
    If .Column<= 4 And .Rows.Count > 1 Then
      .Resize(, 3).Sort Key1:=.Cells(2, 1), Order1:=xlAscending, _
        Header:=xlYes, OrderCustom:=1, MatchCase:=False, _
        Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
    End If
  End With
End Sub

For some work related reason the photo description text in column B must be uppercase (Smitty helped me out with this in a previous post).
Code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
  Dim myRng As Range
    Set myRng = Target.Parent.Range("B:C")
   
    If Intersect(myRng, Target.Cells) Is Nothing Then Exit Sub
    
    Select Case Target.Column
      Case 2   ' Column B
        On Error Resume Next
        Target.Value = UCase(Target.Value)
        On Error Resume Next
        Err.Clear
      Case 3   ' Column C
        Me.Parent.Save
  End Select
End Sub

I added another case to Smitty's code but it combined data ranges:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  Dim myRng As Range
    Set myRng = Target.Parent.Range("A:C")
   
    If Intersect(myRng, Target.Cells) Is Nothing Then Exit Sub
    
    Select Case Target.Column
      Case 1   ' Column A
        On Error Resume Next
        Call Sort_A_to_C
        Err.Clear
      Case 2   ' Column B
        On Error Resume Next
        Target.Value = UCase(Target.Value)
        On Error Resume Next
        Err.Clear
      Case 3   ' Column C
        Me.Parent.Save
  End Select
End Sub

By calling the sort macro as numbers were entered in column A it meant the conditional formatting worked as data was entered.

However, I am starting to suspect that I should just keep it simple and use the extra conditional formatting condition when manually running the sort macro as it requires no code and does not remove existing borders.

Thanks again for all your help it is very much appreciated.

Regards,
Dave T
 
Upvote 0
That HTML maker is not working very well for you and is making the posts hard to read. Perhaps you could try switching to Excel jeanie or RichardSchollar’s beta HTML Maker

The situation is clearly more complex than I was aware of (eg existing Conditional Formatting), so I've taken a re-think.

It seems that you were only sorting each time an entry was made to try to get the duplicates Conditional Formatting working. As a result, I have removed the sorting from the Worksheet_Change code and assume you will run a sort routine manually at some stage.

My approach is now to stop duplicates being entered in column A in the first place, by having the code apply standard Data Validation to column A. That way your existing Conditional Formatting can remain as is, without constant modification by the code.

Anyway, give this code a try and see how it goes.

<font face=Courier New><br><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> Worksheet_Change(<SPAN style="color:#00007F">ByVal</SPAN> Target <SPAN style="color:#00007F">As</SPAN> Range)<br>    <SPAN style="color:#00007F">Dim</SPAN> Changed <SPAN style="color:#00007F">As</SPAN> Range, ChangedB <SPAN style="color:#00007F">As</SPAN> Range, c <SPAN style="color:#00007F">As</SPAN> Range, DVRng <SPAN style="color:#00007F">As</SPAN> Range<br>    <SPAN style="color:#00007F">Dim</SPAN> r <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>  <br>    <SPAN style="color:#00007F">Set</SPAN> Changed = Intersect(Target, Range("A:C"))<br>   <br>    <SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#00007F">Not</SPAN> Changed <SPAN style="color:#00007F">Is</SPAN> <SPAN style="color:#00007F">Nothing</SPAN> <SPAN style="color:#00007F">Then</SPAN><br>        Application.EnableEvents = <SPAN style="color:#00007F">False</SPAN><br>        r = Changed.Row<br>        <SPAN style="color:#00007F">Set</SPAN> DVRng = Intersect(Cells(r, 1).Resize(, 3).CurrentRegion, Columns("A"))<br>        <SPAN style="color:#00007F">With</SPAN> DVRng.Validation<br>            .Delete<br>            .Add Type:=xlValidateCustom, AlertStyle:=xlValidAlertStop, _<br>                Operator:=xlBetween, Formula1:="=COUNTIF(" & DVRng.Address(1, 1) _<br>                & "," & DVRng.Cells(1, 1).Address(0, 0) & ")=1"<br>            .IgnoreBlank = <SPAN style="color:#00007F">True</SPAN><br>            .ErrorMessage = "Duplicates not allowed"<br>            .ShowError = <SPAN style="color:#00007F">True</SPAN><br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>        <SPAN style="color:#00007F">Set</SPAN> ChangedB = Intersect(Changed, Columns("B"))<br>        <SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#00007F">Not</SPAN> ChangedB <SPAN style="color:#00007F">Is</SPAN> <SPAN style="color:#00007F">Nothing</SPAN> <SPAN style="color:#00007F">Then</SPAN><br>            <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> c <SPAN style="color:#00007F">In</SPAN> ChangedB<br>                c.Value = UCase(c.Value)<br>            <SPAN style="color:#00007F">Next</SPAN> c<br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>        Application.EnableEvents = <SPAN style="color:#00007F">True</SPAN><br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>
 
Upvote 0
Hello Peter,

Another very impressive solution that works very well.
I really do appreciate your help and explanations.
I have been using Htmlmaker 2.42 and was also wondering why the screen shots were so huge. Thanks for the suggestions about other options.

As I am just starting to get my head around VBA this is a fantastic forum.

Regards,
Dave T
 
Upvote 0
Found the solution to the problem with the HTML maker images.

When ever I opened Excel I saw the following error message:
Error Referencing[VBIDE = VBE6EXT.OLB]
Error Number: = 91
Error Discrp: = Object variable or With block variable not set

You may have to manually set a reference to the [VBIDE = VBE6EXT.OLB]
The answer was posted in a MrExcel post:
http://www.mrexcel.com/forum/showthread.php?t=89356

Regards,
Dave T
 
Upvote 0

Forum statistics

Threads
1,223,914
Messages
6,175,351
Members
452,638
Latest member
Oluwabukunmi

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