Check a shifting range with numbers for duplicates and highlight?

NessPJ

Active Member
Joined
May 10, 2011
Messages
431
Office Version
  1. 365
Hi,

I am looking for a way to check for duplicates in a moving range and highlight them.

For example i have Columns K, L, M, N roughly 2000 lines filled with numbers and i want every Cell to check the surrounding 36 Cells for a duplicate and when there is, highlight it.

Is this possible?
 

Attachments

  • mrexcel_chkdup.jpg
    mrexcel_chkdup.jpg
    117 KB · Views: 13
Does this do what you want then?

24 12 10.xlsm
KLMN
1Headers
2
3
4
5
632179339
762733867
856432873
957635862
1096335548
115268492
1266672051
1395467683
1449253368
1564866745
1633462464
1788318676
1885973753
1929208695
203992247
212285707
2295975119
2330156226
2488416167
2548633137
2690869926
2796622127
288955535
2932701991
3025658885
3145671428
3292389596
3398584533
349872931
3598904996
3652843376
3728398164
382279874
3949697410
40824754
4189936275
425830222
43682637
4424213762
4528234776
4660779734
475342435
482371024
4932159340
5096738767
5124768524
5243817310
5336797861
549039294
5520595577
563824650
575832736
5835654428
5996162110
6052853421
61833151
6290574475
6354508280
6488425336
6562783868
665819686
6795126868
6869385741
698996134
708272891
7164164458
7293708630
7365582521
7468946118
754859613
7682379361
7725493244
7882671991
794598276
8056832883
8162524294
8293261742
836476567
8460472947
8534346137
868657578
8768873786
8896163710
8925599275
908024932
9190238725
9226777095
9354905465
94262663
9529854575
9663516478
9756339313
982324517
9955264959
10032264849
10165796544
1026395477
10343179812
104361165
10586863224
10660185995
107561050
10844268018
1099356945
11086853779
11155609865
11250969479
1136516777
114844342
115498658
11636161091
11744377756
11862729689
1192338601
12052319451
12190247044
1223628846
12362279981
12455182265
12592221584
12690915769
12769647575
12834773052
12986523752
1302923650
131846158
13224967782
13342897286
134860626
13594871279
1365122637
13783402290
13859742952
13928507512
14058303463
14162909516
14298873962
14326739419
14462629512
1458672874
14686479791
14734455754
14888234890
14968898513
15069831723
15125199085
15228759024
15336699662
15492402615
15559826773
15649338384
1573833199
1584982198
15954432787
16062549243
161654727
1626983076
16380585939
16488943314
16585821236
166851852
16755762482
16888167963
16962288677
17034108780
1719795581
1722467596
17395797384
17482439677
17594215253
17652439357
17785993382
17826544696
17965385513
1809589097
1815937312
1823461756
18366468734
18445835044
18555724528
18669983860
1874026576
1885826062
18990871767
1909095423
19149781993
1923123585
1932466099
19466397338
1952313768
1968549854
19724209969
1985490241
19922259339
2002217715
20190998936
20225828381
2039428565
20456652146
20594339585
20652237779
20748293896
2084959747
20956328465
2105391808
21182153894
21220494824
21394324573
2144567683
21562488429
216872827
2172392829
218802837
2199646347
22046953453
22158764120
22253212482
2234281418
2243989447
22534636723
22689453370
22723691478
22845176468
2294378192
23022294324
23123413129
2328758447
23328765922
23464854777
2359618359
23628264583
23746936848
NessPJ
Cells with Conditional Formatting
CellConditionCell FormatStop If True
K6:N237Expression=COUNTIF($K2:$N10,K6)>1textNO

This seems to work exactly. Can't believe the formula was that simple. Thanks a lot!
 
Upvote 0

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
PS.
Now i created the following VBA code to accompany the conditional formatting as discussed here. But i keep getting an error when trying to write the Formula to the designated Cell.

Line:
TableCell.Formula = "=Text(RandBetweenInt(" & Lowest & "; " & Highest & "; " & Exclude & "); " & TextFormat & ")"
Keeps giving me an error (Object variable not set).
I expect the VBA to read the value from Cell Sheets("PARAMETERS").Range("M48").Value which actually contains a Range for the formula (value; PARAMETERS!$B$7:$B$10).
What am i doing wrong here? :)

VBA Code:
Sub FindAndSolveDupes()

Dim CurSheet As String
Dim CurWb As Workbook
Dim DupeColor As String
Dim TableRange As Range, TableCell As Range
Dim Lowest As Long, Highest As Long, Exclude As Range, TextFormat As String

Application.ScreenUpdating = False
Application.DisplayAlerts = False

START:

DupeColor = Sheets("PARAMETERS").Range("M45").DisplayFormat.Interior.ColorIndex
'Default Orange color from Excel Colorindex = 44

