VBA function to consolidate arrays

rharn

Board Regular
Joined
Jun 21, 2011
Messages
54
I am trying to create a VBA function to consolidate two arrays that is generated from a sub. The objective is to compare to values contained in each array and to put any duplicated values into a new array and to finally pass the array back into the sub. There has been several variations of this function and I have attempted to create a function based off of these functions but my function currently enouncters a 'invalid or unqualified reference' when I try to use the built in .Match function. I would greatly appreciate it if anyone can take a quick look at my code below and let me know what's wrong!

Code:
Function lnArray(X As Variant, Y As Variant) As Variant
    Dim counter1 As Long
    Dim xcount As Long
    Dim t As Long
    Dim FinalResults() As Variant
 
        For xcount = LBound(X) To UBound(X)
            t = .Match(X(xcount), Y, 0)
                If (t > 0) Then
                    counter1 = counter1 + 1
                    ReDim Preserve FinalResults(counter1)
                    FinalResults(counter1) = X(xcount)
                End If
        Next xcount
 
    lnArray = FinalResults
End Function
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Code:
[COLOR="Blue"]Function[/COLOR] lnArray(X [COLOR="Blue"]As[/COLOR] Variant, Y [COLOR="Blue"]As[/COLOR] Variant) [COLOR="Blue"]As[/COLOR] Variant
    [COLOR="Blue"]Dim[/COLOR] counter1 [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]Long[/COLOR]
    [COLOR="Blue"]Dim[/COLOR] xcount [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]Long[/COLOR]
    [COLOR="Blue"]Dim[/COLOR] t [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]Long[/COLOR]
    [COLOR="Blue"]Dim[/COLOR] FinalResults() [COLOR="Blue"]As[/COLOR] Variant
 
        [COLOR="Blue"]For[/COLOR] xcount = [COLOR="Blue"]LBound[/COLOR](X) [COLOR="Blue"]To[/COLOR] [COLOR="Blue"]UBound[/COLOR](X)
            [COLOR="Blue"]On[/COLOR] [COLOR="Blue"]Error[/COLOR] [COLOR="Blue"]Resume[/COLOR] [COLOR="Blue"]Next[/COLOR]
            t = .Match(X(xcount), Y, 0)
            [COLOR="Blue"]If[/COLOR] Err.Number = 0 [COLOR="Blue"]Then[/COLOR]
                [COLOR="Blue"]If[/COLOR] (t > 0) [COLOR="Blue"]Then[/COLOR]
                    counter1 = counter1 + 1
                    [COLOR="Blue"]ReDim[/COLOR] [COLOR="Blue"]Preserve[/COLOR] FinalResults(counter1)
                    FinalResults(counter1) = X(xcount)
                [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]If[/COLOR]
            [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]If[/COLOR]
            [COLOR="Blue"]On[/COLOR] [COLOR="Blue"]Error[/COLOR] [COLOR="Blue"]GoTo[/COLOR] 0
        [COLOR="Blue"]Next[/COLOR] xcount
 
    lnArray = FinalResults
[COLOR="Blue"]End[/COLOR] [COLOR="Blue"]Function[/COLOR]
 
Upvote 0
Thanks again for the help Sektor, VBEditor is still telling me that in order to use the .Match function I need a with block or take out the "." Unfortunately I am not sure exactly how the with block would be set up. And I tried taking out the "." but that gave me an 'undefine sub or function' error.
 
Upvote 0
.match should be proceded by

with Object of some Kind
.Method of object
end with

what object has a match method? I can only find WorksheetFunction!
 
Upvote 0
Ah yes... Completely missed it...
Code:
[COLOR="Blue"]Function[/COLOR] lnArray(X [COLOR="Blue"]As[/COLOR] Variant, Y [COLOR="Blue"]As[/COLOR] Variant) [COLOR="Blue"]As[/COLOR] Variant
    [COLOR="Blue"]Dim[/COLOR] counter1 [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]Long[/COLOR]
    [COLOR="Blue"]Dim[/COLOR] xcount [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]Long[/COLOR]
    [COLOR="Blue"]Dim[/COLOR] t [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]Long[/COLOR]
    [COLOR="Blue"]Dim[/COLOR] FinalResults() [COLOR="Blue"]As[/COLOR] Variant
 
        [COLOR="Blue"]For[/COLOR] xcount = [COLOR="Blue"]LBound[/COLOR](X) [COLOR="Blue"]To[/COLOR] [COLOR="Blue"]UBound[/COLOR](X)
            [COLOR="Blue"]On[/COLOR] [COLOR="Blue"]Error[/COLOR] [COLOR="Blue"]Resume[/COLOR] [COLOR="Blue"]Next[/COLOR]
            t = WorksheetFunction.Match(X(xcount), Y, 0)
            [COLOR="Blue"]If[/COLOR] Err.Number = 0 [COLOR="Blue"]Then[/COLOR]
                [COLOR="Blue"]If[/COLOR] (t > 0) [COLOR="Blue"]Then[/COLOR]
                    counter1 = counter1 + 1
                    [COLOR="Blue"]ReDim[/COLOR] [COLOR="Blue"]Preserve[/COLOR] FinalResults(counter1)
                    FinalResults(counter1) = X(xcount)
                [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]If[/COLOR]
            [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]If[/COLOR]
            [COLOR="Blue"]On[/COLOR] [COLOR="Blue"]Error[/COLOR] [COLOR="Blue"]GoTo[/COLOR] 0
        [COLOR="Blue"]Next[/COLOR] xcount
 
    lnArray = FinalResults
[COLOR="Blue"]End[/COLOR] [COLOR="Blue"]Function[/COLOR]
 
Upvote 0
You can try this:

Code:
Function lnArray(X As Variant, Y As Variant) As Variant
    Dim counter1 As Long
    Dim xcount As Long
    Dim t As Long
    Dim FinalResults() As Variant
 
        For xcount = LBound(X) To UBound(X)
On Error Resume Next
            t = 0
            t = WorksheetFunction.Match(X(xcount), Y, 0)
On Error GoTo 0
                If (t > 0) Then
                    counter1 = counter1 + 1
                    ReDim Preserve FinalResults(counter1)
                    FinalResults(counter1) = X(xcount)
                End If
        Next xcount
 
    lnArray = FinalResults
End Function
 
Upvote 0
It seems I have an underlying boundary error for my X() array even after I correct:

Code:
Function lnArray(X[COLOR=red]()[/COLOR] As Variant, Y[COLOR=#ff0000]()[/COLOR] As Variant) As Variant

X() and Y() are the two arrays that are to be passed into the function to be consolidated.
 
Upvote 0
If your Arrays are from ranges as show below , you need to convert them to a 1 dimentional array using "Transpose" or change your function to show your "X" array as :- X(xcount,1)

Code:
Dim ray
Dim A
Dim B
A = Application.Transpose(Range("A1:A10").value)
B = Application.Transpose(Range("B1:B10").value)
   ray = LnArray(A, B)
Range("C1").Resize(UBound(ray) + 1) = Application.Transpose(ray)
 
Upvote 0
MickG, my arrays come from arrays in another sub so I don't believe transposing them is necessary. Both are 1 dimensional arrays as well.
 
Upvote 0
I think your Y array should be sorted

But really match is short for writing
Code:
Found=false
 
For Z = 0 to Ubound(y)-1
  if Y(Z)=X(Xcount) then
     Found=true
 endif
 
next z
 
Upvote 0

Forum statistics

Threads
1,224,522
Messages
6,179,299
Members
452,904
Latest member
CodeMasterX

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top