Copy and Paste columns based on headers matching a list

mjayz

New Member
Joined
Sep 18, 2009
Messages
30
Office Version
  1. 2013
Hi

I have sheet1 with approx 60 columns with unique headers in row 1. I want to copy some of these columns into a new sheet based on a list in sheet2, column A. So if there are 3 rows in that list, it will copy & paste 3 columns into the new sheet being columns A,B & C.

My knowledge of using variables and ranges is not good so I appreciate the help.

Thanks
MJ
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Code:
With ThisWorkbook
LRow = .Sheets(2).Cells(.Sheets(2).Rows.Count, "A").End(xlUp).Row

.Sheets(3).Columns(1).Resize(, LRow).Value = .Sheets(1).Columns(1).Resize(, LRow).Value
End With
This copies the only the values from one sheet to the other
 
Upvote 0
Thanks for the quick response Tim.

Apologies I was a bit vague on the request. The list in sheet2 should match the headers in sheet1 and copy those columns only. Not necessarily the first 3 columns but it could be column D, J, & K if those are the matching headers which then go into column A,B & C in sheet3 in that order. The list in sheet2 will vary depending on what I need for the report. Could contain 3 headers up to 20 probably.

Thanks
MJ
 
Upvote 0
Code:
With ThisWorkbook
LRow = .Sheets(2).Cells(.Sheets(2).Rows.Count, "A").End(xlUp).Row

For each cell in .Sheets(2).Range("A1:A" & LRow)
    Set FindHeader = .Sheets(1).Rows(1).Find(cell.value)
    If not FindHeader Is Nothing Then
        LastCol = .Sheets(3).Cells(1, .Sheets(3).Columns.Count).End(xlToLeft).Column + 1
        .Sheets(3).Columns(LastCol).Value = .Sheets(1).Columns(FindHeader.Column).Value
    End If
Next cell
 
Upvote 0

Forum statistics

Threads
1,215,459
Messages
6,124,947
Members
449,198
Latest member
MhammadishaqKhan

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