Sub test()
Dim a, b, x, i As Long, ii As Long, rngSite As Range
Dim myCols(), n As Long, myS, temp, t As Long
Set rngSite = Sheets("SITE_TABLE").Cells(1).CurrentRegion
With Sheets("matching").Cells(1).CurrentRegion
t = Application.Match("preference_output*", .Rows(1), 0)
For ii = 1 To t - 1
If .Cells(1, ii).Address = .Cells(1, ii).MergeArea(1).Address Then
n = n + 1: ReDim Preserve myCols(1 To 2, 1 To n)
myCols(1, n) = ii: myCols(2, n) = .Cells(1, ii).MergeArea.Count + ii - 1
End If
Next
If n <> 4 Then MsgBox "Number of merged area in 1st row should be 4", vbCritical: Exit Sub
a = .Resize(, t - 1).Value
.Columns(t).Offset(2).Resize(, myCols(2, 4) - myCols(1, 4) + 1).ClearContents
b = .Columns(t).Resize(, myCols(2, 4) - myCols(1, 4) + 1).Value
For i = 3 To UBound(a, 1)
ReDim myS(1 To myCols(2, 2) - myCols(2, 1) + 1)
For ii = myCols(1, 2) To myCols(2, 2)
If a(i, ii) <> "" Then myS(ii) = a(i, ii)
Next
For ii = myCols(1, 3) To myCols(2, 3)
If a(i, ii) <> "" Then
x = Application.VLookup(a(i, ii), rngSite, 2, False)
If Not IsError(x) Then
If x <> "" Then
ReDim Preserve myS(1 To UBound(myS) + 1)
myS(UBound(myS)) = x
End If
End If
End If
Next
For ii = myCols(1, 4) To myCols(2, 4)
If a(i, ii) <> "" Then
x = Application.VLookup(a(i, ii), rngSite, 2, False)
If Not IsError(x) Then
If x <> "" Then
If IsError(Application.Match(x, myS, 0)) Then
b(i, ii - myCols(1, 4) + 1) = a(i, ii)
End If
Else
b(i, ii - myCols(1, 4) + 1) = a(i, ii) & " (No Match)"
End If
Else
b(i, ii - myCols(1, 4) + 1) = a(i, ii) & " Invalid Entry!"
End If
End If
Next
Next
.Columns(t).Resize(, UBound(b, 2)).Value = b
End With
End Sub
Bookmarks