+ Reply to Thread
Results 1 to 3 of 3

To convert multiple text files to .csv

  1. #1
    Registered User
    Join Date
    07-16-2014
    Location
    texas
    MS-Off Ver
    2010
    Posts
    2

    To convert multiple text files to .csv

    Below code Works for TAB delimeter but if there is ' ,' in a row its not reading correct data.Basically I am trying to read multiple .txt files to .csv .Any help appreciated...Thank you...


    Sub txt2csv()
    Dim Fname As String, ipath As String, retstring, fs, a, i As Long
    With Application.FileDialog(msoFileDialogFolderPicker)
    .AllowMultiSelect = False
    If .Show = -1 Then ipath = .SelectedItems(1) Else Exit Sub
    End With
    Application.ScreenUpdating = False: On Error Resume Next
    Fname = Dir(ipath & "\*.txt")
    Do While Fname <> ""
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set a = fs.OpenTextFile(ipath & "\" & Fname, 1, 0)


    'retstring = Replace(a.readall, vbTab, " " & Replace(Fname, ".txt", "") & "," & vbTab) & "," & Replace(Fname, ".txt", "") & ",": a.Close

    retstring = Replace(a.readall, vbTab, " " & Replace("", ".txt", "") & "," & "") & "," & Replace(Fname, ".txt", "") & ",": a.Close





    If Err.Number = 0 Then
    Open ipath & "\" & Fname For Output As #1
    Print #1, retstring
    Close #1
    Name ipath & "\" & Fname As ipath & "\" & Replace(Fname, "txt", "csv")
    Else
    Err.Clear
    End If
    Fname = Dir
    Loop
    Set fs = Nothing: Application.ScreenUpdating = False
    End Sub

  2. #2
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    16,033

    Re: To convert multiple text files to .csv

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here



    (This thread should receive no further responses until this moderation request is fulfilled, as per Forum Rule 7)
    Never use Merged Cells in Excel

  3. #3
    Registered User
    Join Date
    07-16-2014
    Location
    texas
    MS-Off Ver
    2010
    Posts
    2

    Re: To convert multiple text files to .csv

    Hello Team,

    I am trying to convert multiple .txt file having tab delimited to .csv file. However the code that I am using is also considering ',' as delimited in addition to 'tab'. I have posted the code below and it would be great help if somebody could help me rectify the below code to accept only tab as a delimiter.

    Thanks

    #
    Please Login or Register  to view this content.
    Last edited by Prat01; 07-17-2014 at 09:03 AM.

+ 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. Convert Multiple text files to excel files
    By shanmugapriya.a in forum Excel General
    Replies: 3
    Last Post: 07-07-2015, 10:39 AM
  2. IMPORTING TEXT FILES TO EXCELL - Multiple Multi line text files
    By Toxicca in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 02-12-2014, 09:56 AM
  3. Replies: 5
    Last Post: 11-19-2013, 04:11 PM
  4. [SOLVED] how to convert multiple .txt files to .xlsx files using a macro?
    By Aaron_Tram in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-21-2013, 06:30 PM
  5. Convert Multiple CSV Files to XLS Files Automatically
    By davehunter in forum Excel General
    Replies: 0
    Last Post: 07-11-2007, 05:42 AM

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