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
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