vba中嵌套IF的输出不正确

时间:2018-04-02 14:25:37

标签: excel vba nested-if

我想" TRUE"仅当个别值除总值之外也相等时才输出。我只是包含一部分代码,我没有得到正确的输出。 我试图将输出作为附加,但我收到不正确的输出与以下代码。请帮我理解我的错误:

Dim aPRTS, bNIMS,d19, d8, d25, ud, p19, p8, p25, du19, du8, du25, AudLastCol,AudLastRow As Long
For l = 2 To AudLastRow

    aPRTS = .Cells(l, AudLastCol).Value
    bNIMS = .Cells(l, NIMsLastCol).Value

    d19 = .Cells(l, Application.Match("Deployed(1.9)", .Range("A1:A" & AudLastCol), 0)).Value
    d8 = .Cells(l, Application.Match("Deployed (800)", .Range("A1:A" & AudLastCol), 0)).Value
    d25 = .Cells(l, Application.Match("Deployed (2.5)", .Range("A1:A" & AudLastCol), 0)).Value
    p8 = .Cells(l, Application.Match("Total-800-PRTS", .Range("A1:A" & AudLastCol), 0)).Value
    p19 = .Cells(l, Application.Match("Total-1900-PRTS", .Range("A1:A" & AudLastCol), 0)).Value
    p25 = .Cells(l, Application.Match("Total-2500-PRTS", .Range("A1:A" & AudLastCol), 0)).Value
    ud = .Cells(l, Application.Match("Deployed (Unassigned)", .Range("A1:A" & AudLastCol), 0)).Value

    du19 = d19 + ud
    du8 = d8 + ud
    du25 = d25 + ud

    If aPRTS = bNIMS Then
            If (p19 = d19) And (p8 = d8) And (p25 = d25) Then
                .Cells(l, AudLastCol + 1).Value = "TRUE"
                .Cells(l, AudLastCol + 3).Value = "No Action required."
            ElseIf (p19 = du19) And (p8 = d8) And (p25 = d25) Then
                .Cells(l, AudLastCol + 1).Value = "TRUE"
                .Cells(l, AudLastCol + 3).Value = "No Action required."
            ElseIf (p19 = d19) And (p8 = du8) And (p25 = d25) Then
                .Cells(l, AudLastCol + 1).Value = "TRUE"
                .Cells(l, AudLastCol + 3).Value = "No Action required."
            ElseIf (p19 = d19) And (p8 = d8) And (p25 = du25) Then
                .Cells(l, AudLastCol + 1).Value = "TRUE"
                .Cells(l, AudLastCol + 3).Value = "No Action required."
            Else
                .Cells(l, AudLastCol + 1).Value = "FALSE"
                .Cells(l, AudLastCol + 2).Value = "Check Manually"
                .Cells(l, AudLastCol + 3).Value = "Band wise Carrier Mismatch."
            End If
     End If
Next l

enter image description here

2 个答案:

答案 0 :(得分:0)

使用以下代码:

Dim aPRTS, bNIMS, d19, d8, d25, ud, p19, p8, p25, du19, du8, du25, AudLastCol, AudLastRow As Long
For l = 2 To AudLastRow

    aPRTS = .Cells(l, AudLastCol).Value
    bNIMS = .Cells(l, NIMsLastCol).Value

    d19 = .Cells(l, Application.Match("Deployed(1.9)", .Range("A1:A" & AudLastCol), 0)).Value
    d8 = .Cells(l, Application.Match("Deployed (800)", .Range("A1:A" & AudLastCol), 0)).Value
    d25 = .Cells(l, Application.Match("Deployed (2.5)", .Range("A1:A" & AudLastCol), 0)).Value
    p8 = .Cells(l, Application.Match("Total-800-PRTS", .Range("A1:A" & AudLastCol), 0)).Value
    p19 = .Cells(l, Application.Match("Total-1900-PRTS", .Range("A1:A" & AudLastCol), 0)).Value
    p25 = .Cells(l, Application.Match("Total-2500-PRTS", .Range("A1:A" & AudLastCol), 0)).Value
    ud = .Cells(l, Application.Match("Deployed (Unassigned)", .Range("A1:A" & AudLastCol), 0)).Value

    du19 = d19 + ud
    du8 = d8 + ud
    du25 = d25 + ud

    If aPRTS = bNIMS Then
        If (p19 = d19 Or p19 = du19) And (p8 = d8 Or p8 = du8) And (p25 = d25 Or p25 = du25) Then
            .Cells(l, AudLastCol + 1).Value = "TRUE"
            .Cells(l, AudLastCol + 3).Value = "No Action required."
        Else
            .Cells(l, AudLastCol + 1).Value = "FALSE"
            .Cells(l, AudLastCol + 3).Value = "Band wise Carrier Mismatch."
        End If
    End If
Next l

答案 1 :(得分:0)

自OP的代码更改后编辑(...)

你必须改变:

d19 = .Cells(l, Application.Match("Deployed(1.9)", .Range("A1:A" & AudLastCol), 0)).Value

为:

d19 = .Cells(l, Application.Match("Deployed(1.9)", .Rows(1).Resize(,AudLastCol), 0)).Value

实际搜索row1到AudLastCol列而不是第1列到AudLastCol行

与其他代码行相同