Hi, I need to import 20,000 txt files into excel with the corresponding filenames. Each txt file should only take one cell, likewise the filename.
I came very close to solving this by copying Leith's code from this thread:
http://www.excelforum.com/excel-prog...worksheet.html
and modifying it as follows (just replaced Celldata with FileName in column B)
But it only takes the first line of the txt file. I require all the content to be placed into one cell.Sub ReadTextFiles()
Dim CellData As String
Dim FileName As String
Dim FilePath As String
Dim LineCnt As Long
Dim R As Long
Dim Text As String
Dim Wks As Worksheet
R = 1
Set Wks = Worksheets("Sheet1")
FilePath = "C:\Users\Platinum\Desktop\ExcelMacro"
FileName = Dir(FilePath & "\*.txt")
Do While FileName <> ""
LineCnt = 0
CellData = ""
N = FreeFile
Open FilePath & "\" & FileName For Input As #N
Do While Not EOF(N)
Line Input #N, Text
LineCnt = LineCnt + 1
If Text = "" Then Text = " " & vbLf
If LineCnt = 1 Then
Wks.Cells(R, "A").Value = Text
Else
CellData = CellData & Text
End If
Loop
Wks.Cells(R, "B").Value = FileName
Close #N
R = R + 1
FileName = Dir()
Loop
End Sub
Many thanks in advance, i've tried so hard to do this on my own, but i just dont have the knowledge!
![]()
Bookmarks