PDA

View Full Version : Solved: Excel match headers, append data



fsc23
06-11-2010, 06:08 PM
Hello, I guess this has been already asked and answered somewhere but I haven't been able to find it. I am trying to append data from several "Source" files into a "Master" file based on the column headers. The "Source" files might contain only a few or many columns of data but the headers are kept consistent, although not necessarily in the same order, and all the columns/headers are included in the "Master" file. I've seen code that requires for the combining spreadsheets to maintain the same structure but this is not the case. I have attached a sample file. I thank you in advance for your help.:help

tpoynton
06-11-2010, 07:26 PM
Perhaps this can be combined with some of the other code you found. This combines data from sheets in the same workbook. The sample has the columns in the same order, but some missing - this will not work if they are not in the same order I fear. It will also be slow if there is a lot of data.

Again, the expectation is that you'll combine this with other readily available code - it's meant to be a start, not the finished product!

Welcome to the forum :)
Option Explicit

Public Sub processData()
Dim rCell As Range
Dim i As Long
Dim iLastrowMaster As Long
Dim iLastrowSource As Long
Dim wsMasterSht As Worksheet
Set wsMasterSht = ThisWorkbook.Sheets("master")
iLastrowMaster = wsMasterSht.UsedRange.Rows.Count + 1
iLastrowSource = ThisWorkbook.Sheets("source").UsedRange.Rows.Count
For i = 1 To 10
With ThisWorkbook.Sheets("source")
For Each rCell In .Range(.Cells(1, 1), .Cells(1, .Columns.Count).End(xlToLeft))
If rCell.Value = wsMasterSht.Cells(1, i).Value Then
.Range(.Cells(2, rCell.Column), .Cells(iLastrowSource, rCell.Column)).Copy
wsMasterSht.Cells(iLastrowMaster, i).PasteSpecial (xlPasteValues)
End If
Next rCell
End With
Next i
End Sub

fsc23
06-12-2010, 09:28 AM
Thanks tpoynton, this works beautifully, just as I needed it!!

Best regards