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

wongjoh1

New Member
Joined
Feb 3, 2016
Messages
3
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
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Code:
[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:
Upvote 0

Forum statistics

Threads
1,223,880
Messages
6,175,152
Members
452,615
Latest member
bogeys2birdies

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