Thankfully Excel has a few "hidden" features that can take away some of the hassle and make the whole process a lot easier.
The SolutionThis solution is pretty straight forward and we have created a little example spreadsheet to demonstrate how it is done. You can download the example file containing both the form and the code here.
Open up the spreadsheet and launch the macro.
Enter the two ranges you want to be combined into the 'combine form'.
Note that the range edit boxes are special range boxes. When you select them, you can mark the selection directly in the Excel worksheet below the form and the range automatically fills in, like so:
After selecting the ranges, the user must enter (for each range) the column that holds the id for that range. Rows that have the same ID from both ranges will be joined, so indicating which column holds the ID is pretty important.
When the data is filled out, the form should look like this:
Finally you should choose the place where you want to put the joined data. Like so:
Once you press the join button, the joined data will be written to the selected location:
How it WorksThe code behind this form is pretty simple and you can adapt it to your needs.
All the work is done by one main subroutine
Sub JoinRanges(rngFirstRange As Range, lngFirstIdColumn As Long, rngSecongRange As Range, lngSecondIDColumn As Long, rngWriteResultTo As Range)This routine accepts the ranges, the id columns and the target range (where the data would be written).
For each line copied, the routine uses the match function to find the matching row in the second function:
Set rngColumn = rngSecongRange.Columns(lngSecondIDColumn) lngMatchingRow = WorksheetFunction.Match(varCurrentId, rngColumn, 0)Finally we copy the line from the second range (without the id column).
SummaryAs you can see, our macro does all the heavy lifting for you. These kinds of repetitive data management tasks can be dramatically optimized with the right spreadsheets, we hope this shows one way your workload can be improved.
About the authorYoav Ezer co-authors the technology and productivity blog Codswallop. He is also the CEO of a company that produces PDF to XLS conversion software.
For more Excel tips from Yoav, join him on Facebook or Twitter