Macro to loop thru folder and convert txt to xlsx

mxman

New Member
Joined
Jun 8, 2006
Messages
46
Hi, I am having trouble to loop thru a folder of txt and xlsx files; select the txt files (one by one), convert them to xlsx file (and maintaining the original txt file); save it in same path and with same filename (as when it was txt file, but .xlsx); Note:- must maintain the original txt file); In xlsx file convert text to columns (separator is a space)(applies to column 1); I then run some other calculations and data manipulation (macros), save it; then select next txt file in folder and repeat.
Your assistance will be appreciated.
My code is attached below:-

Sub Test24()
' Create a list of all files in the folder (create an excel list)
' 1st create the excel file
Workbooks.Add
ActiveWorkbook.SaveAs ("C:\Users\Trevor\Documents\Testing\1998\Summary File List.xlsx")
' FileList.Title = "Summary_File_List"
' FileList.SaveAs = ("Summary_File_List.xlsx")
' 2nd create the summary list in the new file
F = Dir("C:\Users\Trevor\Documents\Testing\1998\*.txt")
Do While Len(F) > 0
ActiveCell.Formula = F
ActiveCell.Offset(1, 0).Select
F = Dir()
Loop
ActiveWorkbook.Save
' Now use this summary list to loop through
'Loop through the files in the selected folder
Dim FF As String
FF = Dir("C:\Users\Trevor\Documents\Testing\1998\*.txt")
Do While FF <> ""
' Opens txt file in excel
Workbooks.Open ("C:\Users\Trevor\Documents\Testing\1998\" & FF)
' Saves file as xlsx
ActiveWorkbook.SaveAs "C:\Users\Trevor\Documents\Testing\1998\" & Left(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 4), FileFormat:=52
TextDataFile = ActiveWorkbook.Name
' Do not display the message about overwriting the existing file.
Application.DisplayAlerts = False
' Save the active workbook with the name of the active workbook.
ActiveWorkbook.SaveAs Filename:="C:\Users\Trevor\Documents\Testing\1998" & TextDataFile
' Close the workbook by using the following.
ActiveWorkbook.Close
Loop
End Sub
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.

Forum statistics

Threads
1,215,892
Messages
6,127,611
Members
449,389
Latest member
ChessManNaill

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top