+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 15 of 21

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

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

    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 Moderator Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2007
    Posts
    5,953
    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 Moderator Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2007
    Posts
    5,953
    A macro like this should handle it if you don't want to use a helper column:
    Sub FindMatches()
    Dim Row As Integer, iLastRow As Integer
    
        iLastRow = Range("B65536").End(xlUp).Row
        
        For Row = 1 To iLastRow
                If Cells(Row, 2).Value <> Cells(Row, 1).Value Then
                    Cells(Row, 2) = "No match: " & Cells(Row, 2).Value
                Else
                    Cells(Row, 2) = ""
                End If
        Next Row
            
    End Sub
    As always, before running any macro on your data, make a backup copy of your file or use test data.

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

  5. #5
    Registered User
    Join Date
    03-08-2004
    Posts
    41
    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 Guru mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    2,998
    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 Moderator Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2007
    Posts
    5,953
    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.)
    Sub FindMatches()
        Dim Row As Integer, iLastRow As Integer
    
        iLastRow = Range("A65536").End(xlUp).Row
        
        For Row = 1 To iLastRow
                If Cells(Row, 2).Value <> Cells(Row, 1).Value Then
                    Cells(Row, 2) = "No match: " & Cells(Row, 2).Value
                Else
                    Cells(Row, 2) = ""
                End If
        Next Row
    End Sub
    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
    41
    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 Moderator Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2007
    Posts
    5,953
    Hi Luu... this modified code should fix both issues..
    Sub FindMatches()
        Dim Row As Integer, iLastRow As Integer
    
        iLastRow = Range("A65536").End(xlUp).Row
            
        For Row = 1 To iLastRow
            If Cells(Row, 2).Value = "" Or _
            Cells(Row, 2).Value = Cells(Row, 1).Value Then
                Cells(Row, 2).Value = ""
            ElseIf Cells(Row, 2).Value <> Cells(Row, 1).Value And _
            Left(Cells(Row, 2).Value, 8) <> "No match" Then
                Cells(Row, 2).Value = "No match: " & Cells(Row, 2).Value
            End If
        Next Row
    End Sub
    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
    41
    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 Moderator Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2007
    Posts
    5,953
    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
    41
    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.


    Sub FindMatches()
        Dim Row As Integer, iLastRow As Integer
    
        iLastRow = Range("A65536").End(xlUp).Row
            
        For Row = 1 To iLastRow
            If Cells(Row, 2).Value = "" Or _
            Cells(Row, 2).Value = Cells(Row, 1).Value Then
                Cells(Row, 2).Value = ""
            ElseIf Cells(Row, 2).Value <> Cells(Row, 1).Value And _
            Left(Cells(Row, 2).Value, 8) <> "(s: " Then
                Cells(Row, 2).Value = "(s: " & Cells(Row, 2).Value & ")"
            End If
        Next Row
    End Sub

  13. #13
    Forum Moderator Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2007
    Posts
    5,953
    Left(Cells(Row, 2).Value, 8) <> "(s: " Then
                Cells(Row, 2).Value = "(s: " & Cells(Row, 2).Value & ")"
    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.

    Left(Cells(Row, 2).Value, 4) <> "(s: " Then
         Cells(Row, 2).Value = "(s: " & Cells(Row, 2).Value & ")"

  14. #14
    Registered User
    Join Date
    03-08-2004
    Posts
    41
    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:

    Sub FindMatches()
        Dim Row As Integer, iLastRow As Integer
    
        iLastRow = Range("A65536").End(xlUp).Row
            
        For Row = 1 To iLastRow
            If Cells(Row, 2).Value = "" Or _
            Cells(Row, 2).Value = Cells(Row, 1).Value Then
                Cells(Row, 2).Value = ""
            ElseIf Cells(Row, 2).Value <> Cells(Row, 1).Value And _
            Left(Cells(Row, 2).Value, 4) <> "(s: " Then
                Cells(Row, 2).Value = "(s: " & Cells(Row, 2).Value & ")"
            End If
        Next Row
    End Sub

    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 Moderator Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2007
    Posts
    5,953
    Give this code a try:
    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim Row As Integer, iLastRow As Integer
    
        iLastRow = Range("A65536").End(xlUp).Row
                
        Application.EnableEvents = False
        
        For Row = 1 To iLastRow
            If Cells(Row, 2).Value = "" Or _
            Cells(Row, 2).Value = Cells(Row, 1).Value Or _
            Cells(Row, 2).Value = "(s: " & Cells(Row, 1).Value & ")" Then
                Cells(Row, 2).Value = ""
            ElseIf Cells(Row, 2).Value <> Cells(Row, 1).Value And _
            Left(Cells(Row, 2).Value, 4) <> "(s: " Then
                Cells(Row, 2).Value = "(s: " & Cells(Row, 2).Value & ")"
            End If
        Next Row
        
        Application.EnableEvents = True
    End Sub
    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..

+ 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.2.0