title: Copy data from one worksheet to another based on column
body: <p>I am trying to write a macro that will copy data from one worksheet to another based on column headers. Lets say in ws1 there are three columns: "product", "name", "employer" and the ws2: "product", "name", "region". </p>
  
  <p>So i want the macro to do all the copying as in my original file i have over 100 column headers and it will be very time consuming for to do it myself. </p>
  
  <p>I have written two macros without succes. VBA is something I cant understand for quite some time. but still managed to write something, hope you can tell me if i am going in the right direction. </p>
  
  <p>this is v1</p>
  
  <pre><code>Sub Copy_rangev1()
  
  Dim Ws1 As Worksheet, Ws2 As Worksheet
  Dim SourceRange As Range, CopyRange As Range
  Dim lastrow As Long
  Dim i As Integer
  
  Set Ws1 = ThisWorkbook.Worksheets("Sheet1")
  Set Ws2 = ThisWorkbook.Worksheets("sheet2")
  
  lastrow = Cells(Rows.Count, 1).End(xlUp).Row + 1
  
  Set SourceRange = Ws2.Range("A1").CurrentRegion
  Set CopyRange = Ws1.Range("A1").CurrentRegion
  
  For i = 1 To lastrow
      If SourceRange.Cells(i, 1).Value = CopyRange.Cells(i, 1) Then
         SourceRange.Cells(i + 1 &amp; lastrow, 1).Copy Destination:=CopyRange.Range("a" &amp; lastrow)
      End If
  Next i
  
  End Sub
  </code></pre>
  
  <p>this v2: </p>
  
  <pre><code>Sub Copyrangev2()
  
  Dim SourceRange As Worksheet
  Dim CopyRange As Worksheet
  Dim lastrow As Integer
  Set SourceRange = Worksheets("Sheet2")
  Set CopyRange = ThisWorkbook.Worksheets("sheet1")
  Dim i As Integer
  
  lastrow = Cells(Rows.Count, 1).End(xlUp).Row
  
  For i = 1 To 100
      If SourceRange.Range(1, i).Value = CopyRange.Range(1, i) Then
         SourceRange.Range(1, i).Offset(1, 0).Copy Destination:=CopyRange.Range(1, i)
      End If
  Next i
  
  End Sub
  </code></pre>
  
  <p>My code is a mess, but if you want me to provide any more details leave a comment, i dont expect you to given a fully workable code, a good explanation and few suggestions will do. Thanks</p>