简体   繁体   中英

VBA Macro isn't finding a match even though values are an exact match

I'm running a Macro to loop through a sheet looking for matches bases on multiple criteria. I'm at the point where I want to loop through one more time where (i,13) = "Account details match but payment differs" but use column (i,12) as a match against (J, 14).

When I add this loop though:

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

It populated (i, 13) as "Person not found" which should only be the outcome if the person doesn't exist in the second sheet.

Without the above loop though it populates (i,13) with 1 of 2 values depending on what doesn't match between sheets. There's no "Person not found" until I add the above loop in.

My entire code is:

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

Any help would be good as I'm trying to use the above code to compare each sheet based on multiple values but there's a lot of rows where 3 of the 4 columns are identical and they are only made unique by the 4th column. When I try to use the 4th column to make the rows unique for comparison (the first loop) it then doesn't match 136 rows.

I haven't been using VBA for very long so any tips are welcome.

Let me know if you need more information

Many thanks,

Matt

The problem was the order of my loops. I was setting the "Person not found" right at the end yet trying to loop through rows with that set in (i, 13) prior to it being set.

I then moved the problem loop at the top of my question to the end after everything had been set and it worked straight away.

I just needed to reorder my loops to get it to do what I wanted.

Thanks anyway for trying

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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