EXCEL VBA Find Headers and Copy Column from Sheet 1 to Sheet 2


New Member
Feb 3, 2016
I want to copy data from Sheet1 to Sheet2 based on headers in two sheets. The following code works great when both sheets’ headers are in first row. My question is if header in Sheet2 is in row 8, then how would my code be? thank you.

Sub CopyData()
Dim sws As Worksheet, dws As Worksheet
Dim slr As Long, dlc As Long, c As Long, col As Long
Dim colRng As Range, Rng As Range, Cell As Range
Application.ScreenUpdating = False

Set sws = Sheets("Sheet1")
Set dws = Sheets("Sheet2")

slr = sws.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
dlc = dws.Cells(1, Columns.Count).End(xlToLeft).Column

For c = 1 To dlc
Set colRng = sws.Rows(1).Find(what:=dws.Cells(1, c), lookat:=xlWhole)
If Not colRng Is Nothing Then
col = colRng.Column
sws.Range(sws.Cells(2, col), sws.Cells(slr, col)).Copy dws.Cells(2, c)
End If
Next c

Application.ScreenUpdating = True
End Sub

Some videos you may like

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).


MrExcel MVP
Sep 2, 2009
[COLOR=#333333]Sub CopyData()[/COLOR]
[COLOR=#333333]Dim sws As Worksheet, dws As Worksheet[/COLOR]
[COLOR=#333333]Dim slr As Long, dlc As Long, c As Long, col As Long[/COLOR]
[COLOR=#333333]Dim colRng As Range, Rng As Range, Cell As Range[/COLOR]
[COLOR=#333333]Application.ScreenUpdating = False[/COLOR]

[COLOR=#333333]Set sws = Sheets("Sheet1")[/COLOR]
[COLOR=#333333]Set dws = Sheets("Sheet2")[/COLOR]

[COLOR=#333333]slr = sws.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row[/COLOR]
[COLOR=#333333]dlc = dws.Cells([/COLOR][COLOR=#ff0000]8[/COLOR][COLOR=#333333], Columns.Count).End(xlToLeft).Column[/COLOR]

[COLOR=#333333]For c = 1 To dlc[/COLOR]
[COLOR=#333333]Set colRng = sws.Rows(1).Find(what:=dws.Cells([/COLOR][COLOR=#ff0000]8[/COLOR][COLOR=#333333], c), lookat:=xlWhole)[/COLOR]
[COLOR=#333333]If Not colRng Is Nothing Then[/COLOR]
[COLOR=#333333]col = colRng.Column[/COLOR]
[COLOR=#333333]sws.Range(sws.Cells(2, col), sws.Cells(slr, col)).Copy dws.Cells([/COLOR][COLOR=#ff0000]9[/COLOR][COLOR=#333333], c)[/COLOR]
[COLOR=#333333]End If[/COLOR]
[COLOR=#333333]Next c[/COLOR]

[COLOR=#333333]Application.ScreenUpdating = True[/COLOR]
[COLOR=#333333]End Sub[/COLOR]
Last edited:

Watch MrExcel Video

Forum statistics

Latest member

This Week's Hot Topics

  • Timer in VBA - Stop, Start, Pause and Reset
    [CODE=vba][/CODE] Option Explicit Dim CmdStop As Boolean Dim Paused As Boolean Dim Start Dim TimerValue As Date Dim pausedTime As Date Sub...
  • how to updates multiple rows in muliselect listbox
    Hello everyone. I need help with below code. code is only chaning 1st row in mulitiselect list box. i know issue with code...
  • Delete Row from Table
    I am trying to delete a row from a table using VBA using a named range to find what I need to delete. My Range is finding the right cell. In the...
  • Assigning to a variable
    I have a for each block where I want to assign the value in column 5 of the found row to the variable Serv. [CODE=vba] For Each ws In...
  • Way to verify information
    Hi All, I don't know what to call this formula, and therefore can't search. I have a spreadsheet with information I want to reference...
  • Active Cell Address – Inactive Sheet
    How to use VBA to get the cell address of the active cell in an inactive worksheet and then place that cell address in a location on the current...