【问题标题】:Excel formula to Cross reference 2 sheets, remove duplicates from one sheetExcel 公式交叉引用 2 张表,从一张表中删除重复项
【发布时间】:2012-12-03 16:55:29
【问题描述】:

这与

有关

Excel / VBA Remove duplicate rows by cross referencing 2 different sheets then deleting 1 row

我似乎无法让任何 VBA 运行良好或足够快地运行 100 行。

Excel 是否有一个公式可以通过交叉引用另一张工作表从一张工作表中删除重复项?

感谢您的所有帮助。

【问题讨论】:

    标签: excel excel-formula


    【解决方案1】:

    这是一个更快的 VBA 解决方案,它使用了一个字典对象。如您所见,它仅在工作表 A 和工作表 B 中循环一次,而您的原始解决方案的运行时间与“工作表 A 中的行数”*“工作表 B 中的行数”成正比。

    Option Explicit
    Sub CleanDupes()
        Dim wsA As Worksheet
        Dim wsB As Worksheet
        Dim keyColA As String
        Dim keyColB As String
        Dim rngA As Range
        Dim rngB As Range
        Dim intRowCounterA As Integer
        Dim intRowCounterB As Integer
    
        keyColA = "A"
        keyColB = "B"
    
        intRowCounterA = 1
        intRowCounterB = 1
    
        Set wsA = Worksheets("Sheet A")
        Set wsB = Worksheets("Sheet B")
    
        Dim dict As Object
        Set dict = CreateObject("Scripting.Dictionary")
    
        Do While Not IsEmpty(wsA.Range(keyColA & intRowCounterA).Value)
            Set rngA = wsA.Range(keyColA & intRowCounterA)
            If Not dict.Exists(rngA.Value) Then
                dict.Add rngA.Value, 1
            End If
            intRowCounterA = intRowCounterA + 1
        Loop
    
        intRowCounterB = 1
        Do While Not IsEmpty(wsB.Range(keyColB & intRowCounterB).Value)
            Set rngB = wsB.Range(keyColB & intRowCounterB)
            If dict.Exists(rngB.Value) Then
                 wsB.Rows(intRowCounterB).Delete
                 intRowCounterB = intRowCounterB - 1
            End If
            intRowCounterB = intRowCounterB + 1
        Loop
    End Sub
    

    【讨论】:

    • 谢谢医生!哇,速度更快。
    【解决方案2】:

    您可以使用 ADO 和 Excel 做很多事情。

    Dim cn As Object
    Dim rs As Object
    Dim wb As Workbook
    Dim sSQL As String
    Dim sFile As String
    Dim sCon As String
    Dim sXLFileToProcess As String
    Dim i
    
    sXLFileToProcess = "Book1z.xls"
    
    sFile = Workbooks(sXLFileToProcess).FullName
    
    ''Note that if HDR=No, F1,F2 etc are used for column names,
    ''if HDR=Yes, the names in the first row of the range
    ''can be used.
    ''This is the Jet 4 connection string, you can get more
    ''here : http://www.connectionstrings.com/excel
    
    sCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & sFile _
        & ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"";"
    
    ''Late binding, so no reference is needed
    
    Set cn = CreateObject("ADODB.Connection")
    Set rs = CreateObject("ADODB.Recordset")
    
    cn.Open sCon
    
    '' In this example, the column header for column F is F, see notes
    '' above on field (column) names. It also assumes that the sheets to
    '' be merged have the same column headers in the same order
    '' It would be safer to list the column heards rather than use *.
    
    sSQL = sSQL & "SELECT b.Key,b.b,b.c,b.d,b.e FROM [SheetB$] As B " _
                & "LEFT JOIN [SheetA$] As A " _
                & "ON B.Key=A.Key " _
                & "WHERE A.Key Is Null"
    
    rs.Open sSQL, cn, 3, 3
    
    Set wb = Workbooks.Add
    
    With wb.Worksheets("Sheet1")
        For i = 1 To rs.Fields.Count
            .Cells(1, i) = rs.Fields(i - 1).Name
        Next
    
        .Cells(2, 1).CopyFromRecordset rs
    End With
    
    ''Tidy up
    rs.Close
    Set rs = Nothing
    cn.Close
    Set cn = Nothing
    

    【讨论】:

    • 这听起来很棒 Remou 但我不确定你在做什么。您是在执行 sql 查询来获取数据还是使用 SQL 来访问 2 张工作表中的数据(例如,在 C# 中的列表上执行 LINQ 查询)。请解释一下sql部分的用途,我很兴奋!
    • 您可以将工作表或命名范围视为 ADO 中用于 SQL 目的的表。换句话说,您可以使用 Jet 执行的任何查询都可以针对 Excel 工作表运行。这很有趣。如果我可以提供帮助,请进一步询问。
    • 示例中的 SQL 将两个工作表连接到一个关键字段上,只选择出现在 SheetB 而不是在 SheetA 上的记录(WHERE A.Key 为 Null)并将这些记录写入新工作簿CopyFromRecordset。这不写标题,所以有一个小循环可以做到这一点。
    • 太棒了。那么如何使用 Jet,是我必须下载的东西,还是您提供的内容使用连接字符串就足够了。此外,这是否是便携式的,或者是否有人必须能够访问互联网并可能下载 Jet 插件。谢谢。
    • Jet 与 Windows 一起安装,并且至少从 XP 开始,所以没有下载,没有问题,运气好 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-27
    • 1970-01-01
    • 2021-11-29
    相关资源
    最近更新 更多