Export colums as individual csv files

steffen82

New Member
Joined
Jan 26, 2017
Messages
4
Hi Could someone advise on how you can export each column to an individual csc or tab file using the header as the file name please.

i.e.
[TABLE="width: 377"]
<tbody>[TR]
[TD]Street 1[/TD]
[TD]Street 2[/TD]
[TD]Street 3[/TD]
[/TR]
[TR]
[TD]1 [/TD]
[TD]1C [/TD]
[TD]1 MOORGROVE HOUSE [/TD]
[/TR]
[TR]
[TD]2 [/TD]
[TD]3 [/TD]
[TD]2 MOORGROVE HOUSE [/TD]
[/TR]
[TR]
[TD]3 [/TD]
[TD]5 [/TD]
[TD]3 MOORGROVE HOUSE [/TD]
[/TR]
[TR]
[TD]4 [/TD]
[TD]7 [/TD]
[TD]4 MOORGROVE HOUSE [/TD]
[/TR]
[TR]
[TD]5 [/TD]
[TD]9 [/TD]
[TD]5 MOORGROVE HOUSE [/TD]
[/TR]
[TR]
[TD]6 [/TD]
[TD]11 [/TD]
[TD]6 MOORGROVE HOUSE [/TD]
[/TR]
</tbody><colgroup><col><col><col></colgroup>[/TABLE]

Exported to these files

Filename = Street 1.txt or .csv
File contents:[TABLE="width: 88"]
<tbody>[TR]
[TD]1 [/TD]
[/TR]
[TR]
[TD]2 [/TD]
[/TR]
[TR]
[TD]3 [/TD]
[/TR]
[TR]
[TD]4 [/TD]
[/TR]
[TR]
[TD]5 [/TD]
[/TR]
[TR]
[TD]6 [/TD]
[/TR]
</tbody><colgroup><col></colgroup>[/TABLE]

File name = Street 2.txt
File contents:
[TABLE="width: 131"]
<tbody>[TR]
[TD]1C [/TD]
[/TR]
[TR]
[TD]3 [/TD]
[/TR]
[TR]
[TD]5 [/TD]
[/TR]
[TR]
[TD]7 [/TD]
[/TR]
[TR]
[TD]9 [/TD]
[/TR]
[TR]
[TD]11 [/TD]
[/TR]
</tbody><colgroup><col></colgroup>[/TABLE]

File name = Street 3.txt
[TABLE="width: 158"]
<tbody>[TR]
[TD]1 MOORGROVE HOUSE [/TD]
[/TR]
[TR]
[TD]2 MOORGROVE HOUSE [/TD]
[/TR]
[TR]
[TD]3 MOORGROVE HOUSE [/TD]
[/TR]
[TR]
[TD]4 MOORGROVE HOUSE [/TD]
[/TR]
[TR]
[TD]5 MOORGROVE HOUSE [/TD]
[/TR]
[TR]
[TD]6 MOORGROVE HOUSE [/TD]
[/TR]
</tbody><colgroup><col></colgroup>[/TABLE]
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Try this, changing the folder path as required.
Code:
Public Sub Export_Each_Column_As_CSV_File()

    Dim lastCol As Long, lastRow As Long, c As Long
    
    With ActiveSheet
        lastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
        For c = 1 To lastCol
            lastRow = .Cells(Rows.Count, c).End(xlUp).Row
            Open "C:\path\to\folder\" & .Cells(1, c).Value & ".csv" For Output As [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] 
            Print [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] , Join(Application.Transpose(.Cells(2, c).Resize(lastRow - 1).Value), vbCrLf)
            Close [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] 
        Next
    End With
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
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