Excel VBA地址比较输出不匹配的地址

时间:2018-10-05 20:23:51

标签: excel vba excel-vba

我正在研究一个带有三个选项卡的工作簿。我的客户列表地址,外包客户列表地址:和输出不匹配:。我正在为列表代理运行一个外包列表,如果我的地址列表与外包列表上的任何地址都不匹配。它在“不匹配”选项卡上输出。

我已经建立了一个工作文档,但是它太慢了,觉得这里有人真的可以帮助我指出正确的方向。

所有三个工作表列标题(“客户名称”,“地址1”,“地址2”,“城市”,“州”,“邮政编码”)

我正在使用类似于以下代码的代码在所有列上均找不到匹配项。它只查看前几个字符,希望可以加快速度,但我没有很快。

我正在像这样的循环上运行它,在比较地址代理200,000条记录时,这似乎是非常连续且缓慢的。

For I = 2 To LastRow

If Left(UCase(Trim(wsAddressS_1.Cells(1 + I, 6).Value)), 5) = 
Left(UCase(VLookLike(wsAddressS_1.Cells(1 + I, 6).Value, wsAddressS_2.Range("F1:F" & LastRow2 + 10))), 5) Then
    Match_Zip = "Match"
Else
    Match_Zip = "No Match"
End If

If strMatchZip <> "Match" Then

            LastRow1 = wsAddressS_4.Range("F" & Rows.Count).End(xlUp).Row
            wsAddressS_4.Cells(LastRow4 + 1, 1).Value = wsAddressS_1.Cells(1 + I, 1).Value
            wsAddressS_4.Cells(LastRow4 + 1, 2).Value = wsAddressS_1.Cells(1 + I, 2).Value
            wsAddressS_4.Cells(LastRow4 + 1, 3).Value = wsAddressS_1.Cells(1 + I, 3).Value
            wsAddressS_4.Cells(LastRow4 + 1, 4).Value = wsAddressS_1.Cells(1 + I, 4).Value
            wsAddressS_4.Cells(LastRow4 + 1, 5).Value = wsAddressS_1.Cells(1 + I, 5).Value
            wsAddressS_4.Cells(LastRow4 + 1, 6).Value = wsAddressS_1.Cells(1 + I, 6).Value

End If

    Sleep 10
    DoEvents
Next I

例如VLookLike

Private Function VLookLike(txt As String, rng As Range) As String
Dim temp As String, e, n As Long, a()
Static RegX As Object
If RegX Is Nothing Then
    Set RegX = CreateObject("VBScript.RegExp")
    With RegX
        .Global = True
        .IgnoreCase = True
        .Pattern = "(\S+).*" & Chr(2) & ".*\1"
    End With
End If
With RegX
    For Each e In rng.Value
        If UCase$(e) = UCase(txt) Then
            VLookLike = e
            Exit For
        End If
        temp = Join$(Array(e, txt), Chr(2))
        If .test(temp) Then
            n = n + 1
            ReDim Preserve a(1 To 2, 1 To n)
            a(2, n) = e
            Do While .test(temp)
                a(1, n) = a(1, n) + Len(.Execute(temp)(0).submatches(0))
                temp = Replace(temp, .Execute(temp)(0).submatches(0), "")
            Loop
        End If
    Next
End With
If (VLookLike = "") * (n > 0) Then
    With Application
        VLookLike = .HLookup(.Max(.Index(a, 1, 0)), a, 2, False)
    End With
End If
End Function

任何帮助或建议将不胜感激!

1 个答案:

答案 0 :(得分:0)

抱歉,我还没有阅读所有代码,但是在比较字符串时遇到了问题。如果您告诉vba您要比较2个字符串,则可能会起作用。例如,您可以使用函数Cstr()

CStr(Left(UCase(StrAddress), 3)) = CStr(Left(UCase(VLookLike(StrAddress, rng2)), 3))