Convert CSV files to TXT using VBA

Sharda

New Member
Joined
Nov 19, 2017
Messages
15
I need to convert these CSV files to TXT, I succeeded with below code.
However need help with two things:
1) This code should be applied to all CVS files in given path. Because It gave result on only given number.
2) I want on TXT top line as: LEDES98BI V2[] instead of LEDES98BI V2[]|||||||||||||||||||||||||||||||||||||||||||||||||||

Code:
Sub csv2mat()
Dim Filename As String
Dim Filenamenew As String
Dim str As String
'Change the path to the Files and and create a txt file
Filename = "C:\Users\Rimpi Pihuja\Desktop\New folder (2)\New folder\New folder\1040448.csv"
Filenamenew = "C:\Users\Rimpi Pihuja\Desktop\New folder (2)\New folder\New folder\1040448.txt"
Open Filenamenew For Output As [URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=2"]#2[/URL] 
Open Filename For Input As [URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1"]#1[/URL] 
Do While Not EOF(1)
    Line Input [URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1"]#1[/URL] , str
    str = Replace(str, ",", "|")
    Print [URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=2"]#2[/URL] , str
Loop
Close [URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1"]#1[/URL] 
Close [URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=2"]#2[/URL] 
End Sub
 
If the code is running with no error but no conversion then I suspect it is reaching Line Do While MyFile <> "" and there is no filename in "MyFile" therefore the code stops.

Have you tried putting a break point in your code on that line and hovering your mouse over the MyFile variable to see what it's value is?
 
Upvote 0

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
When I hovered mouse over "FilePath =" it reflects as "FilePath = Empty". However there are files in given path. I fail to understand what I'm missing.
 
Upvote 0
1) File path is incorrect. I note your path is excessively convoluted. You only need 1 tiny mistake. Try copying the path from the code and then searching in file explorer - does it open that path?

or

2) There are no *.csv files in that folder
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,194
Members
453,021
Latest member
pingpong7117

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