+ Reply to Thread
Results 1 to 8 of 8

Filtering an array using another array

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-08-2014
    Location
    israel
    MS-Off Ver
    office 365
    Posts
    101

    Filtering an array using another array

    Hello

    I have 2 arrays
    ArrA = Array("A", "B", "C", "D", "E", "F")
    ArrB = Array(0, 0, 1, 0, 1, 0)

    I want to filter array ArrA by array ArrB
    The desired result is a new array MyArr
    which will contain only the values different from zero in the product of the two arrays

    I tried using:
    MyArr = Filter(ArrA, ArrB)

    the desired result
    MyArr (0)="C"
    MyArr (1)="E"

    Is there a simple way to do it without loops?

  2. #2
    Registered User
    Join Date
    04-10-2022
    Location
    London, England
    MS-Off Ver
    365
    Posts
    20

    Re: Filtering an array using another array

    Filter will not work in this situation as it is only able to Filter the first array (ArrA) based on a string and the criteria set in the rest of the arguments. It's worth looking at this documentation which explains how it works, https://learn.microsoft.com/en-us/of...ilter-function.

    I don't think it is possible to do this in VBA without looping. Can I ask why you don't want to loop? Functions like Filter etc mostly have loops operating beneath the surface anyway.
    Giuseppe
    www.effinovate.com

  3. #3
    Forum Contributor
    Join Date
    07-08-2014
    Location
    israel
    MS-Off Ver
    office 365
    Posts
    101

    Re: Filtering an array using another array

    Thank you very much for the response

    The desired result is part of another macro that needs to be done many times
    Therefore I am looking for an efficient way to find the final values

    If there is no other way besides loops, I would appreciate for advice or an example
    How can you do it with a loop

  4. #4
    Forum Contributor
    Join Date
    07-08-2014
    Location
    israel
    MS-Off Ver
    office 365
    Posts
    101

    Re: Filtering an array using another array

    Attachment 825699

    Using the filter function in the worksheet brings the desired result

  5. #5
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,835

    Re: Filtering an array using another array

    In that particular small arrays.
        ArrA = Array("A", "B", "C", "D", "E", "F")
        ArrB = Array(0, 0, 1, 0, 1, 0)
        ArrB = Filter(Evaluate("if({" & Join(ArrB, ",") & "}=1,column(" & Cells(1).Resize(, UBound(ArrB) + 1).Address & "))"), False, 0)
        MyArr = Application.Index(ArrA, ArrB)
        If LBound(ArrA) = 0 Then ReDim Preserve MyArr(0 To UBound(MyArr) - 1)

  6. #6
    Forum Contributor
    Join Date
    07-08-2014
    Location
    israel
    MS-Off Ver
    office 365
    Posts
    101

    Re: Filtering an array using another array

    Wowwww

    An elegant and really genius solution!!!!!!!

    Thank you

    Exactly what I needed!!!

  7. #7
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,835

    Re: Filtering an array using another array

    If your version can use Filter worksheet function then simply
    MyArr = WorksheetFunction.Filter(ArrA, ArrB)
    then you need to adjust the L/Ubound with Redim Preserve key word.

  8. #8
    Forum Contributor
    Join Date
    07-08-2014
    Location
    israel
    MS-Off Ver
    office 365
    Posts
    101

    Re: Filtering an array using another array

    Quote Originally Posted by jindon View Post
    If your version can use Filter worksheet function then simply
    MyArr = WorksheetFunction.Filter(ArrA, ArrB)
    then you need to adjust the L/Ubound with Redim Preserve key word.
    Even simpler than the excellent solution you gave earlier
    Thank you

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Filtering an array - error when there is no longer a match in the array
    By nigelog in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-17-2018, 11:25 AM
  2. Replies: 5
    Last Post: 02-22-2018, 01:47 AM
  3. Filtering array or similar
    By Solaris in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 06-24-2013, 11:06 AM
  4. [SOLVED] New to arrays-how do I build new array while filtering first array?
    By mc84excel in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 05-03-2013, 05:59 AM
  5. [SOLVED] Quick Array question - Copy array to another array then resize?
    By mc84excel in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 05-02-2013, 01:17 AM
  6. Array Filtering
    By mayhem12 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-15-2011, 06:43 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1