Hi - I am a total beginner so was very interested to see a solution to my requirement, including "for beginner" instructions in the thread at
http://www.excelforum.com/excel-prog...html?p=3107472 .
Code provided there is:
Sub ImportTextFilesMacro_rapidasia()
'Change the folder path as needed
Const strFolderPath As String = "C:\Test"
'Declare variables
Dim FSO As Object 'Used to access the Windows FileSystem
Dim strFileName As String 'Used to store the filename
Dim arrText(1 To 65000) As String 'Used to store the .txt file's text
Dim TextIndex As Long 'Used by arrText for storing the text in the correct location
'Access the Windows FileSystem
Set FSO = CreateObject("Scripting.FileSystemObject")
'Use the folderpath to find all the first .txt file in that folder
strFileName = Dir(strFolderPath & "\*.txt")
'Loop through all the text files
Do While Len(strFileName) > 0
'Found a text file, increase the TextIndex
TextIndex = TextIndex + 1
'Use the Windows FileSystem to read the text file and store that information in arrText
arrText(TextIndex) = FSO.OpenTextFile(strFolderPath & "\" & strFileName).ReadAll
strFileName = Dir 'Advance the loop
Loop
'If at least 1 text file was found, output the results in column C
If TextIndex > 0 Then Range("C1").Resize(TextIndex).Value = Application.Transpose(arrText)
'Cleanup objects and arrays
Set FSO = Nothing
Erase arrText
End Sub
I copied the listing and pasted it in, every time I try to run it I get
Run-time error '1004':
Application-defined or object-defined error
in the debug screen a block of text is highlit yellow, it is not liking the text between the **'s:
If TextIndex > 0 Then **Range("C1").Resize(TextIndex).Value = Application.Transpose(arrText)**
i.e. flagged yellow in this section:
Range("C1").Resize(TextIndex).Value = Application.Transpose(arrText)
I am running Excel 2003, if that makes a difference.
My fingers are crossed that someone can point me in the right direction to fix this.
Andrew
Bookmarks