如何比较两列中的值,然后在第三列中标记true / false?

时间:2016-02-03 21:50:18

标签: vba find excel-2010 lookup

有人可以帮我创建一个宏,它会搜索工作表上的两列以获取条件列表,并在第三列上标记true / false。 (办公室2010年)

e.g。

A列将具有以下值:1111,1,2,3,3,4,... B列具有以下值:O,A,Y,A,S,3Y,......

如果满足以下匹配条件,则列C将标记为TRUE,否则为FALSE。

A       B
1111 =  O
0    =  Y
1    =  A
2    =  S
3    =  3YRY
4    =  Q
6    =  B
12   =  M
13   =  V
360  =  D

条件规则:

IF column A = 1111 AND column B = O 
OR 
IF column A = 0 AND column B = Y 
OR 
IF column A = 1 AND column B = A 
OR 
IF column A = 2 AND column B = S 
OR 
IF column A = 3 AND column B = 3YR 
OR 
IF column A = 4 AND column B = Q 
OR 
IF column A = 6 AND column B = B 
OR 
IF column A = 12 AND column B = M 
OR 
IF column A = 13 AND column B = V 
OR 
IF column A = 360 AND column B = D 
THEN COLUMN C = "TRUE" ELSE "FALSE"

1 个答案:

答案 0 :(得分:1)

这应该与您想要完成的事情更紧密地匹配,有关此处发生的事情的任何问题都告诉我。

Option Base 1

Sub testCriteria()

'arrays for criteria r, r2.  Array for T/F r3
Dim r, r2, r3(10, 1)

'iterators for loop and variable for output column
Dim i As Long, j As Long, c As Long

    'column for output of t/f
    c = 3

    'location of criteria cells h1 through i10
    r = [h1:i10]

    'location of comparison
    r2 = [a1:b10]

    'loop through rows of rows to check (r2) and compare with all rows from criteria (r)
    For i = LBound(r2) To UBound(r2)
        For j = LBound(r) To UBound(r)
            If CStr(r(j, 1)) = CStr(r2(i, 1)) _
            And CStr(r(j, 2)) = CStr(r2(i, 2)) _
            Then r3(i, 1) = "TRUE"
        Next j
        If Not r3(i, 1) Then r3(i, 1) = "FALSE"
    Next i

    'reusing iterators for array limits
    i = LBound(r3): j = UBound(r3)

    'loading t/f array into api
    Range(Cells(i, c), Cells(j, c)) = r3

End Sub