Save each row from a range as separated CSV file via VBA at once

Jorgi

Board Regular
Joined
Jul 7, 2021
Messages
52
Office Version
  1. 2019
Platform
  1. Windows
Hello,
I'm trying to find a macro where each row in column from a range will be saved with the same header as 1 separated CSV file in the current folder. Let's say if I have header in A1 and data in A2 - A5 I will need to save 4 CSV files at once with the same header from cell A1. The name of each file will be part of the data from each row e.g. ABC,EFG, etc. Ideally if the files can be saved in the same/current folder. Thank you so much if anyone can help.

CSV per row.PNG
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Hello, a VBA demonstration for starters :​
VBA Code:
Sub Demo1()
    Dim F%, V, R&
        F = FreeFile
        V = [A1].CurrentRegion.Value2
    For R = 2 To UBound(V)
        Open ThisWorkbook.Path & "\" & Split(V(R, 1), ";")(0) & " .csv" For Output As #F
        Print #F, V(1, 1)
        Print #F, V(R, 1);
        Close #F
    Next
End Sub
 
Upvote 0
Solution
Hello Marc L Thank you very much for you help. I'm not good with VBA just slowly starting to learn the basics . When I tried to run this macro there was Run-Time Error '52'. I think that I'm doing something wrong. Thank you for any hint.
 
Upvote 0
As this way works a treat on my side since last century so close Excel, retry and in case of the error remains​
so well describe the error message and far above all on which codeline this error raises​
as, after all, we are not on some mind readers forum … ;)
 
Upvote 0
Marc L it says Run Time Error '52' Bad file or number and the line highlighted in yellow. Hope I didn't mess up anything. Thank you for your help
Capture1.PNG
 
Upvote 0
Maybe it's a fresh new workbook not yet saved so no path and as some OS does not like when saving to the root​
then it just needs to save first the workbook or to hardcode the drive and the main folder instead of ThisWorbook.Path …​
If it's not the case then just add this codeline before : Debug.Print ThisWorkbook.Path & "\" & Split(V(R, 1), ";")(0) & " .csv"
then revert the result displayed in the VBE Immediate window.​
 
Upvote 0
Marc L you are STAR it works thank you so much!
 
Upvote 0

Forum statistics

Threads
1,214,789
Messages
6,121,593
Members
449,038
Latest member
Arbind kumar

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