selecting multiple columns, copy to new sheet VBA code ?

RompStar

Well-known Member
Joined
Mar 25, 2005
Messages
1,200
Hi there....

I have a raw report that I get, it's headers are always the same, but the order of the columns might not be the same everytime and the number of rows also could be different.

Anyways, I know what headers of the columns of interest that I want, I would like to copy the multiple columns and then paste them into a new sheet, please keep in mind that the columns are multiple and might not be next to each others, so:

Let's say that I have a sheet with Columns A through W

but I might only want 5 columns, A, C, R, P, W - just as as example:

The header name are always the same, say:

A - First Name
C - Last Name
R - Suffix
P - Home Phone
W - Spend Rank

I noticed that I could select multiple columns by using the Ctrl + and clicking on the columns, but I couldn't copy them or nothing, it wasn't allowed.

Please let me know how I can do this in VBA! that would be a great starting point for me, then I will need to learn how to place the columns in the order that I want and do some other things on them.

Thanks!
 
Hi, I know this is an old post, but I reply to it because I am using Peter's code and maybe someone can still help me.
If I should put this question as a new post, let me know!

The code works like a charm, except for a little problem with extracting the right columns if the columns headings I put in the myColumns Array are also part of another column headings.

For instance, if I put in that array the following column headings:

Code:
[COLOR=#333333][FONT=Courier]myColumns = Array("Matrix_nr", "CI", "Switch", "Switch_count")[/FONT][/COLOR]


The new worksheet then returns the following columns:

Matrix_nr
round_decimals
count_switch_sequence
switch_count

(I marked the original column headings that I want to extract in bold to show what happens here).
Not the column with the exact column heading that I want is extracted, but another column with a heading that contains the string I indicated is extracted!

And the strange thing is, that for instance "CI" comes earlier in my original datafile than "round_decimals".


Does anyone know what I can change in the code to fix this?
 
Upvote 0

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.

Forum statistics

Threads
1,223,986
Messages
6,175,788
Members
452,670
Latest member
nogarth

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