Try this instead. The code worked OK on my machine before but now I've changed it to specify a text-tab delimited file (rather than let Excel guess what it is). This brings all the text files onto one sheet.
Let me know how you get on,
Dan
Code:Sub GetTextFiles() Dim lngCounter As Long, wbText As Workbook On Error GoTo ErrHandler Application.DisplayAlerts = False Application.ScreenUpdating = False With Application.FileSearch .NewSearch .FileType = msoFileTypeAllFiles .LookIn = "C:temptext files" 'Change this to your folder name .Execute For lngCounter = 1 To .FoundFiles.Count If Right(.FoundFiles(lngCounter), 4) = ".txt" Then Workbooks.OpenText Filename:=.FoundFiles(lngCounter), tab:=True, DataType:=xlDelimited ActiveSheet.UsedRange.Copy ActiveWorkbook.Close False Range("A" & ActiveSheet.UsedRange.Rows.Count + 1).PasteSpecial End If Next lngCounter End With Application.DisplayAlerts = True Application.ScreenUpdating = True Exit Sub ErrHandler: Application.ScreenUpdating = True MsgBox Err.Description, vbExclamation, "Ooops, an error occurred" End Sub
Try this instead. The code worked OK on my machine before but now I've changed it to specify a text-tab delimited file (rather than let Excel guess what it is). This brings all the text files onto one sheet.
Let me know how you get on,
Dan
Code:Sub GetTextFiles() Dim lngCounter As Long, wbText As Workbook On Error GoTo ErrHandler Application.DisplayAlerts = False Application.ScreenUpdating = False With Application.FileSearch .NewSearch .FileType = msoFileTypeAllFiles .LookIn = "C:temptext files" 'Change this to your folder name .Execute For lngCounter = 1 To .FoundFiles.Count If Right(.FoundFiles(lngCounter), 4) = ".txt" Then Workbooks.OpenText Filename:=.FoundFiles(lngCounter), tab:=True, DataType:=xlDelimited ActiveSheet.UsedRange.Copy ActiveWorkbook.Close False Range("A" & ActiveSheet.UsedRange.Rows.Count + 1).PasteSpecial End If Next lngCounter End With Application.DisplayAlerts = True Application.ScreenUpdating = True Exit Sub ErrHandler: Application.ScreenUpdating = True MsgBox Err.Description, vbExclamation, "Ooops, an error occurred" End Sub