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!!
Bookmarks