3

I have 2 arrays.

  1. Array1 is a list that includes 3 columns.
  2. Array2 is a list that contains 2 columns.
  3. Columns 1 and 2 of both arrays have the same information.

I need to figure out how to create a third array that contains the 3 columns in Array1 and only contains items that belong to Array2.

Any help is much appreciated.

1
  • You can create the third array as, for instance, Dim Array3() As Variant and then as you want to add new items to it, just ReDim Preserve Array3(1 To x, 1 To 3) (where x is a variable containing how many matches you have found to date) Commented Apr 24, 2017 at 4:13

1 Answer 1

1

Something like this:

Assumes your arrays are in A1:C10 and E1:F10 , pls change to suit.

Sub Arid()
Dim X
Dim Y
Dim Z

Dim lngCnt As Long
Dim lngCnt2 As Long
Dim lngCnt3 As Long

X = Range([a1], [c10]).Value2
Y = Range([E1], [F10]).Value2

ReDim Z(1 To UBound(X), 1 To 3)

For lngCnt = 1 To UBound(X, 1)
    For lngCnt2 = 1 To UBound(Y, 1)
        If X(lngCnt, 1) = Y(lngCnt2, 1) Then
            If X(lngCnt, 2) = Y(lngCnt2, 2) Then
                lngCnt3 = lngCnt3 + 1
                Z(lngCnt3, 1) = X(lngCnt, 1)
                Z(lngCnt3, 2) = X(lngCnt, 2)
                Z(lngCnt3, 3) = X(lngCnt, 3)
                Exit For
            End If
        End If
    Next
Next

End Sub

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.