+ Reply to Thread
Results 1 to 21 of 21

Formula to Compare 2 columns and Manipulate the Resulting Data and Output

  1. #1
    Registered User
    Join Date
    03-08-2004
    Posts
    50

    Formula to Compare 2 columns and Manipulate the Resulting Data and Output

    Hi I am very new to excel and had two questions I am hoping you guys will be able to help. I'm sure this is probably a simple question, but it seems very complicated to me. heh

    Say I have column A and column B:

    I will be inputting data into column B.

    I need excel to check to see if the data I input into column B is an exact match to the data in column A.

    If it is an exact match, then column B will remain blank.

    If the data in column B is different, I need column B to show the following:

    No match: <data>


    Example I input in column B the following:

    Column A Column B
    1. Car Car
    2. 4357 9999
    3. fsd34d 4erd
    4. 98dkf 98dkf

    Spreadsheet should show:
    Column A Column B
    1. Car
    2. 4357 No match: 9999
    3. fsd34d No match: 4erd
    4. 98dkf

    (Cell 1 and 4 in column B are empty because they are exact matches to Column A cell 1 and 4)

    My questions:
    1) How does the excel formula need to be written for this to work?

    2) Is there a way to set it so that when I do a mass copy to data into column B that the formula will not be overwritten and it will still check to see if the data I copy and pasted into that column matches the data next to it in column A?


    Greatly appreciate your help everyone!!

  2. #2
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,885
    You can't do what you're looking for without either a helper column or a vba macro.

    If you have formulas going down column B, such as:

    =IF(B1=A1,"","No match: "&B1)

    and you type or paste something into B1, it will overwrite that formula.

    Your options are to paste your data into B1 and in C1 use a formula such as:

    =IF(B1=A1,"","No match: "&B1)

    You could then hide column B if you prefer.

    Or, write a vba macro, which wouldn't require you to use a helper column (the macro could parse through the column and update the cells as necessary).

  3. #3
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,885
    A macro like this should handle it if you don't want to use a helper column:
    Please Login or Register  to view this content.
    As always, before running any macro on your data, make a backup copy of your file or use test data.

  4. #4
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    You might want to put that routine in the sheet's Worksheet_Change event.

  5. #5
    Registered User
    Join Date
    03-08-2004
    Posts
    50
    Thank you guys very much for your replies!

    I'm not exactly sure how the macro thing works. I'm using excel 2007 and can't
    seem to find the Worksheet_Change event to put the routine in.

    Also after I put it in this place, it will just automatically run each time I paste my data into column B? I do not have to initialize or start it up in any way?

  6. #6
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    To find the worksheet_Change event, go to the VB Editor, View the Project Explorer, open the Microsoft Excel Objects folder and double click the sheet you want. When the new window opens, first select Worksheet from the drop down menu on the upper left. Then select Change from the upper right dropdown.
    Then insert pjoaquin's routine (less the "Sub FindMatches.." and "End Sub" lines).
    It should take effect immediatly and automaticaly.

  7. #7
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,885
    Actually, you don't want to put this into a Worksheet_Change or SelectionChange event, otherwise it will continuously update and you'll end up with huge strings in column B (e.g. No Match: 77 No Match: 77 No Match: 77....)

    Keep the code below as a separate function (I modified it to check Column A for the bottom row of text rather than column B so it checks every cell in column B that has a value in Column A.)
    Please Login or Register  to view this content.
    To insert this code in Excel 2007, use either the Developer ribbon bar or simply press ALT+F11 to open the Visual Basic editor.
    Next, expand the 'VBA Projects (yourworkbookname)' tree until you see 'Sheet1', 'Sheet2', 'ThisWorkbook'.
    Next, double-click on the sheet you want the code to run on and paste this code into the whitespace on the right.
    Finally, close the VBA window to return to Excel.

    Your options for running the macro are:
    1) Manually through the Macros window (View or Developer ribbon bars, or press ALT+F8)

    2) This option looks longer, but after the initial setup it will save you time in the future... Attach the macro to a command button on your sheet to run it at the click of a button. On the Developer ribbon bar, in the Controls section, click the Insert button and select the Command Button icon (should be first one under Form controls). Draw a rectangle on the sheet where you want the button. Once you do, you should be prompted with the 'Assign Macro' window. Select your 'FindMatches' macro and click OK. You can then click on the button and change the text shown to something other than "Button 1". Turn off Design Mode by clicking the button in the Developer-Controls ribbon bar and you're all set in the future to type or paste data into column B then click your button.
    Last edited by Paul; 03-31-2007 at 05:53 PM.

  8. #8
    Registered User
    Join Date
    03-08-2004
    Posts
    50
    Thank you pjoaquin, it worked quite well!

    I have been testing it out and I encountered a couple of problems.

    1) If column A has data , but column B does not have any data, and I click the button, it will fill column B with "No Match:"

    Can you adjust it so that if Column A has data and B cell is empty, it will leave B empty?


    2) In the spreadsheet (provided case 1 above is not an issue) it works fine, however, when I enter new data and re-click the macro button, a problem occurs.

    Each time I click the button, and additional "No Match:" is added to the B column. Thus, having a long string of "No Match:" "No Match:"

    Can you adjust the script so that it will not check/re-check the B cells which have already been processed? i.e. if "No match:" is already part of the cell, it will not re-check, thus causing it to add additional "No Match:" to the previous processed cells?

    Thanks a TON for your help!!

  9. #9
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,885
    Hi Luu... this modified code should fix both issues..
    Please Login or Register  to view this content.
    Let me know how it goes.
    Last edited by Paul; 03-31-2007 at 10:08 PM.

  10. #10
    Registered User
    Join Date
    03-08-2004
    Posts
    50
    Hi pjoaquin,

    Awesome! That modification worked perfectly for all changes to column B!

    Ugh! I didn't test it thoroughly enough the first time. I noticed an additional possibility which causes an issue.

    If I change the data in column A, the spreadsheet will add additional "No Match:" "No Match:", etc. to all the rows in column B.

    Can you make an additional modification so that if the data in A is changed, that row for B will be re-scanned and modified without repeating "No Matches:" and without affecting the other cells and rows in the columns?

    Thank you soo much!!!
    Last edited by luu980; 04-01-2007 at 07:02 AM.

  11. #11
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,885
    That's odd, since I don't get that result at all. If I change the data in column A it doesn't update column B, but it also doesn't add "No match:" to any of the cells in B.

    For example, if A1 = "abc" and B1 = "acb", upon running the macro I get "No match: acb" in B1. I can run it ten times and nothing will change. Now when I change A1 to "acb" and run the macro, it still doesn't update B1 to show blank.

    I'll look into it a bit and see what I can find out.

  12. #12
    Registered User
    Join Date
    03-08-2004
    Posts
    50
    Hi again,

    Sorry, I was unclear the first time. I tried to make my example as simple as possible in my original post and as a result, I had to change the coding a little bit to fit what I needed.

    In column B, I wanted it to have the output: (s: <text>)

    What I changed to the code was the following below and it somehow messed up the process. I think it has to do with the end parentheses ')'. I'm not sure how to fix it to make it work properly.


    Please Login or Register  to view this content.

  13. #13
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,885
    Please Login or Register  to view this content.
    The problem isn't the parentheses, it's the length you're checking against. In my original formula it would add "No match: " to the left of the value, so I checked the first 8 characters for "No match". Now that you're just using "(s: " to the left of your value, change the 8 to 4, e.g.

    Please Login or Register  to view this content.

  14. #14
    Registered User
    Join Date
    03-08-2004
    Posts
    50
    Hi,

    Appologies for the late reply. I just got back from a short business trip. Thank you for the help pjoaquin.

    The current code I have is as follows:

    Please Login or Register  to view this content.

    It appears to do everything as you had mentioned! Cool!

    I had one other inquiry.

    If A is aaa and B is bbb, then B becomes (s: bbb), if I change A, B remains the same and does not repeat, which is great! The only time it may be a problem is if A is changed to bbb, the B column does not disappear.

    Can you make a modification so that column B will update if column A becomes the same as column B? Thank you soooo much pjoaquin!!!!

    Oh, I was wondering, with the current routine, can I put it in the sheet's Worksheet_Change event? Or would that still pose as a problem?

  15. #15
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,885
    Give this code a try:
    Please Login or Register  to view this content.
    I added or changed the items in red font (if you just copy and paste the entire code over your current code that will suffice).

    I put it into the Worksheet_Change event, but had to add Application.EnableEvents = FALSE & Application.EnableEvents = TRUE to avoid an infinite loop of changes. I also added the third test to the first section of the If routine, to check if the value in column A has been changed to the value in column B with the "(s: " and ")" wrapped around it.

    Let me know how it goes..

  16. #16
    Registered User
    Join Date
    03-08-2004
    Posts
    50
    It appears I am doing something terribly wrong.

    1) I insert the new code in Excel 2007
    2) ALT+F11 to open the Visual Basic editor.
    3) expanded the 'VBA Projects (yourworkbookname)' tree
    4) double-clicked on the sheet for the code to run on (sheet1) and paste thecode into the whitespace on the right
    5) Made sure the sheet1(code) menu was 'worksheet' and 'change' in the upper options tabs
    5) I close the VBA window to returned to Excel

    However, it didn't appear anything happened when I typed in values for A and B columns. (Just to clarify, setting it as Worksheet_Change event allows excel to update the B column automatically as I enter in the data right?)

    I then removed the "Private Sub Worksheet_Change(ByVal Target As Range)" and " Application.EnableEvents = True" and then clicking alt-f8. In which the new code appeared to work great. (I haven't tested it in detail yet).

    What am I missing here to make it a Worksheet_Change event?

  17. #17
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,885
    Did you completely delete the old code that the most recent code was meant to replace? I have a feeling there are still references to 'Sub FindMatches()' and 'EndSub' wrapped around the latest code. If you want, go into your Sheet1 code and select EVERYTHING, then paste it back into the forum and we can check if you're not sure.

    In the end, you shouldn't have any code except the code I provided last. The entire bit of code from 'Sub FindMatches()' to 'EndSub' in the original code should be deleted.

    (Once the old code has been deleted, it should also no longer appear in the ALT+F8 window.)

  18. #18
    Registered User
    Join Date
    03-08-2004
    Posts
    50
    Hi,

    Yes, I had deleted the entire old code. When I hit alt-f8, nothing appears. However, when I change the data in column A and B, it does not appear to update. It's supposed to update in real time, is that corrrect?

  19. #19
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,885
    Yes, it is supposed to update in real-time, as it does on my computer. You mentioned something about removing the 'EnableEvents' code. That could be a problem if you already ran Application.EnableEvents = False, but never ran Application.EnableEvents = True afterward. Excel would be sitting in a state where it cannot perform worksheet events.

    For the heck of it, copy and paste this code into your vba window and run it (either from the vba window or through the macro list ALT+F8):
    Please Login or Register  to view this content.
    Then try re-running the latest code provided and let me know how it goes. If it still doesn't work right, you may need to post a zipped copy of your file so we can diagnose the problem.

  20. #20
    Registered User
    Join Date
    03-08-2004
    Posts
    50
    Cool, that worked!

    pjoaquin, can you make one last modification for me?

    If A column later is deleted or changed (regardless of what it is changed to), can you make column B blank out for me?

    The reason is if A ends up being changed 95% of the time I will end up having to re-enter the column B data by hand. *sigh*


    Please Login or Register  to view this content.

    I figured out that to empty out B if A is deleted, I add the line (in red), but I couldn't figure out the second part, if A data ends up being changed to any other type of different data.

  21. #21
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,885
    The only way I can think to do this is if you stored the contents of column A either in another column (hidden, perhaps) or in an array variable in the macro. The next time the code ran (on any worksheet_change) it could check to see if column A had changed, and update that cell in column B accordingly.

    I think an array would be cleaner, however I'm not familiar with coding array's and would ask some of the other folks here to assist if possible. Perhaps I'm reading too much into it, so another pair of eyes might be good anyway. Any takers out there?

    I'll see what I can come up with, but hope another coder can assist in the mean-time.

    For clarification, we're already checking to see
    If cell Ax is blank, make cell Bx blank OR
    If cell Bx is blank, make (leave) cell Bx blank OR
    If cell Bx = cell Ax, make cell Bx blank OR
    If cell Bx = "(s: " + Ax + ")", make cell Bx blank
    Else
    If cell Bx <> Ax AND Left(Bx),4 <> "(s: ", make cell Bx "(s: " + Bx + ")"

    And now you're trying to add code that will check to see if the contents of cell Ax have changed, and if so, make Bx blank.

    As an example, if A2 = "abc", and you enter "abc" into B2, B2 will turn blank upon pressing Enter.
    If you had entered "bac" into cell B2, it would turn into (s: bac).

    The current code will blank out cell B2 if you were to change A2 to "bac", but you're looking to blank it out ANY TIME you change the value in A2 (Ax)? It seems easy enough, but my mind just wonders why. (It does that a lot, don't be offended.)


+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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