VBA : A열의 내용이 B열 안에 포함되어 있는지 확인

VBA : A열의 내용이 B열 안에 포함되어 있는지 확인

A열 라인 바이 라인으로 B열 안에 포함되어(included) 있는지 확인하여,

결과를 C열에 표시한다.

Sub Macro1()

‘ Macro1 Macro

‘ 바로 가기 키: Ctrl+k

Dim llist(1000)
Dim lcnt As Integer
Dim rlist(1000)
Dim rcnt As Integer

Dim oneStr As String

‘A열 선택
[A1].Select
lcnt = 0

‘리스트에 저장
Do
    oneStr = ActiveCell.Offset(0, 0).Value

    If oneStr = “” Then
        Exit Do
    End If
   
    llist(lcnt) = oneStr
    lcnt = lcnt + 1
   
    If lcnt > 999 Then
        MsgBox (“llist 배열의 크기를 늘려주세요”)
        Exit Sub
    End If

    ActiveCell.Offset(1, 0).Select
Loop

‘B열 선택
[B1].Select
rcnt = 0

‘리스트에 저장
Do
    oneStr = ActiveCell.Offset(0, 0).Value

    If oneStr = “” Then
        Exit Do
    End If
   
    rlist(rcnt) = oneStr
    rcnt = rcnt + 1
   
    If rcnt > 999 Then
        MsgBox (“rlist 배열의 크기를 늘려주세요”)
        Exit Sub
    End If

    ActiveCell.Offset(1, 0).Select
Loop

‘라인 포함여부 확인

Dim hasLine As Boolean

For i = 0 To lcnt
    hasLine = False
   
    For j = 0 To rcnt
        ‘A열의 라인이 B열 라인 안에 포함되어 있을 경우(Instr)
        If InStr(1, rlist(j), llist(i), False) > 0 Then
            hasLine = True
            Exit For
        End If
    Next j
   

    ‘C열에 표시
    [C1].Select
    ActiveCell.Offset(i, 0).Select
       
    If hasLine = True Then
        ActiveCell.Offset(0, 0).Value = “포함”
    Else
        ActiveCell.Offset(0, 0).Value = “미포함”
    End If
Next i

End Sub