I need a function to detect duplicates formatted as text.
This cannot distinguish between \"46.500\" and\"46.5000\". CountIf probably compares cells as numbers. Thes
The CountIf function doesn't takes a formula as its second argument, so the second argument should be:
"=" & Range(column & x).Text
There are several ways to check for duplicates using VBA - but in case a worksheet formula would help someone (and maybe seemed difficult?), here's an array formula that will notify you whether or not the cells in a given range are unique.
=IF(MAX(COUNTIF(B$3:B$100,B3:B100))>1,"List has duplicates","List is unique")
- Since this is a worksheet array formula, instead of hitting Enter to finish entering the formula, you'll need to use Ctrl + Shift + Enter. (More info in the links below.)
This (obviously) looks at range B3:B100
. You can change it to whatever, but note that the $
exists in one part but not the other. Also, if you use it on large ranges (2000+ cells), it might take a few seconds to update every time you change a cell.
Alternatively, you could place and remove the formula programmatically with VBA, using the FormulaArray property of the Range object.
More info from Microsoft about array formulas here and here and limitations here.
I usually find ado useful in such circumstances.
Dim cn As Object
Dim rs As Object
strFile = Workbooks(1).FullName
strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strFile _
& ";Extended Properties=""Excel 8.0;HDR=No;IMEX=1"";"
Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
cn.Open strCon
strSQL = "SELECT F2, Count(F2) AS CountF2 FROM [Sheet1$] " _
& "GROUP BY F2 HAVING Count(F2)>1 "
rs.Open strSQL, cn
s = rs.GetString
MsgBox s
'' Or
Sheets("Sheet2").Cells(2, 1).CopyFromRecordset rs
Assuming all the "text" cells are textual representations of numbers, then the following change will work:
Function check_duplicates(column As String)
Dim lastrow As Long
Dim x As Long
lastrow = Range(column & "65536").End(xlUp).Row
For x = lastrow To 1 Step -1
If Application.WorksheetFunction.CountIf(Range(column & "1:" & column & lastrow), Val(Range(column & x).Text)) > 1 Then
check_duplicates = x ' return row with a duplicate
x = 1
Else
check_duplicates = 0
End If
Next x
End Function
It coerces the value of the criteria cell to a value by the use of the Val
function
Here is new version based on Remou's code. This one is little more versatile and works with MS Excel 2007.
Function check_duplicates(column As Integer)
' checks for duplicates in a column
' usage: column - numerical (A = 1, B=2 etc...)
' returns: "" - no duplicates, otherwise list of duplicates with numbers of occurrences
Dim cn As Object
Dim rs As Object
strFile = ActiveWorkbook.FullName
strSheet = ActiveWorkbook.ActiveSheet.Name
' connection string for Excel 2007
strcon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strFile & _
";Extended Properties=""Excel 12.0 Xml;HDR=No;IMEX=1"";"
Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
cn.Open strcon
col = "F" & Trim(Str(column))
strsql = "SELECT " & col & ", Count(" & col & ") AS Count" & col & " FROM [" & strSheet & "$]" & _
"GROUP BY " & col & " HAVING Count(" & col & ")>1 "
rs.Open strsql, cn
If rs.BOF = True And rs.EOF = True Then
check_duplicates = ""
Else
check_duplicates = rs.GetString
End If
End Function