I am looking for guidance on how to build a VBA Macro in Excel (I have never created one before) that would look for a unique identifier match in two separate excel files and then would extract the accompanying row data for the match.
Put more plainly:
I have two separate excel files, they each have a column present in them for a unique identifier.
I want the VBA Macro to find the matches in one of the files where the unique identifier is the same as it is in the other file.
Once a match is found in the Excel file, I want to extract the data for that specific row where the match is found.
Ideally, I would like the extracted data to be put into a new excel worksheet.
This is an example that will lead you to doing what you want to do. Here're the steps you will have to take or think about:
Enable developer tools in Excel
Use this article: https://msdn.microsoft.com/en-us/library/bb608625.aspx
Open the first file. Then create a module as the next topic shows
Create module
Use this article and follow it till step 3 - create module: https://www.ablebits.com/office-addins-blog/2013/12/06/add-run-vba-macro-excel/
Create constants
Write the code below to declare constants of file names and sheet names.
Create code that will check if workbook is open
Write this code below the constant declaration code
In case where a file does not exist, create a new file and report that the file is not open. I just used the code by Dan Wagner in VBA macro to copy data from one excel file to another answer. Rest of the code is a modification of Dan's code also.
Create code that will open a book
Write this code next below your rest of the code. This code will take a file name and a reference variable. If workbook is not open, open it and assign it to reference variable. You will have to do some reading on
ByRef
yourself.Create code that will do looping and insert data in resultant file
Write this code at the bottom of your current code. This code will open all 3 books (First book, second book and resultant book in which data will be pasted). First file is read row by row (only 1st cell is read assuming that is the common field between first and second file). Then, file 1
Example
I created Book1.xlsx. In that I have:
Book2.xlsx has
When I click on any line of copycode and press
F5
, copycode subroutine will run. It will go through the code and then the resultant file will look like this:Note that data from file 1 went to file 3 but only those rows that had matching UID in file 2 did. Matt's row from file 1 did not go to resultant file because file 2 does not have UID 2.
Hopefully that will get you stated.