Detecting duplicates formatted as text

前端 未结 5 543
春和景丽
春和景丽 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: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
    

提交回复
热议问题