How to make macros that can copy the content of 100 txt files into 1 excel sheet

Joined
Mar 3, 2011
Messages
2
Hi guys,

Please help me make an excel macro that can process all the txt files in the same folder. I want to open them all and copy the content into 1 excel sheet. The filenames are different and variable. The txt files should be tab delimited.

sample filename: Paris 110303 ADJ.txt

Appreciate your immediate response.

Regards,

James
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Thanks for the quick reply. I tried to copy the script but it gives me an error dialog box saying "run time error 429 Active x cant create object

Sub ImportTextFile()
Dim DestBook As Workbook, SourceBook As Workbook
Dim DestCell As Range
Dim RetVal As Boolean
Application.ScreenUpdating = False
Set DestBook = ActiveWorkbook
Set DestCell = ActiveCell

RetVal = Application.Dialogs(xlDialogOpen).Show("*.txt")

Do While RetVal = True
Set SourceBook = ActiveWorkbook
Range(Range("A1"), Range("A1").SpecialCells(xlLastCell)).Copy
DestBook.Activate
DestCell.PasteSpecial Paste:=xlValues
SourceBook.Close False
Set DestCell = Cells(DestCell.Row + 1, DestCell.Column)
RetVal = Application.Dialogs(xlDialogOpen).Show("*.txt")
Loop
Application.ScreenUpdating = True
End Sub


this is the script i used
 
Upvote 0
Sorry to hear that. Try this instead:

Code:
Sub ImportTextFile()
    Dim DestBookName, SourceBookName
    Dim RetVal As Boolean
    Application.ScreenUpdating = False
    
    DestBookName = ActiveWorkbook.Name    
    RetVal = Application.Dialogs(xlDialogOpen).Show("*.txt")
    
    Do While RetVal = True
        SourceBookName = ActiveWorkbook.Name
        ActiveSheet.UsedRange.Copy
        Workbooks(DestBookName).Activate
        Cells(ActiveSheet.UsedRange.Rows.Count + 1, 1).Select
        ActiveSheet.Paste
        Workbooks(SourceBookName).Close False
        RetVal = Application.Dialogs(xlDialogOpen).Show("*.txt")
    Loop
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,552
Messages
6,179,484
Members
452,917
Latest member
MrsMSalt

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top