简体   繁体   English

比较来自2个不同工作簿的两列的更快方法

[英]Faster method to compare two columns from 2 different workbooks

So I have some code using for loops currently doing this and it takes roughly 6 minutes to run... 所以我有一些使用for循环的代码当前正在执行此操作,运行大约需要6分钟...

I have many sheets showing the same columns with some different data. 我有很多工作表显示具有不同数据的相同列。 One column comes in either a named form or a numerical form (Depending on how a user input it to a completely separate database). 一列以命名形式或数字形式出现(取决于用户将其输入到完全独立的数据库中的方式)。

Another database contains 2 columns: one being the numerical form of data while the other is named. 另一个数据库包含两列:一列是数据的数字形式,另一列是命名的。

My database currently compares my "name" column if numerical with the numerical column in this other database and when it finds a match it changes my "name" cell to match the corresponding name cell in the other database. 我的数据库当前将我的“名称”列(如果数值)与该另一个数据库中的数值列进行比较,当找到匹配项时,它将更改“我的“名称”单元格以匹配另一个数据库中的相应名称单元格。

Is there any faster way to do this than using for loops? 是否有比使用for循环更快的方法? I have to replicate the code around 12 times for different sheets to do the same task. 我必须为不同的工作表复制大约12次代码才能完成相同的任务。

As previously stated, overall to run across all 12 its taking around 6 minutes 如前所述,总体来说,要运行所有12个组件大约需要6分钟

Sub 6mincode()

Workbooks("1").Activate

N = Workbooks("1").Sheets("Data").Cells(Rows.Count, "B").End(xlUp).Row
N2 = Workbooks("2").Sheets("Data Sheet").Cells(Rows.Count, "B").End(xlUp).Row

For I = 2 To N
    If (WorksheetFunction.IsNumber(Sheets("Data").Cells(I, "B").Value)) = True Then
        For zz = 8 To N2
            If StrComp(Sheets("Data").Cells(I, "B").Value, Workbooks("2").Sheets("Data Sheet").Cells(zz, "B").Value) = 0 Then
                Workbooks("1").Sheets("Data").Cells(I, "B").Value = Workbooks("2").Sheets("Data Sheet").Cells(zz, "C").Value
            End If
         Next zz
    End If
Next I

End Sub

You can save the second loop and use Application.Match instead, it will save you a lot of time. 您可以保存第二个循环并改为使用Application.Match ,它将节省大量时间。

See code below, explanations inside the code's comments: 请参阅下面的代码,以及代码注释中的解释:

Option Explicit

Sub Sixmincode()

Dim N As Long, N2 As Long, I As Long
Dim Rng As Range, MatchRow

With Workbooks("1").Sheets("Data")
    N = .Cells(.Rows.Count, "B").End(xlUp).Row
End With

With Workbooks("2").Sheets("Data Sheet")
    N2 = .Cells(.Rows.Count, "B").End(xlUp).Row ' get last row with data in column B

    ' set the Range to Match with
    Set Rng = .Range("B8:B" & N2)
End With

With Workbooks("1").Sheets("Data")
    For I = 2 To N
        If IsNumeric(.Cells(I, "B").Value) Then ' use IsNumeric
            ' use Application.Match, if Not IsError means there is a match found in the second workbook
            If Not IsError(Application.Match(.Cells(I, "B").Value, Rng, 0)) Then
                MatchRow = Application.Match(.Cells(I, "B").Value, Rng, 0)
                .Cells(I, "B").Value = Workbooks("2").Sheets("Data Sheet").Cells(MatchRow, "C").Value
            End If
        End If
    Next I
End With

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM