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

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
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,223,227
Messages
6,170,849
Members
452,361
Latest member
d3ad3y3

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