+ Reply to Thread
Results 1 to 9 of 9

VBA code to compare rows based on 4 column values and highlight is no match

  1. #1
    Registered User
    Join Date
    10-02-2015
    Location
    Netherlands
    MS-Off Ver
    2010
    Posts
    20

    VBA code to compare rows based on 4 column values and highlight is no match

    Hi,
    Im very new to VBA. I can make some easy changes to example VBA codes I found here on the forum which is a great resource, but am having a problem to find something for me to use (perhaps due to my still learning status) for the following problem:

    What I'm trying to do is:
    I have 4 columns, let's say A, B, E, and G
    Column A can have two text values, let's say 'ItemA' or 'ItemB'.
    Column B has multiple different text values (text1, text2, text3 etc. many different options)
    Column E and G both have either 'Yes' or 'No' text.

    I need to compare rows that have ItemA with rows that have ItemB in that:
    I want to check for each row that has ItemA where column E and G are both set to "No", if there is a row with ItemB where column E and G are also both set to 'No' and that has the same text in column B as for the row with ItemA. If there is no match, I want to highlight the row with ItemA that was checked in green. I want to do this for all variations of text in column B.
    Vice versa, I want to check for each row that has ItemB+text1+No+No if there is any row with Item A+same text1,No,No, and highlight rows with ItemB+text1+No+No if no match.

    I figure I may need some kind of nested loop? (for where column value E = 'no' and column value G = 'no', per each row with Item A, check if a row with ItemB + No + No has same value as the Item A Row). Or am I overcomplicating?

    P.s.
    - my worksheet has ~30000 rows of data. There are no empty cells or in the 4 columns I mentioned. there are no empty rows.
    - rows are not unique with respect to the 4 columns, so there can be duplicate rows (that need to remain).

    I have attached an example workbook in case it helps to explain what I would like.

    I hope I made sense and would appreciate if someone can point me in the right direction of how to set-up the VBA code or perhaps point me to a thread I should use.
    Thank you very much in advance!
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: VBA code to compare rows based on 4 column values and highlight is no match

    I think you are going to have a hard time converting this to work on your actual sheet. Good luck

    Please Login or Register  to view this content.
    If you are happy with my response please click the * in the lower left of my post.

  3. #3
    Forum Expert
    Join Date
    08-02-2013
    Location
    Québec
    MS-Off Ver
    Excel 2003, 2007, 2013
    Posts
    1,412

    Re: VBA code to compare rows based on 4 column values and highlight is no match

    Hi,
    Here's an other approach using classes.
    GC Excel

    If this post helps, then click the star icon (*) in the bottom left-hand corner of my post to Add reputation.

  4. #4
    Forum Expert
    Join Date
    08-02-2013
    Location
    Québec
    MS-Off Ver
    Excel 2003, 2007, 2013
    Posts
    1,412

    Re: VBA code to compare rows based on 4 column values and highlight is no match

    Hi,

    I've been working on "classes" recently so here's an other approach using a custom class.

    Run the code in Module1
    Attached Files Attached Files
    Last edited by GC Excel; 10-04-2015 at 09:56 AM.

  5. #5
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: VBA code to compare rows based on 4 column values and highlight is no match

    Hi Sirilias,

    Here's another routine to try:

    Please Login or Register  to view this content.
    And - Welcome to the Forum!
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  6. #6
    Registered User
    Join Date
    10-02-2015
    Location
    Netherlands
    MS-Off Ver
    2010
    Posts
    20

    Re: VBA code to compare rows based on 4 column values and highlight is no match

    Hi people,

    First off al, thank you, you guys are the bomb for helping out!
    stynkts: Had a go at your code, and it runs, but it runs a long long time. I guess since it makes a lot of comparissons for each cell. I may try this with some less data further.
    xladept: I've been tying to adapt your last code, since it obviously works in the example I provided (and I could get to work in that example) and I have a feeling I understand somewhat what you are doing here , but I get stuck on my actualy sheet.
    I think it has to do with my data being in different columns than the example or the fact that some cells actually contain formula's that create the text in them rather then the actual text?
    In any case: this is the error I get:
    "Run time error 91: Object variable or With block variable not set." Debugger stops at: G.Interior.Color = 5287936

    My actual data collums are:
    34/AH (itemA/ItemB)
    35/AI (text1, txt2, text3, text4 etc etc) <this is concatenation of other cells in my sheet>
    36/AJ and 37/AK: Yes or No <e.g. =IF(R2="";"No";"Yes")>

    So I've changed the column refferences in the code you so kindly provided to 35/36/37 (whereever you have 2/5/7) and put "Right(Cells(r, 34), 1)".
    Am I perhaps missing something obvious?

    Kind regards, and appreciate picking your brains!
    Sirilias

  7. #7
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: VBA code to compare rows based on 4 column values and highlight is no match

    Hi Sirilias,

    Attach a sample workbook. Make sure there is just enough data to demonstrate your need. Include a BEFORE sheet and an AFTER sheet in the workbook if needed to show the process you're trying to complete or automate. Make sure your desired results are shown, mock them up manually if necessary.

    Remember to desensitize the data.

    Click on GO ADVANCED and use the paperclip icon to open the upload window.

    View Pic

    And I'll have another "go" at it

    Meanwhile you can try this:

    Please Login or Register  to view this content.
    Last edited by xladept; 10-05-2015 at 02:43 AM.

  8. #8
    Registered User
    Join Date
    10-02-2015
    Location
    Netherlands
    MS-Off Ver
    2010
    Posts
    20

    Re: VBA code to compare rows based on 4 column values and highlight is no match

    Hi xladept,

    Just have to say: You rule! I fiddled around a bit with your code and realized I simply had to align my source with the exact parameters I used in my example (Item A/Item B) and if worked like a charm!
    So thank you very much for helping me out!
    All the best!
    Sirilias

  9. #9
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: VBA code to compare rows based on 4 column values and highlight is no match

    You're welcome - glad you got it working and thanks for the rep!

+ 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. Compare duplicate values in two columns and highlight if match
    By JEKF in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-18-2015, 12:32 PM
  2. [SOLVED] compare values in two cells, highlight when there is no match.
    By frhling in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 01-03-2013, 09:29 AM
  3. [SOLVED] VBA code to highlight rows based on a cell
    By mugs62 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-11-2012, 07:08 AM
  4. Replies: 3
    Last Post: 07-20-2012, 10:31 AM
  5. Replies: 3
    Last Post: 11-22-2011, 02:06 PM
  6. Replies: 7
    Last Post: 09-13-2011, 12:04 PM
  7. Formula to compare multiple rows values based on another column?
    By Murph in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 02-20-2005, 11:06 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