Detecting duplicates formatted as text

前端 未结 5 539
春和景丽
春和景丽 2020-12-21 23:08

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

相关标签:
5条回答
  • 2020-12-21 23:29

    The CountIf function doesn't takes a formula as its second argument, so the second argument should be:

    "=" & Range(column & x).Text

    0 讨论(0)
  • 2020-12-21 23:32

    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.

    0 讨论(0)
  • 2020-12-21 23:37

    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
    
    0 讨论(0)
  • 2020-12-21 23:39

    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

    0 讨论(0)
  • 2020-12-21 23:45

    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
    
    0 讨论(0)
提交回复
热议问题