即使值完全匹配,VBA宏也找不到匹配项

时间:2018-08-12 13:18:07

标签: excel vba excel-vba

我正在运行一个宏以遍历一个工作表,以根据多个条件寻找匹配项。我现在想循环浏览(i,13)=“帐户明细匹配但付款不同”的时间,但请使用(i,12)列作为对(J,14)的匹配。

当我添加此循环时:

For i = 1 To UBound(PensionArr)
    match = False
    For J = 1 To UBound(PayrollArr)
        If CStr(PensionArr(i, 13)) = "Account details match but payment differs" Then

            If CStr(PensionArr(i, 12)) = CStr(PayrollArr(J, 14)) And CStr(PensionArr(i, 4)) = CStr(PayrollArr(J, 5)) And CStr(PensionArr(i, 5)) = CStr(PayrollArr(J, 6)) And CStr(PensionArr(i, 8)) = CStr(PayrollArr(J, 9)) Then
                PensionArr(i, 13) = "Complete match"
                Exit For


                'Looks for SC&AC Match but Amount doesn't
            ElseIf CStr(PensionArr(i, 4)) = CStr(PayrollArr(J, 5)) And CStr(PensionArr(i, 5)) = CStr(PayrollArr(J, 6)) And CStr(PensionArr(i, 8)) <> CStr(PayrollArr(J, 9)) Then
                PensionArr(i, 13) = "Account details match but payment differs"


                'Looks Amount Match but SC&AC don't
            ElseIf CStr(PensionArr(i, 4)) <> CStr(PayrollArr(J, 5)) And CStr(PensionArr(i, 5)) <> CStr(PayrollArr(J, 6)) And CStr(PensionArr(i, 8)) = CStr(PayrollArr(J, 9)) Then
                PensionArr(i, 13) = "Account details do not match but payment is correct"

            End If
        End If
    Next J
Next i

它填充(i,13)为“找不到人”,只有在第二张工作表中不存在该人时,才应是结果。

尽管没有上述循环,但它会根据两张纸之间的不匹配情况使用2个值中的1个填充(i,13)。在添加上述循环之前,没有“找不到人员”。

我的整个代码是:

