Copy data from one sheet to another using headers and placements.

tonywatsonhelp

Well-known Member
Joined
Feb 24, 2014
Messages
3,194
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Hi Everyone,
I need a macro, that when run copys data from one sheet to another using header names,

So I have 3 sheets
Raw Data and Clean Data, then Control

In Control,
Column B I want to put the names for my Headers I want copied over, and Column C the destination Headers To Copy to like This

Sheet "Control"
ABCD
1Name of Copy HeadersName of Paste headers
2NameName
3SalesSales Amount
4AgeD.O.B
5AmountAmount
6etc.
7
8

So what I want the macro to do is take the first Copy Header "Name" and find what column it is in by looking along row 5,
If it not found then in D I want it to say "No Copy Header found" and move on to the next header
If found then copy from row 6 to last row.

then find destination Header name, in sheet " Clean Data row 3"
again if not found, in D I want it to say "No Destination header found" and move on to the next header

So basicly, in this case take the data in Sheet Raw data for each header listed copy and paste it into sheet clean data .

Please help if you can

Thanks
Tony
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
It would be easier to help if you could use the XL2BB add-in (icon in the menu) to attach screenshots (not pictures) of your Raw Data and Clean Data sheets. Alternately, you could upload a copy of your file to a free site such as www.box.com or www.dropbox.com. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here. Explain in detail what you want to do referring to specific cells, rows, columns and sheets using a few examples from your data (de-sensitized if necessary).
 
Upvote 0
Try:
VBA Code:
Sub CopyData()
    Application.ScreenUpdating = False
    Dim LastRow As Long, fnd1 As Range, fnd2 As Range, rng As Range, srcWS As Worksheet, desWS As Worksheet, crtlWS As Worksheet
    Set srcWS = Sheets("Raw Data")
    Set desWS = Sheets("Clean Data")
    Set crtlWS = Sheets("Control")
    For Each rng In crtlWS.Range("B2", crtlWS.Range("B" & Rows.Count).End(xlUp))
        Set fnd1 = srcWS.Rows(5).Find(rng.Value, LookIn:=xlValues, lookat:=xlWhole)
        If Not fnd1 Is Nothing Then
            Set fnd2 = desWS.Rows(3).Find(rng.Offset(, 1).Value, LookIn:=xlValues, lookat:=xlWhole)
            If Not fnd2 Is Nothing Then
                With srcWS
                    LastRow = .Cells(.Rows.Count, fnd1.Column).End(xlUp).Row
                    fnd1.Offset(1).Resize(LastRow - 5).Copy fnd2.Offset(1)
                End With
            Else
                MsgBox ("Destination header " & rng.Offset(, 1) & " was not found.")
            End If
        Else
            MsgBox ("Copy header " & rng & " was not found.")
        End If
    Next rng
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,908
Messages
6,122,187
Members
449,072
Latest member
DW Draft

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