VBA Copy headers from Inputfile into Masterworkbook

Mallesh23

Well-known Member
Joined
Feb 4, 2009
Messages
976
Office Version
  1. 2010
Platform
  1. Windows
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 !

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
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)

Forum statistics

Threads
1,214,605
Messages
6,120,473
Members
448,967
Latest member
visheshkotha

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top