title: Copy Data from One workbook to another using column header
body: <p>Does any one have a piece of code to copy from one excel WB to another based on column headers?  </p>
  
  <p>Update:
  Sorry to all, I am new to this site and I hope you can forgive my ignorance.  </p>
  
  <p>Here is the code I have tried, based on other's posts (thank you, Simon!).</p>
  
  <pre><code>Sub copy_cols()
  
      Set SourceWS = Workbooks("Source.xlsx").Worksheets(1)
      Set TargetWS = Workbooks("Business Loader V7.1.xlsx").Worksheets(2)
  
  For Each rgCell In SourceWS.Range("A1:AX1")
  
  TargetWS.Columns(GetColumn(TargetWS, rgCell.Value)) = _
   SourceWS.Columns(GetColumn(SourceWS, rgCell.Value))
  ' I Have also tried this with no success:
  ' TargetWS.Columns(GetColumn(TargetWS, rgCell.Value)) = _
   SourceWS.Columns(GetColumn(SourceWS, rgCell.Column))
  
  End Sub
  
  Function GetColumn(GCSheet As Worksheet, ColumnName As String) As Integer
      Dim intCol As Integer
  
      On Error Resume Next
      intCol = Application.WorksheetFunction.Match(ColumnName, GCSheet.Rows(1), 0)
      If Err.Number &lt;&gt; 0 Then
          GetColumn = 0
      Else
          GetColumn = intCol
      End If
  End Function
  </code></pre>
  
  <p>I am getting an error "ByRef argument type mismatch" on the first and 5th line (excluding the spaces when count) at TargetWS.Cells....</p>
  
  <p>I also have this... which works, but I have to add in a bunch of .End(xlDown)'s to account for missing information so the whole column is copied (not just to the next cell WITH a value).  Do you have a better system to account for this?</p>
  
  <pre><code>Sub CopyHeaders()
      Dim header As Range, headers As Range
  
      Set SourceWS = Workbooks("Source.xlsx").Worksheets(1)
      Set TargetWS = Workbooks("Business Loader V7.1.xlsx").Worksheets(2)
  
      Set headers = SourceWS.Range("A1:AX1")
  
      For Each header In headers
          If GetHeaderColumn(header.Value) &gt; 0 Then
             Range(header.Offset(1, 0), header.End(xlDown).End(xlDown).End(xlDown).End(xlDown).End(xlDown).End(xlDown).End(xlDown).End(xlDown).End(xlDown)).Copy Destination:=TargetWS.Cells(2, GetHeaderColumn(header.Value)) '.End(xlDown).Offset(1, 0)
          End If
      Next
  </code></pre>
  
  <p>As you can see, I have to add in .End(xlDown) for every blank cell.  Thanks in advance for any assistance you can offer. </p>