create excel file from uncontiginous date

neveu

Board Regular
Joined
Jan 27, 2009
Messages
225
hy forum,

would need your support with the following challenge:

i have raw data with several columns - which can also contain blank columns or uneeded data ; i would like to copy information from only several columns into a new excel file.

here is an example:

row data

col1 blankcol column2 blankcol column3

the layout in the excel i create should be

col1 column2 column3


i've tried using Union but i don't quite get the code to select the range retrieved with Union and copy it in the new excel file.
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
not clear why not delete the blank columns. perhaps a trivial solution. explain little more preferably with small extract of your sheet
 
Upvote 0
As an example, this copies non contiguous columns from Sheet1 to Sheet2.

Code:
Sheets("Sheet1").Range("A:A, C:C, E:E").Copy _
    Destination:=Sheets("Sheet2").Range("A1")
 
Upvote 0
i've tried using Union but i don't quite get the code to select the range retrieved with Union and copy it in the new excel file.

Hi

No problem using Union either:

Code:
Dim r As Range
 
With Worksheets("Sheet1")
    Set r = Application.Union(.Columns(1), .Columns(3), .Columns(5))
End With
 
r.Copy Destination:=Worksheets("Sheet3").Range("J1")
 
Last edited:
Upvote 0
hi all,

the issue with Union is that i;m using it in a loop that goes through 20 columns and if these have a "yes" asociated on another column, then the column will be added to my range
 
Upvote 0
hi all,

the issue with Union is that i;m using it in a loop that goes through 20 columns and if these have a "yes" asociated on another column, then the column will be added to my range

Hi

I don't see any problem in adding columns to the range.

For ex., using the code that I posted

Code:
With Worksheets("Sheet1")
    Set r = Application.Union(.Columns(1), .Columns(3), .Columns(5))
End With

if you want to add another column, you can use

Code:
With Worksheets("Sheet1")
    Set r = Application.Union(r, .Columns(10))
End With

You can add more columns like this in the loop.

If you have difficulties post the relevant part of the code you are using.
 
Upvote 0

Forum statistics

Threads
1,223,162
Messages
6,170,432
Members
452,326
Latest member
johnshaji

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