Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to *quickly* convert many .txt files into .xls files

Tags:

excel

vba

Update: I just found out that someone with a more powerful server is going to work on the task I was assigned, so it's fine that I didn't make this program fast enough. However, the answer below (automating Excel) helped make the program three times faster, so I'd recommend it to someone with fewer (but still many) files.

I'm trying to convert many (over 300,000) .txt files into .xls files. I found out how to do it here:

Batch Convert TXT to XLS Using VBA

But it's really slow (in over an hour, it only converted ~200 our of 300,000 of the files), even though the files aren't that big.

I tried speeding it up by turning off ScreenUpdating, but I wasn't able to turn off ScreenUpdating successfully. Can someone explain where in the to turn off ScreenUpdating so that my code will run quicker? Or, better yet, any ideas for a more efficient program?

Here's the code:

Sub TXTconvertXLS()

'Variables
Dim wb As Workbook
Dim strFile As String
Dim strDir As String

 Application.ScreenUpdating = False
'Directories
strDir = 'path went here
strFile = Dir(strDir & "*.txt")

 Do While strFile <> ""

    Set wb = Workbooks.Open(strDir & strFile)
        With wb
            .SaveAs Replace(wb.FullName, ".txt", ".xls"), 50
            .Close False   '<-already saved in the line directly above
        End With
    Set wb = Nothing
    strFile = Dir   '<- stuffs the next filename into strFile
Loop
Application.ScreenUpdating = True
End Sub
like image 743
studiis Avatar asked Jan 12 '17 02:01

studiis


People also ask

How do I combine multiple textbooks into one Excel workbook?

Steps to Combine Data From Multiple Text Files to ExcelOpen a new workbook and go to Data tab > Get & Transform Data group > Get Data > From File > From Folder.

What is the fastest way to convert text to numbers in Excel?

Use Paste Special and Multiply Select the cells that have numbers stored as text. On the Home tab, click Paste > Paste Special. Click Multiply, and then click OK. Excel multiplies each cell by 1, and in doing so, converts the text to numbers.


1 Answers

A couple of options which should be quicker.

  1. Use Powershell (saves the code below in Notepad as say xx.ps1, update your source directory and run)
  2. Automate Excel in a hidden instance rather than in your current one.

Powershell

Drawing on https://superuser.com/questions/875831/using-powershell-is-it-possible-to-convert-an-xlsx-file-to-xls and Using Powershell to loop through Excel files and check if Spreadsheet name exists

$files = Get-ChildItem C:\Temp\*.txt
Write "Loading Files..."

$Excel = New-Object -ComObject Excel.Application
$Excel.visible = $false
$Excel.DisplayAlerts = $false

ForEach ($file in $files)
{

     $WorkBook = $Excel.Workbooks.Open($file.Fullname)
     $NewFilepath = $file.Fullname -replace ".{4}$"
     $NewFilepath =  $NewFilepath + ".xls"
     $Workbook.SaveAs($NewFilepath,56)   

}
  Stop-Process -processname EXCEL
  $Excel.Quit()

Automate Excel

Sub TXTconvertXLS2()

Dim objExcel As Excel.Application
Dim wb As Workbook
Dim strFile As String
Dim strDir As String

Set objExcel = New Excel.Application
With objExcel
    .Visible = False
    .DisplayAlerts = False
End With

    'Directories
    strDir = "c:\temp\"
    strFile = Dir(strDir & "*.txt")

    'Loop
     Do While strFile <> ""
        Set wb = objExcel.Workbooks.Open(strDir & strFile)
            With wb
                .SaveAs Replace(wb.FullName, ".txt", ".xls"), 50
                .Close False   '<-already saved in the line directly above
            End With
        Set wb = Nothing
        strFile = Dir   '<- stuffs the next filename into strFile
    Loop

objExcel.DisplayAlerts = False
objExcel.Quit
Set objExel = Nothing

End Sub
like image 102
brettdj Avatar answered Oct 17 '22 07:10

brettdj