对于If和condition中的每个循环。你能澄清一下吗?

时间:2017-09-20 21:25:23

标签: excel-vba vba excel

代码:

Sub aa()
    Dim a As Range
    Dim b As Range
    For Each a In Range("a1:a10")
        For Each b In Range("b1:b10")
            If a = "Saran" And b = "Raj" Then
                a.Offset(0, 2) = "Correct"
            Else
                a.Offset(0, 2) = "InCorrect"
            End If
        Next
    Next
End Sub

输出:

Saran   Raj     Correct
Saran   Raj     Correct
Saran   Raj     Correct
Saran   Mohan   InCorrect
Saran   Mohan   InCorrect
Saran   Mohan   InCorrect
Saran   Mohan   InCorrect
Saran   Raj     Correct
Saran   Raj     Correct
Saran   Raj     Correct

1 个答案:

答案 0 :(得分:1)

假设"输出"实际上并不是你得到的输出,但认为应该得到的输出,问题是For Each b In Range("b1:b10")的最后一次迭代正在查看"Raj"中的Range("B10").Value值。由于每个a.Value值均为"Saran",因此,在最后b次迭代中,a.Value "Saran"和{{1 } b.Value,因此您应该获得每行中C列"Raj"的答案。

我想你想要

"Correct"