Results 1 to 5 of 5

Batch convert .txt files to .xls

Threaded View

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

    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

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. 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