Public Sub GetMatchingElements()
Dim vArray3 As Variant
Dim v1 As Variant
Dim v2 As Variant
Dim sFormat As String
Dim i As Long
v1 = Array("DX", "DZ", "WX", "TX", 0, 0, 0, 0, 0)
v2 = Array("BX", "CX", "DS", "EX", "FX", "HX", "IX", "KX", "LK", "N", "NX", "OP", "SK", "SX", "TX", "WX", "ZX", 0, 0, 0, "WX", "WX", "WX", "WX", "WX", "WX")
i = iMatchElements(v1, v2, vArray3)
i = Application.WorksheetFunction.RoundUp((Log(i) + 1) / Log(10), 0)
sFormat = Application.WorksheetFunction.Rept("0", i)
For i = LBound(vArray3) To UBound(vArray3)
Debug.Print Format(i, sFormat) & ":", vArray3(i)
Next i
End Sub
Private Function iMatchElements(v1 As Variant, v2 As Variant, ByRef vArray3 As Variant) As Long
Dim vShorter As Variant
Dim vLonger As Variant
Dim iSize As Long
Dim iPos As Long
Dim i As Long
iSize = Application.Min(UBound(v1), UBound(v2))
If iSize = UBound(v1) Then
vShorter = v1
vLonger = v2
Else
vShorter = v2
vLonger = v1
End If
ReDim vArray3(1 To iSize)
iPos = 0
For i = LBound(vShorter) To UBound(vShorter)
If vShorter(i) <> 0 Then
On Error Resume Next
If IsNumeric(Application.Match(vShorter(i), vLonger, 0)) Then
iPos = iPos + 1
vArray3(iPos) = vShorter(i)
End If
On Error GoTo 0
End If
Next i
If iPos < i Then
ReDim Preserve vArray3(1 To iPos)
End If
iMatchElements = iPos
End Function