Solved

test if values are unique vba

Posted on 2010-08-13
9
365 Views
Last Modified: 2012-05-10
hy ,
how to test if values in 4 cells are unique

       A      B      C      D
1     4      9       7      5

something like :
if A1 <>B1<>C1<>D1 then
0
Comment
Question by:thmh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 100 total points
ID: 33431903
I would loop through and use CountIf:
Dim rng As Range, cel As Range
Dim Counter As Long

Set rng = ActiveSheet.Range("a1:d1")
For Each cel In rng.Cells
    Counter = Counter + Application.CountIf(rng, cel.Value)
Next
If Counter > 4 Then
    'duplicates
Else
    'no duplicates
End If

Open in new window

0
 
LVL 45

Expert Comment

by:patrickab
ID: 33432029
thmh,

The code below is in the attached file. Try changing one of the numbers so that it is the same as another.

Hope it helps

Patrick
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range
Dim celle As Range
Dim coll As New Collection

With Sheets("Sheet1")
    Set rng = Range(.Cells(1, "A"), .Cells(1, "D"))
End With
If Not Intersect(rng, Target) Is Nothing Then
    For Each celle In rng
        On Error Resume Next
        coll.Add CStr(celle), CStr(celle)
        If Err <> 0 Then
            MsgBox "One or more are the same"
            Exit Sub
        End If
    Next celle
End If

End Sub

Open in new window

thmh-04.xls
0
 

Author Comment

by:thmh
ID: 33432054
what if cells are in different locations

 
       A      BD      CP      DJ
1     4                        
2               9
3                         3
5                                    7
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 45

Expert Comment

by:patrickab
ID: 33432070
If you want just a formula, perhaps this will do it:

=IF(OR(COUNTIF(A1:D1,A1)<>1,COUNTIF(A1:D1,B1)<>1,COUNTIF(A1:D1,C1)<>1,COUNTIF(A1:D1,D1)<>1),"Duplicates present","No duplicates")

Patrick(ab)
0
 
LVL 81

Expert Comment

by:byundt
ID: 33432078
You could use an array-entered formula like:
=SUM(IFERROR(1/COUNTIF(A1:D1,A1:D1),0))=COUNTA(A1:D1)                  returns either TRUE or FALSE; requires Excel 2007 or later
=SUM(IF(ISERROR(1/COUNTIF(A1:D1,A1:D1)),0,1/COUNTIF(A1:D1,A1:D1)))=COUNTA(A1:D1)                  works in any version of Excel

To array-enter a formula:
1) Click in the formula bar
2) Hold the Control and Shift keys down
3) Hit Enter
4) Release all three keys

Excel should respond by adding curly braces { } surrounding the formula. If not, repeat steps 1 through 4.

If you prefer a user-defined function, I posted one in the snippet.
Function NoDupes(Values As Variant) As Boolean
Dim v As Variant
Dim coll As New Collection
On Error Resume Next
For Each v In Values
    If v <> "" Then coll.Add CStr(v), CStr(v)
Next
NoDupes = Err = 0
On Error GoTo 0
End Function

Open in new window

0
 
LVL 81

Assisted Solution

by:byundt
byundt earned 40 total points
ID: 33432115
The NoDupes user-defined function will work with non-contiguous cells if you use the Union operator (surrounding the list of cells by parentheses). These cells must all be on the same worksheet.
=NoDupes((A1,B2,C3,D4))
0
 
LVL 45

Assisted Solution

by:patrickab
patrickab earned 40 total points
ID: 33432171
>what if cells are in different locations

       A      BD      CP      DJ
1     4                        
2               9
3                         3
5                                    7

Change the macro to that shown below.

Patrick(ab)
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range
Dim celle As Range
Dim coll As New Collection

With Sheets("Sheet1")
    Set rng = .Range("A1,BD2,CP3,DJ5")
End With
If Not Intersect(rng, Target) Is Nothing Then
    For Each celle In rng
        On Error Resume Next
        coll.Add CStr(celle), CStr(celle)
        If Err <> 0 Then
            MsgBox "One or more are the same"
            Exit Sub
        End If
    Next celle
End If

End Sub

Open in new window

0
 

Author Comment

by:thmh
ID: 33432345
tnx all
matthewspatrick solutions is best for what i need
0
 
LVL 45

Expert Comment

by:patrickab
ID: 33432573
thmh - Tks4pts - Patrick(ab)
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question