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
 
Hello All,

I have another question regarding a modification/addition to Richard's code from reply number 8, and I have been trying to get this to work without much success...

I have inserted a new column between the Description (column B) and the Photo ID (was column C) that will contain the rest of the photo details.
I would like to just enter the photo number into column D and after pressing enter have this number copied to the cell to the left (column C) and have “IMG_” added before the number and “.JPG” added after the number.

For example if there was a photo number of 1234 added in column D, the cell immediately to the left would contain a description of IMG_1234.JPG.
I did try using a formula, but this affected the sorting macro, hence the need to modify the macro.

Thanks in advance,

Regards,
Dave T
 
Upvote 0

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
I have another question regarding a modification/addition to Richard's code from reply number 8, ...
Is that Richard also known as Peter? :biggrin:


I have inserted a new column between the Description (column B) and the Photo ID (was column C) that will contain the rest of the photo details.
I would like to just enter the photo number into column D and after pressing enter have this number copied to the cell to the left (column C) and have “IMG_” added before the number and “.JPG” added after the number.

For example if there was a photo number of 1234 added in column D, the cell immediately to the left would contain a description of IMG_1234.JPG.
I did try using a formula, but this affected the sorting macro, hence the need to modify the macro.
It's a long time since I provided that code and I don't have that test sheet any more. However, I think this may do what you want. Make these changes to the code from post #8

1. Add a new Dim statement at the top:
Rich (BB code):
Dim ChangedD As Range

2. Change this line
Rich (BB code):
Set Changed = Intersect(Target, Range("A:D"))

3. This change may not be necessary but should do no harm
Rich (BB code):
Set DVRng = Intersect(Cells(r, 1).Resize(, 4).CurrentRegion, Columns("A"))

4. Add this new section just before 'Application.EnableEvents = True'
Rich (BB code):
Set ChangedD = Intersect(Changed, Columns("D"))
If Not ChangedD Is Nothing Then
    For Each c In ChangedD
        c.Offset(, -1).Value = "IMG_" & c.Value & ".JPG"
    Next c
End If
 
Upvote 0
My appologies Peter,

I had just responded to a private message from Richard Schollar and still had his name on my brain.

Your reply was very much appreciated and by coincidence I had done virtually all of what you had suggested, i.e. I had done steps 1, 2 & 3 exactly as you had suggested and had step 4 most of the way there.
The line
Code:
 [COLOR=#0000ff]c.Offset(, -1).Value = "IMG_" & c.Value & ".JPG" [/COLOR][COLOR=black]
was the part that had me stumped.[/COLOR]

When I enter the code above I get a message that says "Compile error: Syntax error.". I even tried to play around with your code
Code:
 [COLOR=#0000ff]c.Value = c.Offset(, -1).Value = "IMG_" & c.Value & ".JPG" [/COLOR][COLOR=black]
to no avail.[/COLOR]
What am I doing wrong???

Sorry again about confusion with your name.

Regards,
Dave T
 
Upvote 0
I tested the code successfully (no syntax error) so I am not sure what the problem is with your code. Here's my full code:

<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> ChangedD <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:D"))<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(, 4).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>        <SPAN style="color:#00007F">Set</SPAN> ChangedD = Intersect(Changed, Columns("D"))<br>        <SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#00007F">Not</SPAN> ChangedD <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> ChangedD<br>                c.Offset(, -1).Value = "IMG_" & c.Value & ".JPG"<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><br><br><br></FONT>
 
Upvote 0
Hello Peter,

Yes you are right your code does work...
Not sure why I had that error message.
At home I was using Excel 2007 (which is where I had the error message) and here at work I am using Excel 2003 and there are no problems.

Once again thanks for all your help, it is very much appreciated.

Regards,
Dave T
 
Upvote 0
Hello All,

I have a question about whether it is possible to add other conditions to the data validation VBA used in column A that prevents duplicates within separate groups of data.

The prevention of duplicates VBA provided by Peter works perfectly, but I am wondering if it is possible to add another two conditions to the existing criteria…

Extra Data Validation criteria:
1) Only allow ‘Whole numbers’
2) Numbers within each separate group of data must be within sequential order

Option number 2 may not be possible to do as the sorting order whole numbers are entered in a random order in column A against the photo descriptions in column B. The only time they will/should be in numerical order is when the sort order macro is run. This sorts the data within the specific group in columns A, B & C by column A.
I am hoping to prevent users from not using a non numerical sequence i.e. they could enter 1, 2, 3, 4 in random order but not be able to enter 1, 2, 3, 5 (they have missed number 4).

If the sequential number option is too hard I may have to settle for using conditional formatting that will colour cells that are not in sequential order (however this would only be evident when the data has been sorted). If others agree this looks like the easiest option can someone suggest a conditional formatting formula that will highlight numbers not in sequential order, but only apply within the various ranges of data.

Thanking you in advance.

Regards,
Dave T
 
Upvote 0
Once again quite a deal of time has passed since doing this so I'm not sure if I remember this exactly, but see how these blue changes go:

