Copying data based on header value

padawan88

New Member
Joined
Jul 14, 2018
Messages
1
Hi Excel Master,

Every month, I'm require to generate a report. I export the data from a source, put in my excel file and have to arrange the column to meet my layout . My report has a fix layout whereby the column header is fixed (e.g A1 - Name, B1 - Address, etc). The source header text is the same as my layout header text but the arrangement is not (e.g A1 - Address, B1 - Name, etc).

Is there an macro that can help to based on my fixed layout header text (say in Sheet1) copy the data from the source (say in Sheet 2). My source has a lot data header. The macro mechanics that I am thinking is if header text in Sheet1 (my fixed layout) match header text in Sheet 2 (source), copy all the data in the column of the header in Sheet2 and paste to the column in Sheet1.
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Try this:
Code:
Sub Copy_Two()
'Modified  7/14/2018  5:55:09 PM  EDT
Application.ScreenUpdating = False
Sheets(1).Activate
Dim i As Long
Dim ans As String
Dim anss As Long
Dim LastrowColumn As Long
Dim SearchString As String
Dim SearchRange As Range
Lastcolumn = Sheets(1).Cells(1, Columns.Count).End(xlToLeft).Column
For i = 1 To Lastcolumn
    ans = Sheets(1).Cells(1, i).Value
    SearchString = Sheets(1).Cells(1, i).Value
    Set SearchRange = Sheets(2).Cells(1, 1).Resize(, Lastcolumn).Find(SearchString, LookIn:=xlValues, lookat:=xlWhole)
    anss = SearchRange.Column
    Sheets(2).Columns(anss).Copy Sheets(1).Columns(i)
Next
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Welcome to the MrExcel board!

Here is another version that you could also consider.
Rich (BB code):
Sub Copy_Data()
  Dim ws1 As Worksheet, ws2 As Worksheet
  Dim c As Range, HeaderFound As Range

  Set ws1 = Sheets("Sheet1")  '<- Check sheet name
  Set ws2 = Sheets("Sheet2")  '<- Check sheet name
  ws1.UsedRange.Offset(1).Clear
  For Each c In Intersect(ws1.UsedRange, ws1.Rows(1))
    Set HeaderFound = ws2.Rows(1).Find(What:=c.Value, LookIn:=xlValues, lookat:=xlWhole, MatchCase:=False)
    If Not HeaderFound Is Nothing Then Intersect(HeaderFound.EntireColumn, ws2.UsedRange).Copy Destination:=c
  Next c
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,691
Members
448,978
Latest member
rrauni

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