Find column header and copy contents of column to a second sheet within workbook

Lilly44

New Member
Joined
Feb 7, 2014
Messages
20
Hi,
I was wondering if someone could help me with a macro. I have a spreadsheet 'Sheet 1' that has column headers starting on row 8 and information corresponding to the columns for up to 50 rows. The column headers span columns A-Z. I need to find the following headers: "Well", "Sample Name", "Task", "Ct" and "Quantity" and move the header and contents of the rows underneath to the following static locations in 'Sheet 2': G9, A9, B9, H9 and C9 respectively.

The column headers and corresponding info in Sheet 1 are not always in the same columns which is why i'm saying they need to be 'found' and then moved over however, they always start on row 8.

Thanks in advance for any help!
Lilly
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
This assumes by "move the header and contents" you mean cut them. Also assumes that the tab names are Sheet1 and Sheet2 (not Sheet 1 and Sheet 2), and that Sheet2 already exists.
Code:
Sub Lilly44()
Dim Sht1 As Worksheet, Sht2 As Worksheet, FindRng As Range, Fnd As Range, Headers As Variant, Receivers As Variant, lR As Long
Set Sht1 = Sheets("Sheet1"): Set Sht2 = Sheets("Sheet2")
Headers = Array("Well", "Sample Name", "Task", "Ct", "Quantity")
Receivers = Array("G9", "A9", "B9", "H9", "C9")
Set FindRng = Intersect(Sht1.Rows(8), Sht1.Columns("A:Z"))
Application.ScreenUpdating = False
For i = LBound(Headers) To UBound(Headers)
    Set Fnd = FindRng.Find(Headers(i))
    If Not Fnd Is Nothing Then
        lR = Sht1.Cells(Sht1.Rows.Count, Fnd.Column).End(xlUp).Row
        Sht1.Range(Fnd, Sht1.Cells(lR, Fnd.Column)).Cut Destination:=Sht2.Range(Receivers(i))
    Else
        MsgBox "Can't find the header " & Headers(i) & " in Sheet1, row 8"
    End If
Next i
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Thanks for your prompt respose and help JoeMo!

The second assumption is correct but can it be tweaked so that instead of cutting it the columns are copied onto Sheet2?
Thanks again.
Lilly
 
Upvote 0
Hi Joe! Thanks for the above solution, I was able to tweak it to my own needs and works perfectly well almost in one case:
Let's say I have headers ["Employee ID", "Employee"] in that particular order and I want to copy-paste only the "Employee" column. When I use the above code, it for some reason copies "Employee ID" and not the "Employee" column.

Any suggestions?
 
Upvote 0
Change this line:
Set Fnd = FindRng.Find(Headers(i))
to this:
Set Fnd = FindRng.Find(Headers(i),Lookat:= xlWhole)
 
Upvote 0

Forum statistics

Threads
1,214,976
Messages
6,122,539
Members
449,088
Latest member
RandomExceller01

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