+ Reply to Thread
Results 1 to 5 of 5

Batch convert .txt files to .xls

Hybrid View

  1. #1
    Registered User
    Join Date
    12-17-2019
    Location
    Leeds, UK
    MS-Off Ver
    365
    Posts
    63

    Question Batch convert .txt files to .xls

    Hi all,

    I have 48 text files that need converting to Excel.
    The obvious answer found in most forums uses the following code:

    Sub TXTconvertXLS()
    
    Dim wb As Workbook
    Dim strFile As String
    Dim strDir As String
    
    'Directories
    strDir = "\\xx\xx\xx\xx\Desktop\SignalLogs\"
    strFile = Dir(strDir & "*.txt")
    
    'Loop
    Do While strFile <> ""
        Set wb = Workbooks.Open(strDir & strFile)
            With wb
                .SaveAs Replace(wb.FullName, ".txt", ".xls"), 50
                .Close True
            End With
        Set wb = Nothing
    Loop
    
    End Sub
    The problem is this converts the text from the files in a single column of each excel workbook as opposed to distributed into columns.
    Excel's Text Wizard sorts this problem for manual conversions, so using the macro recorder, I've identified a code for manual, correct conversion as :

    Sub Macro1()
    
        ChDir "C:\XXXX\XXXX\Desktop\SignalLogs"
        Workbooks.OpenText Filename:= _
            "C:\XXXX\XXXX\Desktop\SignalLogs\WR2022092708M37.TXT", Origin:=xlMSDOS, _
            StartRow:=1, DataType:=xlFixedWidth, FieldInfo:=Array(Array(0, 1), Array(8, _
            1), Array(17, 1), Array(21, 1), Array(29, 1), Array(40, 1), Array(44, 1), Array(52, 1), _
            Array(57, 1)), TrailingMinusNumbers:=True
    End Sub
    Can someone please suggest a code which combines the two and allows me to convert all 48 .txt files into correctly formatted Excel files, please?

    Thanks!!
    Attached Files Attached Files

  2. #2
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,021

    Re: Batch convert .txt files to .xls

    Untested here :

    Sub LoopAllFiles()
        Dim sPath As String, sDir As String
        sPath = "C:\work\"
        If Right(sPath, 1) <> "\" Then sPath = sPath & "\"
        sDir = Dir$(sPath & "*.txt", vbNormal)
        Do Until Len(sDir) = 0
            Workbooks.Open (sPath & sDir)
            With ActiveWorkbook
                Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
                    TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
                    Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _
                    :="|", FieldInfo:=Array(1, 1), TrailingMinusNumbers:=True
                .SaveAs Filename:=Left(.FullName, InStrRev(.FullName, ".")) & "xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
                .Close
            End With
            sDir = Dir$
        Loop
    End Sub

    https://stackoverflow.com/questions/...umns-using-vba

  3. #3
    Registered User
    Join Date
    12-17-2019
    Location
    Leeds, UK
    MS-Off Ver
    365
    Posts
    63

    Re: Batch convert .txt files to .xls

    Thanks, but this still has the same problem. The excel files import all the .txt data into the 1st column of the excel files..
    Attached is an example of the .txt files I'm working with
    Attached Files Attached Files

  4. #4
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Exclamation Re: Batch convert .txt files to .xls


    Hi,

    'cause the file does very not match the data type expected by the VBA procedure !

    So just activate the Macro Recorder and import manually the file in order to get a code base
    or just reading the VBA help according to your actual VBA procedure ...

    Edit : as it seems you already have a working procedure in your initial post so just combine it with post #2.
    Last edited by Marc L; 10-20-2022 at 12:31 PM.

  5. #5
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,021

    Re: Batch convert .txt files to .xls

    Here is a similar macro to yours that will distribute the data to different columns.
    I agree with Marc L, you already have a macro to do what you are seeking ... why not use it ?

    Sub DistributeData()
    
        Range("A1").Select
        ActiveSheet.Paste
        Selection.TextToColumns Destination:=Range("A1"), DataType:=xlFixedWidth, _
            OtherChar:="|", FieldInfo:=Array(Array(0, 1), Array(8, 1), Array(17, 1), Array(21 _
            , 1), Array(30, 1), Array(41, 1), Array(44, 1), Array(53, 1), Array(58, 1)), _
            TrailingMinusNumbers:=True
    End Sub

+ 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] Batch convert .docx to .txt
    By Cunner in forum Word Programming / VBA / Macros
    Replies: 8
    Last Post: 10-21-2021, 05:26 AM
  2. Named Parameter error (Trying to batch save as xlsx files as xml data files)
    By dzheng.328 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-13-2019, 03:22 PM
  3. [SOLVED] Batch convert all excel files in a folder (and its subfolders) to pdf
    By billj in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-23-2019, 05:12 PM
  4. [SOLVED] Batch Convert XLSX to XLS
    By Ernching in forum Excel General
    Replies: 3
    Last Post: 08-22-2015, 11:46 AM
  5. [SOLVED] Batch convert data in multiple files (Meters to Feet)
    By chris45 in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 12-11-2013, 12:16 PM
  6. Replies: 1
    Last Post: 11-12-2013, 03:44 PM
  7. [SOLVED] Batch conversion of XP files to 97 files
    By AG in forum Excel General
    Replies: 2
    Last Post: 10-02-2005, 10:05 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