Rich (BB code):
With DVRng.Validation
    .Delete
    .Add Type:=xlValidateCustom, AlertStyle:=xlValidAlertStop, _
        Operator:=xlBetween, Formula1:="=AND(COUNTIF(" & DVRng.Address(1, 1) _
        & "," & DVRng.Cells(1, 1).Address(0, 0) & ")=1,MOD(" & DVRng.Cells(1, 1).Address(0, 0) & ",1)=0," _
        & DVRng.Cells(1, 1).Address(0, 0) & ">=1," _
        & DVRng.Cells(1, 1).Address(0, 0) & "<=ROWS(" & DVRng.Address(1, 1) & ")-1)"
    .IgnoreBlank = True
    .ErrorMessage = "You probably want a different message here now"
    .ShowError = True
End With
 
Last edited:
Upvote 0
Hello Peter,

You blow me away with you knowledge...
You solution works perfectly. Thank you very much for all of your help.

I used the macro recorder to try to work this out and the output did not look anything like what you produced.

I just have two more things I have spent hours trying to find/work out.
1) Stand alone VBA code that I can call from another macro (most likely “Sort_A_to_C”) that will put a border around the region i.e. if the various separate ranges of data have photo descriptions in column B and the associated photo ID numbers in column C. The macro will put external and internal borders around the A to C range on a range by range basis for however many rows are used but not including the conditionally formatted bold “PN…” header. The data in columns B or C will define the rectangular A to C range.
I have tried using the “With ActiveCell.CurrentRegion” with other code I have found on the web and from the vba recorder without much success.
2) Conditional formatting for column A that will highlight where numbers have been missed.
• If a row is blank nothing is conditional formatted
• If the cell is the blank to the left on the PN heading nothing is conditional formatted
• If the numbers are in sequential order nothing is conditional formatted
• If a number has been missed it will be conditional formatted i.e. 1, 2, 3, 5, 6, 9, 10. Cells 5 & 9 would be highlighted to show they are not in numerical order (the next numbers have been jumped).
I have tried =IF(AND($A43<>"",$A42+1=$A43,LEFT($B43,2)<>"PN"),FALSE,TRUE) and many other combinations without much success.

Once again any help would be appreciated.

Regards,
Dave T
 
Upvote 0
1. Try this (only tested in Excel 2007):

<font face=Courier New><br><SPAN style="color:#00007F">Sub</SPAN> DavesBorders()<br>    <SPAN style="color:#00007F">Dim</SPAN> cRng <SPAN style="color:#00007F">As</SPAN> Range, cArea <SPAN style="color:#00007F">As</SPAN> Range<br>    <br>    Application.ScreenUpdating = <SPAN style="color:#00007F">False</SPAN><br>    <SPAN style="color:#00007F">Set</SPAN> cRng = Range("C2", Range("C" & Rows.Count).End(xlUp))<br>    cRng.Offset(, -2).Resize(, 3).Borders.LineStyle = xlNone<br>    <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> cArea <SPAN style="color:#00007F">In</SPAN> cRng.SpecialCells(xlCellTypeConstants).Areas<br>        <SPAN style="color:#00007F">With</SPAN> cArea.Offset(, -2).Resize(, 3)<br>            .BorderAround LineStyle:=xlContinuous, Weight:=xlThin, ColorIndex:=0<br>            <SPAN style="color:#00007F">With</SPAN> .Borders(xlInsideVertical)<br>                .LineStyle = xlContinuous<br>                .ColorIndex = 0<br>                .Weight = xlThin<br>            <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>            <SPAN style="color:#00007F">With</SPAN> .Borders(xlInsideHorizontal)<br>                .LineStyle = xlContinuous<br>                .ColorIndex = 0<br>                .Weight = xlThin<br>            <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>    <SPAN style="color:#00007F">Next</SPAN> cArea<br>    Application.ScreenUpdating = <SPAN style="color:#00007F">True</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>


2. Try this 'Formula is' in Conditional Formatting for, say, row 43:

=AND($A43<>"",$A43<>$A42+1)
 
Upvote 0
Thank you very much once again for all your help Peter,

All you suggestions work fine.

I was so close to the Conditional Formatting formula, but was getting frustrated and seemed to be going around in circles. Your solution works perfectly.

Your solution to the borders was an interesting approach, even though it operates differently than what I was thinking or expecting.
With your solution I could enter all the data in their various range groups and when the borders macro is called from the sort macro it puts borders around all the various groups of data on the entire worksheet.
I was thinking/trying to get the border macro just to put borders around the specific range that was being sorted.

Just as an aside… I have noticed that there is a problem when you run your border macro which puts a border around all data in columns A to C, not including the PN** heading. If you do this and move onto another range of data, the data validation kicks in and prevents duplicates being entered.
As the border macro uses .End(xlUp) does this confuse the validation macro into thinking all separated ranges of data is now one large range ???

Once again thanks for all your help.

Regards,
Dave T
 
Upvote 0

Forum statistics

Threads
1,223,912
Messages
6,175,348
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