Lowest = Sheets("PARAMETERS").Range("M46").Value
Highest = Sheets("PARAMETERS").Range("M47").Value
Exclude = Sheets("PARAMETERS").Range("M48").Value
TextFormat = Sheets("PARAMETERS").Range("M49").Value

Set TableRange = Selection
    For Each TableCell In TableRange
        If TableCell.DisplayFormat.Interior.ColorIndex = DupeColor Then
        TableCell.NumberFormat = "General"
        TableCell.Formula = "=Text(RandBetweenInt(" & Lowest & "; " & Highest & "; " & Exclude & "); " & TextFormat & ")"
        Application.Calculate
        TableCell.Formula = TableCell.Value
        End If

    Next TableCell
   
EINDE:

Application.ScreenUpdating = True
Application.DisplayAlerts = True


End Sub

Function RandBetweenInt(Lowest As Long, Highest As Long, Exclude As Range) As Long

Dim R As Long
Dim C As Range
Do
R = Lowest + Int(Rnd() * (Highest + 1 - Lowest))
For Each C In Exclude
If R = C Then Exit For
Next C
Loop Until C Is Nothing

RandBetweenInt = R
Application.Volatile

End Function
 
Upvote 0
When entering a formula with vba, use commas not semicolons, even if your Excel version normally uses semicolons for a worksheet formula separator.

If that does not resolve your problem please tell us what, exactly, is in each of these cells?

1733883288347.png
 
Upvote 0
When entering a formula with vba, use commas not semicolons, even if your Excel version normally uses semicolons for a worksheet formula separator.

If that does not resolve your problem please tell us what, exactly, is in each of these cells?

View attachment 120261

Okay, so i tried updating the code with a Set as Range, but sadly that still did not work.
The values that are in those variables (and the Cells) are:

Lowest = 1
Highest = 99

ExcludeSheet = PARAMETERS
ExcludeAddress = $B$7:$B$10
Set Exclude = Sheets(ExcludeSheet).Range(ExcludeAddress)

TextFormat = 00

I still seem to be getting a "Type Mismatch" on "Exclude".

VBA Code:
Sub FindAndSolveDupes()

Dim CurSheet As String
Dim CurWb As Workbook
Dim DupeColor As String
Dim TableRange As Range, TableCell As Range
Dim Lowest As Long, Highest As Long, ExcludeSheet As String, ExcludeAddress As String, Exclude As Range, TextFormat As String

Application.ScreenUpdating = False
Application.DisplayAlerts = False

START:

DupeColor = Sheets("PARAMETERS").Range("M45").DisplayFormat.Interior.ColorIndex
'Default Orange color from Excel Colorindex = 44

Lowest = Sheets("PARAMETERS").Range("M46").Value
Highest = Sheets("PARAMETERS").Range("M47").Value

ExcludeSheet = Sheets("PARAMETERS").Range("M48").Value
ExcludeAddress = Sheets("PARAMETERS").Range("M49").Value
Set Exclude = Sheets(ExcludeSheet).Range(ExcludeAddress)

TextFormat = Sheets("PARAMETERS").Range("M50").Value

Set TableRange = Selection
    For Each TableCell In TableRange
        If TableCell.DisplayFormat.Interior.ColorIndex = DupeColor Then
        TableCell.NumberFormat = "General"
        TableCell.Formula = "=Text(RandBetweenInt(" & Lowest & ", " & Highest & ", " & Exclude & "), " & TextFormat & ")"
        Application.Calculate
        TableCell.Formula = TableCell.Value
        End If

    Next TableCell
    
EINDE:

Application.ScreenUpdating = True
Application.DisplayAlerts = True


End Sub

Function RandBetweenInt(Lowest As Long, Highest As Long, Exclude As Range) As Long

Dim R As Long
Dim C As Range
Do
R = Lowest + Int(Rnd() * (Highest + 1 - Lowest))
For Each C In Exclude
If R = C Then Exit For
Next C
Loop Until C Is Nothing

RandBetweenInt = R
Application.Volatile

End Function
 
Upvote 0
This seems to have done the trick
VBA Code:
Dim Lowest As Long, Highest As Long, ExcludeSheet As String, ExcludeAddress As String, Exclude As Range, TextFormat As String

ExcludeSheet = Sheets("PARAMETERS").Range("M48").Value
ExcludeAddress = Sheets("PARAMETERS").Range("M49").Value
Set Exclude = Sheets(ExcludeSheet).Range(ExcludeAddress)
ExcludeString = "'" & ExcludeSheet & "'!" & Exclude.Address


TableCell.Formula = "=Text(RandBetweenInt(" & Lowest & ", " & Highest & ", " & ExcludeString & "), " & TextFormat & ")"
 
Upvote 0
Glad you seem to have resolved it. Thanks for letting us know. (y)
 
Upvote 0

Forum statistics

Threads
1,224,819
Messages
6,181,153
Members
453,021
Latest member
Justyna P

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