+ Reply to Thread
Results 1 to 8 of 8

Compare two tables, find matches, and extract rows based on matches.

  1. #1
    Registered User
    Join Date
    12-19-2019
    Location
    Sweden
    MS-Off Ver
    2016
    Posts
    5

    Compare two tables, find matches, and extract rows based on matches.

    So, I have two large tables of data, with first columns in both being names of schools (I am trying to do some analysis of school data).
    Unfortunately, the two tables do not have the same number of rows, since some schools haven't reported data for table 1 and som haven't reported for table 2.

    So, I need to find the schools that appear in both tables, and extract the entire rows of these schools (or remove non-matches, either way works).

    Example (not all columns are included)

    Table 1
    School name | Municipality | Public/Private | Grade average
    A
    B
    D
    E
    G

    Table 2
    School name | Municipality | Public/Private | Grade average
    A
    B
    C
    E
    F

    So, in this case, only schools A, B, and E should be kept from each table.

    How can I achieve this?

    Please tell me if something is unclear and I will try to clarify further.

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,888

    Re: Compare two tables, find matches, and extract rows based on matches.

    Fast answers need visual help. Please read the yellow banner at the top of this page on how to attach a file.
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  3. #3
    Registered User
    Join Date
    12-19-2019
    Location
    Sweden
    MS-Off Ver
    2016
    Posts
    5

    Re: Compare two tables, find matches, and extract rows based on matches.

    Attached is a part of the two tables. In fact, all I need help with is to find which school codes are the same in both tables, then extract the entire rows from both tables and construct two new ones.

    Note: school names, codes, grades, etc are public information in Sweden, so none of it is confidential.
    Attached Files Attached Files

  4. #4
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,888

    Re: Compare two tables, find matches, and extract rows based on matches.

    If you are willing to work with Power Query then the following table can be achieved

    Excel 2016 (Windows) 32 bit
    A
    B
    C
    D
    1
    Skol-enhetskod Skolkommun Table2.Skol-enhetskod Table2.Skolkommun
    2
    76026322 Kiruna 76026322 Kiruna
    3
    44673074 Stockholm 44673074 Stockholm
    4
    99648792 Stockholm 99648792 Stockholm
    5
    22335099 Knivsta 22335099 Knivsta
    6
    47684564 Örebro 47684564 Örebro
    7
    15561397 Svedala 15561397 Svedala
    8
    71387206 Ale 71387206 Ale
    9
    41408780 Nacka 41408780 Nacka
    10
    10715898 Österåker 10715898 Österåker
    11
    37796564 Stockholm
    12
    48184058 Vallentuna 48184058 Vallentuna
    13
    28145722 Vallentuna 28145722 Vallentuna
    14
    59092675 Timrå 59092675 Timrå
    15
    90711097 Stockholm 90711097 Stockholm
    16
    84230115 Hultsfred 84230115 Hultsfred
    17
    27323097 Solna 27323097 Solna
    18
    85416841 Jönköping 85416841 Jönköping
    19
    92304123 Ovanåker 92304123 Ovanåker
    20
    65024773 Bollnäs 65024773 Bollnäs
    21
    82838118 Stockholm 82838118 Stockholm
    22
    60217642 Skurup 60217642 Skurup
    23
    45270097 Vara 45270097 Vara
    24
    56528690 Åtvidaberg 56528690 Åtvidaberg
    25
    51403955 Lerum
    Sheet: Merge1

    Power Query is a free AddIn for Excel 2010 and 2013, and is built-in functionality from Excel 2016 onwards (where it is referred to as "Get & Transform Data").

    It is a powerful yet simple way of getting, changing and using data from a broad variety of sources, creating steps which may be easily repeated and refreshed. I strongly recommend learning how to use Power Query - it's among the most powerful functionalities of Excel.

    - Follow this link to learn how to install Power Query in Excel 2010 / 2013.

    - Follow this link for an introduction to Power Query functionality.

    - Follow this link for a video which demonstrates how to use Power Query code provided.

    Specifically, I loaded each table into the PQ editor and the did a merge(Full outer Join) to get the results.

  5. #5
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,888

    Re: Compare two tables, find matches, and extract rows based on matches.

    Here is the file for you to review.
    Attached Files Attached Files

  6. #6
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,587

    Re: Compare two tables, find matches, and extract rows based on matches.

    Pl see file.
    In G3, H3, I3 copied down

    =IFERROR(INDEX(D$3:D$25,AGGREGATE(15,6,ROW($D$3:$D$25)/(ISNUMBER(MATCH($D$3:$D$25,$A$3:$A$25,0))),ROWS($G$3:$G3))-ROW($D$2)),"")

    =IF($G3="","",INDEX($B$3:$B$25,MATCH($G3,$A$3:$A$25,0)))

    =IF($G3="","",INDEX($E$3:$E$25,MATCH($G3,$D$3:$D$25,0)))
    Attached Files Attached Files
    Pl note
    Array formula should be confirmed with Ctrl+Shift+Enter keys together.
    If answere is satisfactory press * to add reputation.

  7. #7
    Valued Forum Contributor janmorris's Avatar
    Join Date
    07-24-2021
    Location
    Japan
    MS-Off Ver
    Google Sheets (& Mac 2021)
    Posts
    1,066

    Re: Compare two tables, find matches, and extract rows based on matches.

    create New Table 1, by putting this formula in cell G3, then drag across to F3, then drag G3:F3 down for as many rows as you like:

    Please Login or Register  to view this content.
    create New Table 2, by putting this formula in cell J3, then drag across to K3, then drag J3:K3 down for as many rows as you like:

    Please Login or Register  to view this content.
    bonus conditional formatting...

    green table 1 range $A$3:$B$25
    Please Login or Register  to view this content.
    red table 1 range $A$3:$B$25
    Please Login or Register  to view this content.
    green table 2 range $D$3:$E$25
    Please Login or Register  to view this content.
    red table 2 range $D$3:$E$25
    Please Login or Register  to view this content.
    sample file attached
    Attached Files Attached Files
    Last edited by janmorris; 01-20-2022 at 03:52 PM.

  8. #8
    Registered User
    Join Date
    12-19-2019
    Location
    Sweden
    MS-Off Ver
    2016
    Posts
    5

    Re: Compare two tables, find matches, and extract rows based on matches.

    Cheers! I'll give it a go!

+ 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] Extract multiple matches into separate rows, but with tables
    By MCdyna in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 11-17-2021, 05:14 AM
  2. Compare two tables and find records without matches
    By Isaacliu in forum Access Tables & Databases
    Replies: 1
    Last Post: 11-14-2019, 11:01 PM
  3. Replies: 1
    Last Post: 07-07-2018, 02:52 AM
  4. Macro to find matches and extract rows to into a new worksheet
    By Fi-Fi01666 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-12-2015, 06:24 PM
  5. Replies: 4
    Last Post: 05-10-2015, 02:11 PM
  6. Replies: 1
    Last Post: 09-26-2013, 12:16 PM
  7. Compare Sheet 1 and Sheet 2, and find matches and non-matches
    By danNAD in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-01-2010, 03:59 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