Copy data under specific columns in sheet1 n paste to sheet2 under the same headers

ShyamExcel

New Member
Joined
Mar 8, 2012
Messages
1
I'm pretty new to VBA and I'm trying to automate a few things and the task at hand is like this:

Two sheets in my workbook 'marketing.xlsm' -Sheet1 and Sheet2. Sheet1 is the source sheet containing between 5 and 15 columns at different instances (import different CSV files weekly) but they will always contain the 4 columns alpha, beta, gamma, and theta in no particular order but always on row 2.

Sheet2 is my destination sheet which will have only the 4 headers on row 2 for the four columns - alpha, beta, gamma and theta. Only headers in this blank sheet on row 2. No data under them.

What I want to do is create a VBA code to look up each of header values in row 2 of sheet2, search sheet1 in row 2 for the cell text (say, alpha), copy the data from the corresponding column and paste the data without header into sheet 2. So in effect, we would have copied data underneath column 'alpha' in sheet1 into column 'alpha' in sheet2. (if alpha column was in B2 in sheet1 and had 100 data points, we would have copied B3:B103 into sheet 2 under column 'alpha'.

Alternative source:
Since the data for sheet1 comes from a web report as CSV, I'm also thinking, instead of importing the data into sheet1, if the VBA code can look up the file named 'data.csv' and sheet1 in it, extract the data and paste under the 4 columns listed in sheet2 of my destination workbook (marketing.xlsm) directly, that would be even better.

The critical points are
1. All column headers in sheet1 can occur anywhere in row 2, no fixed position. So alpha can be at C2, D2, or G2 or such.
2. The number of data points under each column can vary too (10 to 10000) but all columns will have the same number of data points as its a table.

Appreciate your suggestions and any assistance you can provide!

Shyam
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.

Forum statistics

Threads
1,215,477
Messages
6,125,030
Members
449,205
Latest member
Eggy66

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