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.
Street 1Street 2Street 3
1 1C 1 MOORGROVE HOUSE
2 3 2 MOORGROVE HOUSE
3 5 3 MOORGROVE HOUSE
4 7 4 MOORGROVE HOUSE
5 9 5 MOORGROVE HOUSE
6 11 6 MOORGROVE HOUSE

<tbody>
</tbody><colgroup><col><col><col></colgroup>

Exported to these files

Filename = Street 1.txt or .csv
File contents:
1
2
3
4
5
6

<tbody>
</tbody><colgroup><col></colgroup>

File name = Street 2.txt
File contents:
1C
3
5
7
9
11

<tbody>
</tbody><colgroup><col></colgroup>

File name = Street 3.txt
1 MOORGROVE HOUSE
2 MOORGROVE HOUSE
3 MOORGROVE HOUSE
4 MOORGROVE HOUSE
5 MOORGROVE HOUSE
6 MOORGROVE HOUSE

<tbody>
</tbody><colgroup><col></colgroup>
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
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,214,985
Messages
6,122,607
Members
449,090
Latest member
vivek chauhan

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