+ Reply to Thread
Results 1 to 10 of 10

Finding duplicate rows based on values in multiple columns

  1. #1
    Registered User
    Join Date
    03-11-2014
    Location
    Coventry
    MS-Off Ver
    Excel 2010
    Posts
    24

    Finding duplicate rows based on values in multiple columns

    Hi

    I have a data set which has 6 columns (and lots of rows). Every row is different but I want to aggregate them based on 4 fields and then find the average of the numerical column for the results. Sorry if this is unclear but if you are familiar with Access I basically want to Group based on 4 fields and find the average of the 5th field.

    My initial approach was to introduce a column which is a combination of the 4 fields I want to group by, simply in the Excel file (=A2&B2&C2&D2) and then find duplicates of that. I have a solution for this in VBA but when importing new data sets in this method is very slow, so I want to be able to do the whole thing in VBA.

    The code which I have so far is:

    Please Login or Register  to view this content.


    The first part finds the unique values (of the column which is a concatenation of the 4 columns) and pastes them into column N. The 2nd part then pastes the relevant averages next to them.

    All I need to change is to search for unique combinations of the 4 cells, and then make the same change to the 2nd part. I am a novice with VBA, so apologies in this is not too clear.

    Any help would be great.

    Thanks

  2. #2
    Registered User
    Join Date
    03-28-2014
    Location
    Hertford, England
    MS-Off Ver
    Excel 2010,2013
    Posts
    38

    Re: Finding duplicate rows based on values in multiple columns

    Hi,

    Have you used the remove duplicate function?

    It can be coded, a little like this?

    Please Login or Register  to view this content.
    or its is on the ribbon: Data > Data Tools

  3. #3
    Valued Forum Contributor
    Join Date
    03-21-2013
    Location
    cyberia
    MS-Off Ver
    Excel 2007
    Posts
    457

    Re: Finding duplicate rows based on values in multiple columns

    Quote Originally Posted by foundy View Post
    Hi,

    Have you used the remove duplicate function?

    It can be coded, a little like this? ...
    Hmm.
    Just to check how well the RemoveDuplicates (vba or ribbon) works, manually type the following 11 data values in column A of a worksheet, and then remove the duplicates

    3c
    3
    6
    1c
    10
    1
    4c
    5
    4c
    10
    1c

    Do you really think the method is useful?

  4. #4
    Registered User
    Join Date
    03-28-2014
    Location
    Hertford, England
    MS-Off Ver
    Excel 2010,2013
    Posts
    38

    Re: Finding duplicate rows based on values in multiple columns

    Bit messy but maybe this can help?

    Please Login or Register  to view this content.
    Not sure how many rows you have, so can't comment on speed.

  5. #5
    Registered User
    Join Date
    03-11-2014
    Location
    Coventry
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: Finding duplicate rows based on values in multiple columns

    Thanks for the replies but I'm still stuck.

    Foundy I'm not really sure what's going on in the code you posted (sorry I am not good at this...).

    Is there no way that the method I originally posted can simply be adapted so that rather than searching for rows with 1 column the same, it searches for rows with 4 (specified) columns the same?

  6. #6
    Valued Forum Contributor
    Join Date
    03-22-2013
    Location
    Australia,NSW, Wirrimbi
    MS-Off Ver
    Excel 2013
    Posts
    1,057

    Re: Finding duplicate rows based on values in multiple columns

    Hi..

    Can you attach a sample workbook..

  7. #7
    Registered User
    Join Date
    03-11-2014
    Location
    Coventry
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: Finding duplicate rows based on values in multiple columns

    Sample Workbook.xlsm

    Here is a sample file. The real file has around 3 times as many rows.

    The current VBA is in the file. It is something I found online which works but most likely appears messy. It creates a new table showing the keys and their average prices.

    In the file column G is a key, made up by B,C,D & F. The VBA finds the average price (across all locations) for every row with the same key (column G), i.e. it finds a national average for each combination of B, C, D & F.

    Once I have the averages I cannot just show them by the key, I need to show them next to the individual columns which make up the key. As you would see if you used a pivot table (or Access query) to calculate these average prices. Using this current method, with the key, the only way I could think to do this was to use vlookups to align the keys with the individual rows (using another key column to match to). This makes the file run very slow so I need to be able to do it all in VBA - finding the averages by matching by combinations of the four columns and then probably creating a new table with 5 columns - for the 4 matched columns and the resulting average price.

    I cannot use pivot tables (to sort and copy-paste values etc), as this will make the file too slow.

    Apologies for the essay...

    Any help would be great

  8. #8
    Valued Forum Contributor
    Join Date
    03-22-2013
    Location
    Australia,NSW, Wirrimbi
    MS-Off Ver
    Excel 2013
    Posts
    1,057

    Re: Finding duplicate rows based on values in multiple columns

    Hi..
    Once I have the averages I cannot just show them by the key, I need to show them next to the individual columns which make up the key
    I am assuming you mean rows (not columns) there.. as in.. you want the average shown in every row of your main data for each Unique key(B,C,D,F concatenated).

    This does that.. i did it in about 20 seconds for your 8737 rows.. not the quickest(have a stretch while its running).. but does the job..

    No need for your existing 3 Subs I think..

    Workbook attached to show it working..

    Please Login or Register  to view this content.
    Attached Files Attached Files

  9. #9
    Registered User
    Join Date
    03-11-2014
    Location
    Coventry
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: Finding duplicate rows based on values in multiple columns

    Apo,

    Thanks for your help, it wasn't exactly what I needed but I only had to add a bit to get there. I needed it to remove the duplicates (of the 4 column combinations).

    I've attached another workbook showing the result. I added the part to remove duplicates after using your method to calculate the averages. I've also added a couple of subs to concatenate columns, rather than having that done in the worksheet, as it will be more efficient going forward. It's probably not the most efficient code but it works quite quickly on my computer so it's all good!

    Thanks again
    Attached Files Attached Files

  10. #10
    Valued Forum Contributor
    Join Date
    03-22-2013
    Location
    Australia,NSW, Wirrimbi
    MS-Off Ver
    Excel 2013
    Posts
    1,057

    Re: Finding duplicate rows based on values in multiple columns

    Hi..

    Ahh.. I see what you wanted now..

    On another note.. having a quick look.. change your ConcatColumns Sub..

    From this:
    Please Login or Register  to view this content.
    To this:
    Please Login or Register  to view this content.
    Also.. change your Concat2 Sub similarly..

+ 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. [SOLVED] Finding duplicate values in rows
    By fitkhan in forum Excel General
    Replies: 2
    Last Post: 03-24-2014, 04:53 AM
  2. Replies: 1
    Last Post: 03-28-2013, 01:48 PM
  3. Finding Duplicate Values in Multiple Columns
    By krissyboy78 in forum Excel General
    Replies: 3
    Last Post: 01-04-2013, 09:36 AM
  4. Replies: 3
    Last Post: 06-27-2012, 10:12 PM
  5. VBA code to find duplicate values within multiple rows and columns
    By pcbpinoy in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-27-2011, 09:04 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