Hi Team,
Need your help in Copying headers from Input File into Master workbook.
user provides List of Headers he wants in Master workbook.
User provides 60 header Name to be pasted in new Blank Master workbook.
afterword he provides Input file which Contain 40 headers.
Now my Task is i have to match input files header with Master workbooks HeaderName and paste data below the header of Masterworkbook.
My attempted below code, which is not working, But it will give an idea , What I am trying to achieve. thanks in advance !
Need your help in Copying headers from Input File into Master workbook.
user provides List of Headers he wants in Master workbook.
User provides 60 header Name to be pasted in new Blank Master workbook.
afterword he provides Input file which Contain 40 headers.
Now my Task is i have to match input files header with Master workbooks HeaderName and paste data below the header of Masterworkbook.
My attempted below code, which is not working, But it will give an idea , What I am trying to achieve. thanks in advance !
VBA Code:
Sub Headers_to_Copy()
Dim ar As Variant
Dim header_lr As Long
Dim lr As Long
'Header List to copy from input file to Master workbook
ar = Map.Range("c2:C61")
Dim dwbk As Workbook
Set dwbk = Workbooks.Open(Mac.Range("b5").Value, , True)
Dim LastRow As Long
LastRow = dwbk.Worksheets(1).Range("a6000").End(xlUp).Row
Workbooks.Add
Dim mwbk As Workbook
Set mwbk = ActiveWorkbook
mwbk.ActiveSheet.Range("a1").Resize(, UBound(ar)).Value = WorksheetFunction.Transpose(ar)
lr = Map.Range("a100").End(xlUp).Row
Dim dest_col As Long
Dim copy_col As Long
Dim i As Integer
Dim wsf As WorksheetFunction
Set wsf = Application.WorksheetFunction
For i = 2 To lr
On Error Resume Next
dest_col = wsf.Match(Map.Cells(i, 1).Value, mwbk.Worksheets(1).Rows("1:1"), 0)
copy_col = wsf.Match(Map.Cells(i, 1).Value, dwbk.Worksheets(1).Rows("1:1"), 0)
dwbk.Worksheets(1).Range(Cells(2, copy_col), Cells(LastRow, copy_col)).Copy
mwbk.Worksheets(1).Cells(2, dest_col).PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
On Error GoTo 0
Next i
End Sub