+ Reply to Thread
Results 1 to 15 of 15

Convert multiple txt files in folder to csv

Hybrid View

tom_b888 Convert multiple txt files in... 12-29-2017, 05:40 AM
than_gold Re: Convert multiple txt... 12-29-2017, 05:56 AM
tom_b888 Re: Convert multiple txt... 12-29-2017, 06:16 AM
than_gold Re: Convert multiple txt... 12-29-2017, 06:26 AM
tom_b888 Re: Convert multiple txt... 12-29-2017, 06:32 AM
than_gold Re: Convert multiple txt... 12-29-2017, 06:37 AM
tom_b888 Re: Convert multiple txt... 12-29-2017, 07:39 AM
AB33 Re: Convert multiple txt... 12-29-2017, 07:44 AM
than_gold Re: Convert multiple txt... 12-29-2017, 07:45 AM
AB33 Re: Convert multiple txt... 12-29-2017, 08:00 AM
jindon Re: Convert multiple txt... 12-29-2017, 08:05 AM
tom_b888 Re: Convert multiple txt... 12-29-2017, 08:18 AM
jindon Re: Convert multiple txt... 12-29-2017, 08:44 AM
tom_b888 Re: Convert multiple txt... 12-29-2017, 08:54 AM
djamieleonard Re: Convert multiple txt... 10-18-2022, 11:18 PM
  1. #1
    Registered User
    Join Date
    03-12-2015
    Location
    London
    MS-Off Ver
    2010
    Posts
    8

    Convert multiple txt files in folder to csv

    Hi

    I wonder if someone might be able to help.

    I am trying to find out if it is possible to batch/bulk convert a high volume of comma delimited txt files into individual csv (comma delimited) files and retain the original filename for each and save them in the same folder. I basically want a script to carry out the same 'text to column' feature within Excel and then save the file with the same filename and it doesn't' matter whether there is one file in the folder or 100, it will loop until there are no more txt files to convert.

    Each file row and column length will be variable, the constant will be that they are always comma delimited.

    I have been searching through this forum and can kind similar vb scripts but these don't convert all files within a folder and I cannot find anything that will really help, but am sorry if there is something similar that I haven't found yet.

    Many thanks

  2. #2
    Valued Forum Contributor than_gold's Avatar
    Join Date
    10-17-2017
    Location
    Coimbatore India
    MS-Off Ver
    Office 365
    Posts
    646

    Re: Convert multiple txt files in folder to csv

    Try this:

    Dim oFSO, myFolder
    Dim xlCSV
    
    myFolder="C:\your\path\to\excelfiles\"
    
    
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    xlCSV = 6 'Excel CSV format enum
    Call ConvertAllExcelFiles(myFolder)
    Set oFSO = Nothing
    
    Call MsgBox ("Done!")
    
    
    Sub ConvertAllExcelFiles(ByVal oFolder)
    Dim targetF, oFileList, oFile
    Dim oExcel, oWB, oWSH
    
        Set oExcel = CreateObject("Excel.Application")
        oExcel.DisplayAlerts = False
        Set targetF = oFSO.GetFolder(oFolder)
        Set oFileList = targetF.Files
        For Each oFile in oFileList
            If (Right(oFile.Name, 4) = "xlsx") Then
                Set oWB = oExcel.Workbooks.Open(oFile.Path)
                For Each oWSH in oWB.Sheets
                    Call oWSH.SaveAs (oFile.Path & oWSH.Name & ".csv", xlCSV)
                Next
                Set oWSH = Nothing
                Call oWB.Close
                Set oWB = Nothing
            End If
        Next
        Call oExcel.Quit
        Set oExcel = Nothing
    
    End Sub
    Regards,
    Thangavel D

    Appreciate the help? CLICK *

  3. #3
    Registered User
    Join Date
    03-12-2015
    Location
    London
    MS-Off Ver
    2010
    Posts
    8

    Re: Convert multiple txt files in folder to csv

    Thanks than_gold for your help, I have got a runtime error at....

    Set targetF = oFSO.GetFolder(oFolder)

    Any ideas?

  4. #4
    Valued Forum Contributor than_gold's Avatar
    Join Date
    10-17-2017
    Location
    Coimbatore India
    MS-Off Ver
    Office 365
    Posts
    646

    Re: Convert multiple txt files in folder to csv

    pls share the error screenshot.

  5. #5
    Registered User
    Join Date
    03-12-2015
    Location
    London
    MS-Off Ver
    2010
    Posts
    8

    Re: Convert multiple txt files in folder to csv

    Here you go!

    runetime424.jpg

    debug.jpg

  6. #6
    Valued Forum Contributor than_gold's Avatar
    Join Date
    10-17-2017
    Location
    Coimbatore India
    MS-Off Ver
    Office 365
    Posts
    646

    Re: Convert multiple txt files in folder to csv

    Please enable the highlighted ref.
    run time enable.PNG

  7. #7
    Registered User
    Join Date
    03-12-2015
    Location
    London
    MS-Off Ver
    2010
    Posts
    8

    Re: Convert multiple txt files in folder to csv

    I have set this to true but unfortunately the same error comes up

  8. #8
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Convert multiple txt files in folder to csv

    Dim oFSO should be dimmed as object. The same for all variables as assigned using "Set"

    Dim oFSO as object

  9. #9
    Valued Forum Contributor than_gold's Avatar
    Join Date
    10-17-2017
    Location
    Coimbatore India
    MS-Off Ver
    Office 365
    Posts
    646

    Re: Convert multiple txt files in folder to csv

    Try to update this:
    Dim oFSO As New FileSystemObject

  10. #10
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Convert multiple txt files in folder to csv

    It is going to be the same issue with all variables not dimmed as obejcts: Ftarget and etc.
    You will get an error if you tell VBA you are assigning an object by using Set and then variables are declared as "Variant"

  11. #11
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Convert multiple txt files in folder to csv

    If they are comma delimited text files...
    Sub test()
        Dim myDir As String, fn As String
        With Application.FileDialog(msoFileDialogFolderPicker)
            If .Show Then myDir = .SelectedItems(1) & "\"
        End With
        If myDir = "" Then Exit Sub
        fn = Dir(myDir & "*.txt")
        Do While fn <> ""
            Name myDir & fn As myDir & Replace(fn, ".txt", ".csv")
            fn = Dir
        Loop
    End Sub

  12. #12
    Registered User
    Join Date
    03-12-2015
    Location
    London
    MS-Off Ver
    2010
    Posts
    8

    Re: Convert multiple txt files in folder to csv

    That works!!!!

    I am going to test it but it seems to work perfectly.

    Thanks so much for your help

  13. #13
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Convert multiple txt files in folder to csv

    You are welcome and don't forget that all the text files in the folder must be comma delimited.
    The code will rename the extension regardless of the delimiter.

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

  14. #14
    Registered User
    Join Date
    03-12-2015
    Location
    London
    MS-Off Ver
    2010
    Posts
    8

    Re: Convert multiple txt files in folder to csv

    Yes, all the files will always be comma delimited.

    I will mark this as solved now, thanks again.

  15. #15
    Registered User
    Join Date
    10-18-2022
    Location
    USA
    MS-Off Ver
    office 365 A1
    Posts
    1

    Re: Convert multiple txt files in folder to csv

    I can't figure out how to delete my post but I figured it out
    Last edited by djamieleonard; 10-18-2022 at 11:28 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Automatically Convert Txt files in a folder to Excel
    By tommyfernandez in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 12-20-2016, 08:01 PM
  2. Convert csv files in folder to xlsm
    By YasserKhalil in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 11-13-2015, 09:49 PM
  3. Replies: 1
    Last Post: 03-12-2013, 04:45 AM
  4. Convert all .txt files to .xls in folder
    By natalie1230 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 10-19-2011, 03:50 AM
  5. Convert all txt files in Folder to csv
    By nms2130 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-07-2011, 11:14 AM
  6. Open rtf files from a folder and convert them to pdf
    By TANATOS in forum Word Programming / VBA / Macros
    Replies: 0
    Last Post: 07-08-2009, 01:03 PM
  7. Convert Folder of Workbooks to TXT Files
    By dvent in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-17-2009, 06:00 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1