Sub copy2master()
'
' Open both files Master (mf) and Data (cf)
' Go to the data file and select header area and run this macro
' Click or select Macros you will see this
' macro as 'Master.xlsm'!copy2master click on run
' v1 User's version
' v2 Copies matching headers columns initial version
' v3 Data files have a 2nd row that should not be copied
' If new columns are found they will be copied at the right end of master file
' v4 Empty and blamk headers are not processed there is not matchig with blank or empty strings
' v5 Area with informatio to copy from is discovered by selected range only
' Define names of files and sheets so it is easier when they change
Dim mf, cf, key As String
Dim lr, lc, lrcf, lccf, y1, x1, y2, y4, x4 As Long
Dim Wbmf As Workbook
Dim mc As Range
mf = "MASTER.xlsm"
cf = ActiveWorkbook.Name
' Finds number of columns in data file (cf) to copy using selected area
lccf = Selection.Columns.Count
If lccf < 2 Then
rc = MsgBox("Please select a columns header range before calling this macro, " & _
"this macro needs a columns header range selected with more " & _
"than one column to count columns from.", _
vbOKOnly, "Error: No range was selected")
Else
' Finds rows in cf with selected range
lrcf = Selection.Rows.Count
' Finds last used row in master file (mf) also it deactivates screen updates
Application.ScreenUpdating = False
Windows(mf).Activate
Set Wbmf = ActiveWorkbook
lr = Range("A1").CurrentRegion.Rows.Count + 1
lc = Range("A1").CurrentRegion.Columns.Count
Set mc = Range(Range("A1"), Range("A1").Offset(0, lc - 1))
' Goes to cf
Windows(cf).Activate
' Loop thru all columns of cf
For i = 1 To lccf
' Gets column heading
key = Range("A1").Offset(0, i - 1).Value
' There is no good way to handle empty column headers
' They will not be processed
If key > " " Then ' v4: Empty coll heading not processed
' Gets corresponding column number using name as key in master sheet (mf)
kc = 0
On Error Resume Next
kc = mc.Find(key, LookIn:=xlValues, LookAt:=xlWhole).Column
'
' If kc is > 0 it has found a corresponding column in master file will copy and paste
' if kr = 0 then it means that column in data file is new and the macro will (v3) paste
' at the far right end of the master file
' (When a value is not found an error 91 is thrown that is why on error is used)
'
' If in the data file (cf) there are two columns with the same column heading
' the second occurrance will paste over the first so you will see only the second
'
' Calcs range origin and destination
y1 = 2 ' v3: Do not include Second row (changed 1 for a 2)
x1 = i - 1
y2 = lrcf - 1
y4 = lr - 1
x4 = kc - 1
' Copy range
Range(Range("A1").Offset(y1, x1), Range("A1").Offset(y2, x1)).Copy
' Activate master sheet and Paste range to cell
Windows(mf).Activate
If kc > 0 Then
' Column exists in master file
Range("A1").Offset(y4, x4).PasteSpecial _
Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Else
' Column does not exists in master file paste at the far end right
' Paste column heading
Range("A1").Offset(0, lc).FormulaR1C1 = key
Range("A1").Offset(y4, lc).PasteSpecial _
Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
' Updates lc for new coloumn
lc = lc + 1
End If
Windows(cf).Activate
' Process next column in data file (cf)
End If
Next i
End If
' it activates screen updates
Application.ScreenUpdating = True
End Sub