+ Reply to Thread
Results 1 to 11 of 11

Tricky: How to check if two different values are in the same column

  1. #1
    Registered User
    Join Date
    04-22-2021
    Location
    Lisbon, Portugal
    MS-Off Ver
    365
    Posts
    6

    Tricky: How to check if two different values are in the same column

    Good evening,

    Having the following data:
    Col 1 Col 2
    A B
    B C
    C D

    Determine if two given values exist in the same column. In this case, should only be false for the combinations of A and D (order doesn't matter)

    I know something like this would work:
    OR(
    AND( ISNUMBER(MATCH("A",Col1,0)) , ISNUMBER(MATCH("B",Col1,0)) ),
    AND( ISNUMBER(MATCH("A",Col2,0)) , ISNUMBER(MATCH("B",Col2,0)) ),
    ...
    )
    I'm hoping there's a better way of doing this - If not, every time I add another column, I'd have to review this function - bad idea


    Tried thinking about the issue in a different way, but couldn't come up with a solution either.
    In this case, return true if two given values are shared with the same key:
    key value
    Col 1 A
    Col 1 B
    Col 1 C
    Col 2 B
    Col 2 C
    Col 2 D

    Thank you for your time

  2. #2
    Registered User
    Join Date
    10-07-2020
    Location
    Rome, Ga.
    MS-Off Ver
    2016
    Posts
    15

    Re: Tricky: How to check if two different values are in the same column

    Are you opposed to using conditional formatting to find duplicates or are you truly just wanting a formula for TRUE, FALSE?

  3. #3
    Forum Moderator
    Join Date
    01-21-2014
    Location
    St. Joseph, Illinois U.S.A.
    MS-Off Ver
    Office 365 v 2404
    Posts
    13,406

    Re: Tricky: How to check if two different values are in the same column

    Hello mrcdinis.

    Welcome to the forum.

    Perhaps something like this?

    Using this formula in cell C2
    Formula: copy to clipboard
    Please Login or Register  to view this content.



    A
    B
    C
    1
    key
    value
    2
    Col 1
    A
    TRUE
    3
    Col 1
    B
    4
    Col 1
    C
    5
    Col 2
    B
    6
    Col 2
    C
    7
    Col 2
    D
    Dave

  4. #4
    Forum Moderator
    Join Date
    01-21-2014
    Location
    St. Joseph, Illinois U.S.A.
    MS-Off Ver
    Office 365 v 2404
    Posts
    13,406

    Re: Tricky: How to check if two different values are in the same column

    I also think this will work in C2, but try it on a few samples to see.
    Formula: copy to clipboard
    Please Login or Register  to view this content.


    These are all assuming the values in column B are representative of the real data.
    Last edited by FlameRetired; 04-22-2021 at 07:24 PM.

  5. #5
    Registered User
    Join Date
    04-22-2021
    Location
    Lisbon, Portugal
    MS-Off Ver
    365
    Posts
    6

    Re: Tricky: How to check if two different values are in the same column

    Quote Originally Posted by RSkeen View Post
    Are you opposed to using conditional formatting to find duplicates or are you truly just wanting a formula for TRUE, FALSE?
    I don't need any visual input
    For the specific issue at hand, all I need is something that returns TRUE/FALSE given the specified conditions: 2 inputs, have to match a specific context in order to make a decision later on
    Last edited by mrcdinis; 04-22-2021 at 08:45 PM.

  6. #6
    Registered User
    Join Date
    04-22-2021
    Location
    Lisbon, Portugal
    MS-Off Ver
    365
    Posts
    6

    Re: Tricky: How to check if two different values are in the same column

    Quote Originally Posted by FlameRetired View Post
    I also think this will work in C2, but try it on a few samples to see.
    Formula: copy to clipboard
    Please Login or Register  to view this content.


    These are all assuming the values in column B are representative of the real data.
    Thank you for the quick reply, and holy moly that looks like a tiny thing! Will check if it solves my issue

  7. #7
    Registered User
    Join Date
    04-22-2021
    Location
    Lisbon, Portugal
    MS-Off Ver
    365
    Posts
    6

    Re: Tricky: How to check if two different values are in the same column

    ok, maybe I've expressed it wrong. Here's the thing:

    everything inside the table is actual data, volatile data: everything may change at some point.

    I have to group things into categories, let's say this is my data:
    foo item
    foo data
    foo stuff
    foo this
    bar this
    bar that
    bar item
    other something
    other else

    And this is my input and output:

    item + data = TRUE (both share "foo")
    stuff + that = FALSE (one is in foo, the other is in bar)
    something + else = TRUE (both share "other")

    all data can be changed at any point. It's the actual check that has to keep working regardless

  8. #8
    Registered User
    Join Date
    04-22-2021
    Location
    Lisbon, Portugal
    MS-Off Ver
    365
    Posts
    6

    Re: Tricky: How to check if two different values are in the same column

    Hey, a solution has just been presented to me via discord from a friend of Mine, which works perfectly for what I'm after.

    MAX(
    (
    FILTER(A:A,B:B=H3)
    =
    TRANSPOSE(FILTER(A:A,B:B=H4))
    )*1
    )=1

    Where A:A is the first column, and B:B the second
    H3 is the first keyword to check, and H4 is the second

    Great usage of arrays right there


    Thank you all for your time.

  9. #9
    Forum Expert
    Join Date
    07-06-2004
    Location
    Northern California
    MS-Off Ver
    2K, 2003, 2010, O365
    Posts
    1,490

    Re: Tricky: How to check if two different values are in the same column

    Problem is you'd need either a 3-D array or a way to iterate through columns or through "A", "B" and perhaps more values. Neither are available using only built-in worksheet functions.

    If your columns would always be in a single, multiple-column range with each column having the same number of rows OR any column with fewer rows having blank cells where the other columns have data, name the range containing all the columns Cols. Then if there were only 2 values, "A" and "B", try

    Formula: copy to clipboard
    Please Login or Register  to view this content.

    Since you show you're using Office 365, you should be able to enter this as a regular formula. This would need to be entered as an array formula in previous versions, so [Ctrl]+[Shift]+[Enter].

    The --(Cols="X") terms are arrays of 1s and 0s depending on whether values in Cols equal "X". The MMULT(...) calls reduce that to horizontal arrays of the number of instances in each column of Cols, so given your data, {1,0} for "A" and {1,1} for "B". The SIGN(...) calls reduce those to 1s for positives and 0s for 0. Adding the results of the SIGN(...) calls gives 2 when both "A" and "B" appear in a column, 1 if only one of them appears, 0 if neither appears. So the OR(...) call returns TRUE if one column in Cols has both "A" and "B", FALSE otherwise.

    This is relatively inefficient. VBA might be better for something like this.

    Please Login or Register  to view this content.
    If the range of columns were named Cols, call this user-defined function in formulas like

    Formula: copy to clipboard
    Please Login or Register  to view this content.

    I figure for a large number of values ({"A";"B";...}) and large Cols range, this may be faster to calculate than complex formulas calling several built-in functions. The optional arguments make this broadly applicable, though you could extend Anum and Bnum semantics to negative numbers meaning all less Xnum, so if the B argument had, say, 5 columns, Bnum of -2 would mean find Anum matches in 5-2=3 columns in B. Left for you or others.

  10. #10
    Registered User
    Join Date
    04-22-2021
    Location
    Lisbon, Portugal
    MS-Off Ver
    365
    Posts
    6

    Re: Tricky: How to check if two different values are in the same column

    Quote Originally Posted by hrlngrv View Post
    Problem is you'd need either a 3-D array or a way to iterate through columns or through "A", "B" and perhaps more values. Neither are available using only built-in worksheet functions.
    (...)
    I'm sorry you went through all that :/
    I've posted the solution given to me, which is already working. Didn't have to go with VBA

    It's basically creating a 2d array of booleans, where each cell is true only when both col (FILTER A) and row (TRANSPOSE of FILTER B) match the same value.

  11. #11
    Forum Expert
    Join Date
    07-06-2004
    Location
    Northern California
    MS-Off Ver
    2K, 2003, 2010, O365
    Posts
    1,490

    Re: Tricky: How to check if two different values are in the same column

    If A1:A3 contained {"A";"B";"C"}, B1:B3 contained {"B";"C";"D"}, A4:B1048576 were all blank, H3 evaluated to "A", and H4 evaluated to "B" (close to the example in your original post above), FILTER(A:A,B:B=H3) would return a #CALC! error since "A" wouldn't appear in B:B, so FILTER would have nothing to return. [Tangent: wouldn't #NULL! be more appropriate for this particular situation? Consistency isn't a MSFT strength.]

    As shown, I don't see how the formula you show produces anything other than an error using data from your original post.

    You had mentioned wanting to avoid editing formulas if you add more columns. How could you add columns without needing to edit the formula in your post immediately above since the formula only includes A:A and B:B?

+ 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] Tricky formula to match data in 3 column or disregard a column if not applicable
    By tml2424 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 01-06-2021, 03:57 AM
  2. Check if all values in a column are between two defined values
    By AKJRADIO in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 07-07-2020, 09:59 AM
  3. Replies: 1
    Last Post: 03-01-2020, 05:59 AM
  4. [SOLVED] Check all rows in UsedRange - if column A is not empty do stuff with column A and B values
    By Arnold Layne in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-14-2020, 04:28 PM
  5. [SOLVED] Tricky: Sum if either column contains value AND third column contains EITHER of two values
    By omahacrab in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 11-10-2012, 02:17 PM
  6. Replies: 7
    Last Post: 10-01-2012, 09:20 AM
  7. Script to check for duplicates in one column then alter adjacent column values
    By SebN in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 04-29-2010, 12:23 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