Macro to convert TXT to CSV files

neobavesten

Board Regular
Joined
Oct 15, 2009
Messages
94
Hi,

I have a bulk of TXT files organized in same format. Is the a way to write a macro that could convert those text files to *csv files?

Text file contains multiple sentences. I would like to keep that text as one block in CSV file.

I would really appreciate if someone could help me with this.
Many thanks!
 
add this in the userform code area as well...


Code:
Sub DoConvert()
    Application.Cursor = xlWait
    Open SelFolder & "Combined Files.txt" For Output As #2
    For row = 0 To ListBox1.ListCount - 1
        Open SelFolder & ListBox1.List(row) For Input As #1
        res=""
        while not eof(3)
            line input #3,sdata
            if len(sdata)>18 then
                res=res & chr(9) & sdata
            end if
        wend
        print #1,res
    next row
    close
end sub
 
Upvote 0

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Many thanks for your effort. Well this is getting more and more complex.
After running macro I select the folder that contains the text files but right after that get an error: "User-defined type not defined"

It points to this part of code:
Code:
Function AllFiles(ByVal FullPath As String) As String()
    Dim oFs As New FileSystemObject
 
Upvote 0
in the VBE goto tools>references and enable microsoft scripting runtime
 
Upvote 0
Done. After compiling I get an error: "Only comments may appear after End Sub,End Function, or end Property".
It directs to this part of the code:
Code:
ErrHandler:
    Set oFs = Nothing
    Set oFolder = Nothing
    Set oFile = Nothing
End Function
End Function
 
Upvote 0
youve got 'end function' twice. delete one
 
Upvote 0
Now I get Run-time error '424' Object required. It points to:

Code:
If Right(sFiles(row), 4) = Right(ComboBox1.List(ComboBox1.ListIndex, 1), 4) Then
 
Upvote 0
sorry about that. this is part of a larger project which allows selection of different file types.

Code:
    If Right(sFiles(row), 4) = ".txt" Then

ill check through for other problems...
 
Upvote 0
Many thanks Diddi!!!

Now it load files. Results ar shown in user form (number of loaded files). It stops there. Is there anything else I need to do?
 
Upvote 0
so there is your list of files!

now if you click the other commandbutton, it should process them and combine all files into 1 result called 'combined files' on C:\ and whats more it should be CSV
 
Upvote 0
put this in as well

Code:
Sub userform_initialize()
    commandbutton2.visible=false
end sub
 
Upvote 0

Forum statistics

Threads
1,223,671
Messages
6,173,732
Members
452,529
Latest member
jpaxonreyes

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