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

Jorgi

New Member
Joined
Jul 7, 2021
Messages
41
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

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.

Marc L

Well-known Member
Joined
Apr 5, 2021
Messages
1,729
Office Version
  1. 2010
Platform
  1. Windows
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
 
Solution

Jorgi

New Member
Joined
Jul 7, 2021
Messages
41
Office Version
  1. 2019
Platform
  1. Windows
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.
 

Marc L

Well-known Member
Joined
Apr 5, 2021
Messages
1,729
Office Version
  1. 2010
Platform
  1. Windows
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 … ;)
 

Jorgi

New Member
Joined
Jul 7, 2021
Messages
41
Office Version
  1. 2019
Platform
  1. Windows

ADVERTISEMENT

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
 

Marc L

Well-known Member
Joined
Apr 5, 2021
Messages
1,729
Office Version
  1. 2010
Platform
  1. Windows
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.​
 

Jorgi

New Member
Joined
Jul 7, 2021
Messages
41
Office Version
  1. 2019
Platform
  1. Windows
Marc L you are STAR it works thank you so much!
 

Forum statistics

Threads
1,148,346
Messages
5,746,198
Members
423,998
Latest member
Nancy2021

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
Top