Subtracting an array from another array?

Grand

Board Regular
Joined
May 11, 2017
Messages
52
Hi
Is there a way to subtract an array of string from another array of string? I have a larger array and what I need is an array which is the result of the larger array minus the other array.

Code:
Function HideUnwantedElements(MainArray As Variant)

Dim ToBeFilteredOut  As Variant
Dim Array_Filtered As Variant

ToBeFilteredOut = Array("A9673", "A9674", "A9675", "A10066")
‘So what is next? I tried the following but it didn’t work 

For m = LBound(ToBeFilteredOut) To UBound(ToBeFilteredOut)
 Array_Filtered = Filter(MainArray, ToBeFilteredOut (m), False)
Next m

End Function

Any help is appreciated.
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Try this for "MainArray" in column "A" and results in column "C".
NB:-
Mainarray
= One-dimensional array of strings to be searched

<tbody>
</tbody>


Code:
Private [COLOR="Navy"]Sub[/COLOR] CommandButton1_Click()
[COLOR="Navy"]Dim[/COLOR] MArray [COLOR="Navy"]As[/COLOR] Variant
 MArray = Application.Transpose(Range("A1:A20").Value)
  HideUnwantedElements MArray
    Range("C1").Resize(UBound(MArray) + 1).Value = Application.Transpose(MArray)
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]

Function HideUnwantedElements(MainArray [COLOR="Navy"]As[/COLOR] Variant) [COLOR="Navy"]As[/COLOR] Variant
[COLOR="Navy"]Dim[/COLOR] ToBeFilteredOut  [COLOR="Navy"]As[/COLOR] Variant
[COLOR="Navy"]Dim[/COLOR] Array_Filtered [COLOR="Navy"]As[/COLOR] Variant
[COLOR="Navy"]Dim[/COLOR] m [COLOR="Navy"]As[/COLOR] Variant
ToBeFilteredOut = Array("A9673", "A9674", "A9675", "A10066")
    [COLOR="Navy"]For[/COLOR] m = LBound(ToBeFilteredOut) To UBound(ToBeFilteredOut)
        MainArray = Filter(MainArray, ToBeFilteredOut(m), False)
    [COLOR="Navy"]Next[/COLOR] m
[COLOR="Navy"]End[/COLOR] Function
Regards Mick




<tbody>
</tbody>
 
Upvote 0
Code:
Sub Test_aFilter()
  Dim s1, s2, a
  
  s1 = Array("A9673", "A9674", "a9673", "a9674", 1)
  s2 = Array("A9673", "A9674", "A9675", "A10066")
  
  a = aFilter(s1, s2)
  MsgBox Join(a, vbLf)
  
  a = aFilter(s1, s2, , vbBinaryCompare)
  MsgBox Join(a, vbLf)
End Sub

'vbBinaryCompare=0, vbDatabaseCompare=2, vbTextCompare=1
Function aFilter(s1Array, s2Array, _
  Optional tfKeep As Boolean = False, _
  Optional iCompare As Integer = vbTextCompare)
  Dim a, e
  a = s1Array
  For Each e In s2Array
    a = Filter(a, e, tfKeep, iCompare)
  Next e
  aFilter = a
End Function
 
Upvote 0
Thanks to both of you. I have looked at your codes and made some simplifications. Here is what I ended up with and thanks to you, it works:

Code:
Function HideUnwantedElements(MainArray As Variant)

Dim ToBeFilteredOut  As Variant

ToBeFilteredOut = Array("A9673", "A9674", "A9675", "A10066")
    
    For m = LBound(ToBeFilteredOut) To UBound(ToBeFilteredOut)
        MainArray = Filter(MainArray, ToBeFilteredOut(m), False)
    Next m
'MainArray now contains every thing except those elements listed in ToBeFilteredOut array

End Function
 
Upvote 0

Forum statistics

Threads
1,223,889
Messages
6,175,226
Members
452,620
Latest member
dsubash

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