+ Reply to Thread
Results 1 to 7 of 7

Mark Cell Based on Two Columns

  1. #1
    Registered User
    Join Date
    01-25-2017
    Location
    United States
    MS-Off Ver
    2010
    Posts
    13

    Post Mark Cell Based on Two Columns

    Hi all,
    I'm trying to find a way to mark a cell based off two matching columns.

    A colleague has suggested two nested "Do Whiles" but the original sheet goes over about 7000 cells which would take even the macro a bit, even with optimized code.

    I've thought about even just using formulas, but I can't find out how to logic that either based off how one of the tables is set up (The one on the right)

    I've attached a doc with a "Before" and "After" mockup.
    I've been stuck on this for too long to admit...
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    03-23-2004
    Location
    London, England
    MS-Off Ver
    Excel 2019
    Posts
    7,064

    Re: Mark Cell Based on Two Columns

    In C6
    =IF(ISNA(MATCH($B6,IF(C$5=$H$6:$H$10,$G$6:$G$10),0)),"","X")
    Array formula, use Ctrl-Shift-Enter
    copy across and down as far as E8

    Bear in mind this is an array formula so it may be slow if you have 7000 cells.
    Try it and see I guess.
    Regards
    Special-K

    Ensure you describe your problem clearly, I have little time available to solve these problems and do not appreciate numerous changes to them.

  3. #3
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Mark Cell Based on Two Columns

    Using code :
    Please Login or Register  to view this content.
    1. I care dog
    2. I am a loop maniac
    3. Forum rules link : Click here
    3.33. Don't forget to mark the thread as solved, this is important

  4. #4
    Registered User
    Join Date
    01-25-2017
    Location
    United States
    MS-Off Ver
    2010
    Posts
    13

    Re: Mark Cell Based on Two Columns

    You guys solved it so quick!
    Both codes worked perfectly!

    I hate to trouble you more, but would you guys be able to explain them a bit?
    Thank you both so much

  5. #5
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Mark Cell Based on Two Columns

    You are welcome.

    My method is using the "collection" object to do fast lookup.
    You can googling using keywords "excel vba collection object introduction" to know more about it.
    The first loop is to load the data (G5:H10) into this collection object.
    The second loop then checking each queries (C6:E8) against the collection, whether they are exists or not.

    Regards

  6. #6
    Registered User
    Join Date
    01-25-2017
    Location
    United States
    MS-Off Ver
    2010
    Posts
    13

    Re: Mark Cell Based on Two Columns

    I'm looking into collections now through a few guides, but I have a couple questions.
    I've added some annotations based off what I can identify and what I am curious about.

    Please Login or Register  to view this content.
    Last edited by bobredford; 03-17-2017 at 02:47 PM.

  7. #7
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Mark Cell Based on Two Columns

    'Adding what is in column 1 and 2 from a's region. What is "Chr$(2)" and why is Item empty?
    >> What is "Chr$(2)"
    When we contatenate two string, we need to add a delimiter character(s), to make the contatenated string keep unique.
    For example :
    ROBIN SONDAKH
    ROBINSON DAKH
    These are two different people, if we just contatenate them, both will be : ROBINSONDAKH, so how can we spot them ?
    By adding a delimiter character, for example if the delimiter character is "#", then they become :
    ROBIN#SONDAKH
    ROBINSON#DAKH
    Now, after they are concatenated, we can still spot them.
    The delimiter character, should be something that is uncommon used in daily words.
    For example, if you choose "@" for the delimiter character, they become ROBIN@SONDAKH and ROBINSON@DAKH, it is very like an email address.
    How if there is/are other item(s) that also use this "@" character in the words ? This become a problem again.
    So for the safest method, we need to use an almost impossible used character (as delimiter) for this purpose.
    A "normal" characters used in daily words, is start from Chr$(32) (a space) and higher, so numbers before 32 are considered safe.
    Then why choose 2 ? Well, I'm pretty new joined in this forum, and my senior here is used to be use that character, so I just follow

    >> why is Item empty
    Because we only need to know whether the key is exists or not.
    For example, we create a collection for company, we add :
    coll.Add Key:="Manager", Item:="John"
    coll.Add Key:="Supervisor", Item:="Mike"
    When we need to know the "associated item" with words "Manager", we look in the item property, so "John" is the result.
    But how if we only need to know, for example, if the "Manager" (the key) is already added to collection (exists) ? In this case we don't need to know the "associated item".
    So if from the beginning we already knew that we need the "key" part only (we don't need the "associated item"), we can add "Empty" as the item, like :
    coll.Add Key:="Manager", Item:=Empty
    coll.Add Key:="Supervisor", Item:=Empty
    This way, we can save the memory (RAM) from unnecessary things.



    'Admittedly lost here...
    Lets look at this sub :
    Please Login or Register  to view this content.
    If you run the above sub, the program is successfully print out the c("One"), but is failed for c("Three"), because this key is not added yet (not exists).
    So the "collection way" to check whether a key is exists or not, is by wrapping inside "On Error Resume Next ... On Error Goto 0" code block, then examine whether an error is occured.
    If no error occoured ("Err.Number = 0" means no error) then the searched key is exists, if an error is occoured (Err.Number = 5) this means the search key is not exists.
    Please Login or Register  to view this content.
    so your comment :
    'If theres an error above; fill in with "X"
    should be :
    'If no error occurred (Err.Number = 0, which means the searched key is exists), fill in with "X"



    'What does this do?
    It is the way to write back an array to Excel sheet.
    For example if we have 3x2 array size like this
    Please Login or Register  to view this content.
    If we try to put the array to Excel sheet like above on cell M1, only the topleft array is put there ("A").
    We need to resize the range as the same size as the array, but using .Resize method, like this :
    Please Login or Register  to view this content.
    Last edited by karedog; 03-17-2017 at 10:13 PM.

+ 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. Mark Specific Columns
    By stevie875 in forum Excel General
    Replies: 1
    Last Post: 04-13-2016, 05:27 PM
  2. [SOLVED] comparing columns and mark difference
    By Armand0 in forum Excel General
    Replies: 11
    Last Post: 10-30-2013, 08:53 AM
  3. Replies: 2
    Last Post: 10-28-2013, 03:52 PM
  4. [SOLVED] How to put a qutation mark (") automatically based on data in other cell
    By Khanzaki in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 11-15-2012, 02:58 AM
  5. How to mark cells based on date in other cell using conditional formatting
    By mharsvik in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 11-01-2012, 12:33 PM
  6. Compare 2 columns and mark the duplicates
    By powpowninjastar in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-12-2010, 10:06 PM
  7. How to mark duplicates cell with 2 columns ?
    By yannipr in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-23-2008, 07:59 AM

Tags for this Thread

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