+ Reply to Thread
Results 1 to 4 of 4

Comparing columns in two different excel sheets and if they match, copy a third column

  1. #1
    Registered User
    Join Date
    09-12-2018
    Location
    Birmingham, England
    MS-Off Ver
    1808
    Posts
    2

    Comparing columns in two different excel sheets and if they match, copy a third column

    I need to upload a spreadsheet containing the details of a substantial number of Year 8 pupils, onto a course in the school VLE (Moodle). The fields I will use for the upload are Col A = firstname, Col B = lastname, Col C = username and so on. I have obtained fresh data for Cols A and B from the school MIS, but the MIS it does not contain Moodle usernames, course names etc.

    Fortunately, I was able to obtain (ie export) a spreadsheet from Moodle, which contains last year's data (Year 7) for the same group (who are now Y8). It had columns as follows: Col A = firstname, Col B = lastname, Col C = username etc. Pupil's names for my Y8 group are largely the same as last year (when they were in Year 7), although some pupils have been added and some taken away between July 2018 and Sept 2018. Hence Col A and B on S1 and S2 do not match up.

    If I call last year's (2017-2018) spreadsheet S1, and this year's (2018-2019) spreadsheet S2. I need a formula which will read Col A and Col B in S2 (the new sheet), and compare the names with the same columns in S1 (last years sheet), and where there is a match (ie names are the same), pull the usernames from S1 and place them in the appropriate row in S2.

    This will populate around 85% of S2 with usernames and I can do the rest manually. I cannot simply copy the usernames across from S1 to S2 as the Cols A and B on the two sheets do not match. Please can anyone suggest a way I might do this? The alternative involves a lot of copying and pasting!

  2. #2
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: Comparing columns in two different excel sheets and if they match, copy a third column

    See if this will work for you

    Please Login or Register  to view this content.
    You will need to substitute the correct sheet names where comments say 'Edit sheet name
    Any code provided by me should be tested on a copy or a mock up of your original data before applying it to the original. Some events in VBA cannot be reversed with the undo facility in Excel. If your original post is satisfied, please mark the thread as "Solved". To upload a file, see the banner at top of this page.
    Just when I think I am smart, I learn something new!

  3. #3
    Registered User
    Join Date
    09-12-2018
    Location
    Birmingham, England
    MS-Off Ver
    1808
    Posts
    2

    Re: Comparing columns in two different excel sheets and if they match, copy a third column

    Thank you so much for your reply.
    I ran the script and named the sheets 'LastYear' and 'ThisYear' as you did.
    I keep getting "runtime error 9: Subscript out of range".
    Debug highlights the line "Set s1 = Sheets("LastYear")".
    I ran the script from worksheet S2.
    Would be grateful for your guidance
    Thanks again

  4. #4
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: Comparing columns in two different excel sheets and if they match, copy a third column

    The code should be installed in a public code module (module1, module2, etc.). If you press Alt + F11 it will open the vb editor and in the small Project pane at upper left you will see the Objects currently available such as ThisWorkbook, Sheet1, Sheet2, etc. If 'module1' does not appear there, then click 'Insert' on the editor tool menu bar, then click 'Module' in the drop down menu. The code pane should brighten and you can paste the code into it. As for the sheet names, they must be exactly the same on the name tab as in the code, including upper/lower case letters and spaces. The quote marks used in the code should not be used in the sheet name tab. The quorte marks in the code are to identify the data as string type. Excel sees any character sent from the keyboard, whether we see it or not. So if you have spaces in the sheet tab but not in the code, or vice versa, Excell will see those spaces and consider it a mismatch, which will produce the error message.
    Last edited by JLGWhiz; 09-13-2018 at 09:38 AM.

+ 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] Comparing two columns and if there is a match then return values from a different column
    By skywalkertinks in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 05-26-2017, 07:18 AM
  2. Match 2 Columns in two sheets; copy an offset Column of matched Row to Sheet 1
    By sureng20 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 05-01-2017, 03:08 AM
  3. Replies: 1
    Last Post: 08-18-2014, 11:10 PM
  4. [SOLVED] Comparing 2 sheets in Excel and returning a value if there is a match
    By ava285 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 02-19-2013, 01:40 AM
  5. [SOLVED] Comparing Two Columns and Copy Adjacent Column IF Match Found
    By 10AVATAR in forum Excel - New Users/Basics
    Replies: 2
    Last Post: 09-29-2012, 04:33 PM
  6. Comparing 2 columns- if a match is found return a value in 3rd column
    By lineson in forum Excel - New Users/Basics
    Replies: 4
    Last Post: 12-20-2011, 04:48 PM
  7. comparing two different columns and copy field from 3rd column
    By bkl99 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 05-12-2011, 12:49 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