Sub PensionCheckAccName()

    Application.ScreenUpdating = False
    Application.Calculation =     xlCalculationManual
    Application.EnableEvents = False

    Dim Pension As Worksheet
    Dim Payroll As Worksheet

    Dim cell As Range

    Dim i As Long, J As Long

    Dim PensionArr As Variant
    Dim PayrollArr As Variant

    Dim match As Boolean

    Dim PensionRng As Range
    Dim PayrollRng As Range

    Set Pension = ActiveWorkbook.Sheets("Pensions Bank")
    Set Payroll =  ActiveWorkbook.Sheets("PensionItrent")

    Set PensionRng = Pension.Range("A2",  Pension.Cells(Rows.Count, "A").End(xlUp).Offset(0, 13))
    Set PayrollRng = Payroll.Range("A2", Payroll.Cells(Rows.Count, "A").End(xlUp).Offset(0, 14))

    PensionArr = PensionRng.Value2
    PayrollArr = PayrollRng.Value2

    For i = 1 To UBound(PensionArr)
        match = False
        For J = 1 To UBound(PayrollArr)
            If CStr(PensionArr(i, 6)) = CStr(PayrollArr(J, 7)) And CStr(PensionArr(i, 13)) <> "Complete match" Then


                'Looks for a complete match across SC,AC,Amount

                If CStr(PensionArr(i, 4)) = CStr(PayrollArr(J, 5)) And CStr(PensionArr(i, 5)) = CStr(PayrollArr(J, 6)) And CStr(PensionArr(i, 8)) = CStr(PayrollArr(J, 9)) Then
                    PensionArr(i, 13) = "Complete match"
                    Exit For


                    'Looks for SC&AC Match but Amount doesn't
                ElseIf CStr(PensionArr(i, 4)) = CStr(PayrollArr(J, 5)) And CStr(PensionArr(i, 5)) = CStr(PayrollArr(J, 6)) And CStr(PensionArr(i, 8)) <> CStr(PayrollArr(J, 9)) Then
                    PensionArr(i, 13) = "Account details match but payment differs"


                    'Looks Amount Match but SC&AC don't
                ElseIf CStr(PensionArr(i, 4)) <> CStr(PayrollArr(J, 5)) And CStr(PensionArr(i, 5)) <> CStr(PayrollArr(J, 6)) And CStr(PensionArr(i, 8)) = CStr(PayrollArr(J, 9)) Then
                    PensionArr(i, 13) = "Account details do not match but payment is correct"

                End If
            End If
        Next J
    Next i


    For i = 1 To UBound(PensionArr)
        match = False
        For J = 1 To UBound(PayrollArr)
            If CStr(PensionArr(i, 13)) = "Account details match but payment differs" Then

                If CStr(PensionArr(i, 12)) = CStr(PayrollArr(J, 14)) And CStr(PensionArr(i, 4)) = CStr(PayrollArr(J, 5)) And CStr(PensionArr(i, 5)) = CStr(PayrollArr(J, 6)) And CStr(PensionArr(i, 8)) = CStr(PayrollArr(J, 9)) Then
                    PensionArr(i, 13) = "Complete match"
                    Exit For


                    'Looks for SC&AC Match but Amount doesn't
                ElseIf CStr(PensionArr(i, 4)) = CStr(PayrollArr(J, 5)) And CStr(PensionArr(i, 5)) = CStr(PayrollArr(J, 6)) And CStr(PensionArr(i, 8)) <> CStr(PayrollArr(J, 9)) Then
                    PensionArr(i, 13) = "Account details match but payment differs"


                    'Looks Amount Match but SC&AC don't
                ElseIf CStr(PensionArr(i, 4)) <> CStr(PayrollArr(J, 5)) And CStr(PensionArr(i, 5)) <> CStr(PayrollArr(J, 6)) And CStr(PensionArr(i, 8)) = CStr(PayrollArr(J, 9)) Then
                    PensionArr(i, 13) = "Account details do not match but payment is correct"

                End If
            End If
        Next J
    Next i


    For i = 1 To UBound(PensionArr)
        match = False
        For J = 1 To UBound(PayrollArr)
            If CStr(PensionArr(i, 13)) = "Person not found" Then

                If CStr(PensionArr(i, 12)) = CStr(PayrollArr(J, 14)) And CStr(PensionArr(i, 4)) = CStr(PayrollArr(J, 5)) And CStr(PensionArr(i, 5)) = CStr(PayrollArr(J, 6)) And CStr(PensionArr(i, 8)) = CStr(PayrollArr(J, 9)) Then
                    PensionArr(i, 13) = "Complete match"
                    Exit For


                    'Looks for SC&AC Match but Amount doesn't
                ElseIf CStr(PensionArr(i, 4)) = CStr(PayrollArr(J, 5)) And CStr(PensionArr(i, 5)) = CStr(PayrollArr(J, 6)) And CStr(PensionArr(i, 8)) <> CStr(PayrollArr(J, 9)) Then
                    PensionArr(i, 13) = "Account details match but payment differs"


                    'Looks Amount Match but SC&AC don't
                ElseIf CStr(PensionArr(i, 4)) <> CStr(PayrollArr(J, 5)) And CStr(PensionArr(i, 5)) <> CStr(PayrollArr(J, 6)) And CStr(PensionArr(i, 8)) = CStr(PayrollArr(J, 9)) Then
                    PensionArr(i, 13) = "Account details do not match but payment is correct"
                End If
            End If
        Next J
    Next i

    For i = 1 To UBound(PensionArr)
        If PensionArr(i, 13) = "" Then
            PensionArr(i, 13) = "Person not found"
        End If
    Next i

    PensionRng.Value = PensionArr

    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic
    Application.EnableEvents = True


End Sub

任何帮助都会很好,因为我正在尝试使用以上代码基于多个值比较每个工作表,但是有很多行,其中4列中的3列是相同的,并且只有第4列才使它们唯一。当我尝试使用第4列使行唯一以进行比较时(第一个循环),那么它不匹配136行。

我使用VBA已有很长时间了,因此欢迎您提出任何提示。

让我知道是否需要更多信息

非常感谢,

马特

1 个答案:

答案 0 :(得分:0)

问题出在我循环的顺序上。我在最后设置了“找不到人”,但试图在设置之前在(i,13)中设置的行中循环浏览。

在所有内容都设置好之后,我将问题顶部的问题循环移到了最后,并立即起作用。

我只需要对循环进行重新排序即可使其执行我想要的操作。

仍然感谢您的尝试