How to save individual rows in a spreadsheet as a csv file

dbzmkl

New Member
Joined
Jul 9, 2011
Messages
10
i have a spreadsheet that has approximately 5000, names, contacts, and info of clients.

i need to separate the each row into a unique csv file that has as it's name the information in the first cell and saved on the same folder. so if the cell in column 1 reads ABC123 then the csv would be named ABC123.

if this could be a macro button that can do this automatically for all rows except row 1, it would be highly appreciated.
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Assuming that Row 1 contains the column headers, and that the data starts at Column A, make sure that the sheet containing the data is the active sheet and then try the following macro...

Code:
[font=Verdana][color=darkblue]Option[/color] [color=darkblue]Explicit[/color]

[color=darkblue]Sub[/color] CreateCSVFiles()

    [color=darkblue]Dim[/color] strDestPath [color=darkblue]As[/color] [color=darkblue]String[/color]
    [color=darkblue]Dim[/color] LastRow [color=darkblue]As[/color] [color=darkblue]Long[/color]
    [color=darkblue]Dim[/color] LastColumn [color=darkblue]As[/color] [color=darkblue]Long[/color]
    [color=darkblue]Dim[/color] i [color=darkblue]As[/color] [color=darkblue]Long[/color]
    [color=darkblue]Dim[/color] j [color=darkblue]As[/color] [color=darkblue]Long[/color]

    strDestPath = ActiveWorkbook.Path
    
    [color=darkblue]With[/color] ActiveSheet.UsedRange
        LastRow = .Rows.Count + .Rows(1).Row - 1
        LastColumn = .Columns.Count + .Columns(1).Column - 1
    [color=darkblue]End[/color] [color=darkblue]With[/color]
    
    [color=darkblue]If[/color] LastRow = 1 [color=darkblue]Then[/color]
        MsgBox "No data is available...", vbExclamation
        [color=darkblue]Exit[/color] [color=darkblue]Sub[/color]
    [color=darkblue]End[/color] [color=darkblue]If[/color]
    
    [color=darkblue]For[/color] i = 2 [color=darkblue]To[/color] LastRow
        [color=darkblue]Open[/color] strDestPath & "\" & Cells(i, "A").Value & ".csv" [color=darkblue]For[/color] [color=darkblue]Output[/color] [color=darkblue]As[/color] #1
        [color=darkblue]For[/color] j = 1 [color=darkblue]To[/color] LastColumn
            [color=darkblue]If[/color] j <> LastColumn [color=darkblue]Then[/color]
                [color=darkblue]Write[/color] #1, Cells(i, j).Text;
            [color=darkblue]Else[/color]
                [color=darkblue]Write[/color] #1, Cells(i, j).Text
            [color=darkblue]End[/color] [color=darkblue]If[/color]
        [color=darkblue]Next[/color] j
        [color=darkblue]Close[/color] #1
    [color=darkblue]Next[/color] i
    
    MsgBox "Completed...", vbInformation
    
[color=darkblue]End[/color] [color=darkblue]Sub[/color]
[/font]
 
Upvote 0
thank you so much domenic for your solution. it saved me alot of hours of copying and pasting.
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,728
Members
452,939
Latest member
WCrawford

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