I need to import text from several text files into successive columns in a single XLS worksheet. Thread707-1444974 of this forum provided a solution for opening many text files and saving them in separate xls Workbook files. I modified that code below to enable it to use part of the file path as the single output file name that will be used to save the concatonation of the contents of many text files, but I have been unable to figure out how to insert the contents of multiple text files into successive columns of a single worksheet. The formatting of the contents of my text files is such that my code below appropriately places all of the imported text into a Column A. I would like the text from the second .txt file to go into Column B of the same worksheet, the text from the third .txt file to go into Column C, etc. (but not into separate worksheets in separate workbooks). The top cell used to paste the contents of each of the text files should begin in the same row of the single Excel worksheet.
The problem is with the following code:
Workbooks.OpenText FileName:=FileName(i), StartRow:=73, DataType:=xlDelimited, Tab:=True
How can I change my code to fix this problem?
Thank you,
murphyw
------------------------------------------
Option Explicit
Sub Text_file_convert()
Dim Filter As String, Title As String, msg As String
Dim i As Integer, FilterIndex As Integer
Dim FileName As Variant
Dim Path As String
Dim Drive As String
Dim OutputFile() As String
' File filters
Filter = "Text Files (*.txt),*.txt,"
' Set default filter to *.txt
FilterIndex = 1
' Set the title of the dialog
Title = "Choose the files you want to open"
' Choose the drive, path, and output file prefix
Path = ThisWorkbook.Path
OutputFile = Split(Path, "\")
Drive = Left(Path, 1)
ChDrive (Drive)
ChDir (Path)
With Application
' Set the array of file names to the selected filenames (allow multiple)
FileName = .GetOpenFilename(Filter, FilterIndex, Title, , True)
' Reset the initial drive / path
ChDrive (Left(.DefaultFilePath, 1))
ChDir (.DefaultFilePath)
End With
' Exit when cancelled
If Not IsArray(FileName) Then
MsgBox "No file was selected."
Exit Sub
End If
' Open files
For i = LBound(FileName) To UBound(FileName)
msg = msg & FileName(i) & vbCrLf
Workbooks.OpenText FileName:=FileName(i), StartRow:=73, DataType:=xlDelimited, Tab:=True
' ActiveWorkbook.SaveAs Replace$(FileName(i), ".txt", ".xls"), xlWorkbookNormal
' ActiveWorkbook.Close
Next i
MsgBox msg, vbInformation, "Converted " & i - 1 & " files"
ChDir (Path)
ActiveWorkbook.SaveAs (OutputFile(UBound(OutputFile)) & ".xls"), xlWorkbookNormal
ActiveWorkbook.Close
End Sub
The problem is with the following code:
Workbooks.OpenText FileName:=FileName(i), StartRow:=73, DataType:=xlDelimited, Tab:=True
How can I change my code to fix this problem?
Thank you,
murphyw
------------------------------------------
Option Explicit
Sub Text_file_convert()
Dim Filter As String, Title As String, msg As String
Dim i As Integer, FilterIndex As Integer
Dim FileName As Variant
Dim Path As String
Dim Drive As String
Dim OutputFile() As String
' File filters
Filter = "Text Files (*.txt),*.txt,"
' Set default filter to *.txt
FilterIndex = 1
' Set the title of the dialog
Title = "Choose the files you want to open"
' Choose the drive, path, and output file prefix
Path = ThisWorkbook.Path
OutputFile = Split(Path, "\")
Drive = Left(Path, 1)
ChDrive (Drive)
ChDir (Path)
With Application
' Set the array of file names to the selected filenames (allow multiple)
FileName = .GetOpenFilename(Filter, FilterIndex, Title, , True)
' Reset the initial drive / path
ChDrive (Left(.DefaultFilePath, 1))
ChDir (.DefaultFilePath)
End With
' Exit when cancelled
If Not IsArray(FileName) Then
MsgBox "No file was selected."
Exit Sub
End If
' Open files
For i = LBound(FileName) To UBound(FileName)
msg = msg & FileName(i) & vbCrLf
Workbooks.OpenText FileName:=FileName(i), StartRow:=73, DataType:=xlDelimited, Tab:=True
' ActiveWorkbook.SaveAs Replace$(FileName(i), ".txt", ".xls"), xlWorkbookNormal
' ActiveWorkbook.Close
Next i
MsgBox msg, vbInformation, "Converted " & i - 1 & " files"
ChDir (Path)
ActiveWorkbook.SaveAs (OutputFile(UBound(OutputFile)) & ".xls"), xlWorkbookNormal
ActiveWorkbook.Close
End Sub