+ Reply to Thread
Results 1 to 11 of 11

Compare values in adjacent columns, replace if identical

  1. #1
    Registered User
    Join Date
    11-08-2012
    Location
    Copenhagen
    MS-Off Ver
    Excel 2007
    Posts
    56

    Compare values in adjacent columns, replace if identical

    Dear forum

    I am struggling with a problem in a massive set of data.
    It is a table with four columns and different number of rows. Each individual item consists of a varying number of rows.

    I need to compare two of the columns, and if they are identical, replace the content of the other column.

    So if columns are named A B C D. Column A has only one entry per item, and B C D all has several entries per item.

    So the codes has to compare A with B, and if A = B, then replace all the B's with A. But only until there is a change in B. Then it has to compare again.

    I hope it makes sense. I have attached a sample workbook illustrating what I am looking for.
    SampleWorkbook.xlsm

    I am thinkin I need two if statements, where one of them monitors when B changes, and the other doing the comparison?
    But I am not sharp enough with coding to do this..

    Hope you are able to help.

  2. #2
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Compare values in adjacent columns, replace if identical

    Hi dannyjoer,
    try it
    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    11-08-2012
    Location
    Copenhagen
    MS-Off Ver
    Excel 2007
    Posts
    56

    Re: Compare values in adjacent columns, replace if identical

    Hi Nilem

    That works perfectly, I must admit I don't know exactly why.
    Let's say their are many tables next to each other in a sheet. All with the same horizontal distance. Can I get the code to repeat it self at many tables in a sheet?

    Br/Danny

  4. #4
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Compare values in adjacent columns, replace if identical

    Hi Danny,
    can you show an example of your data with multiple tables

  5. #5
    Registered User
    Join Date
    11-08-2012
    Location
    Copenhagen
    MS-Off Ver
    Excel 2007
    Posts
    56

    Re: Compare values in adjacent columns, replace if identical

    Absolutely. Actually the new sample diverts a little bit from the first, (I discovered an error)

    The number in column A that is to be compared to column B, always has a number above.

    So this new sample workbook is a perfect example of my situation. I have marked the number in column A with Grey, the one to replace the number in column B, if they differ.

    SampleWorkbookRev1.xlsx

    Hope you understand my rather cryptic explanation.

  6. #6
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Compare values in adjacent columns, replace if identical

    try
    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    11-08-2012
    Location
    Copenhagen
    MS-Off Ver
    Excel 2007
    Posts
    56

    Re: Compare values in adjacent columns, replace if identical

    I must say that it is very impressing. It works perfectly.

    Could you tell a little bit about the code, and how it can be modified to fit other tables. I mean, what are the critical points?
    If I for instance wanted to add a "comment" column between each table. What do I have to change then.

    Br

  8. #8
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Compare values in adjacent columns, replace if identical

    If you add a column "comment" between each table, then modify this line
    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    11-08-2012
    Location
    Copenhagen
    MS-Off Ver
    Excel 2007
    Posts
    56

    Re: Compare values in adjacent columns, replace if identical

    Perfect,, just one final question..

    I am pretty sure that your code can be edited very easily to be able to handle this type of table..
    The important number is highligted yellow

    SampleWorkbookRev3.xlsx


    Thanks again.
    Last edited by dannyjoer; 02-13-2014 at 11:07 AM.

  10. #10
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Compare values in adjacent columns, replace if identical

    ok, try again
    Please Login or Register  to view this content.

  11. #11
    Registered User
    Join Date
    11-08-2012
    Location
    Copenhagen
    MS-Off Ver
    Excel 2007
    Posts
    56

    Re: Compare values in adjacent columns, replace if identical

    Hi again nilem

    I have come across a little issue with the otherwise extremely effective code you have provided me with.

    The code works smoothly whenever the numbers in the currentregion keep changing.
    But if the number in the column where new values are to be inserted, the code overwrites it and put in the last number.

    Is there a workaround for that?
    I have included a sample workbook SampleWorkbookRev4.xlsm.
    It contains the code and an example of where it has gone "wrong"

    I would very much appreciate any help on this.

    Br/Danny

+ 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 Columns and Replace Values without overwriting
    By Girish Punjabi in forum Excel General
    Replies: 1
    Last Post: 03-14-2013, 12:43 PM
  2. Replies: 2
    Last Post: 02-28-2013, 04:45 PM
  3. Replies: 3
    Last Post: 02-15-2012, 01:18 AM
  4. Compare columns and replace values?
    By llckll in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 06-30-2010, 02:51 PM
  5. Compare values in two columns and return text from adjacent cell
    By MrBorders in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 07-04-2009, 04:54 AM

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