Importing Text files into excel macro

excel_2009

Active Member
Joined
Sep 14, 2009
Messages
318
Hi excel gurus,

i stumbled across the following coding:

Code:
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")

  
    If RetVal = False Then Exit Sub
   
    Set SourceBook = ActiveWorkbook
 
    Range(Range("A1"), Range("A1").SpecialCells(xlLastCell)).Copy
   
    DestBook.Activate
    DestCell.PasteSpecial Paste:=xlValues

    SourceBook.Close False

End Sub

The coding allows me to import a text file sucessfully into an excel sheet, however i'd like to somehow adapt the coding so that multiple text files can be imported, is that possible? :)

Thank you
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Hi Andrew,

Thank you so much it's perfect! just one question though, is it at all possible to insert the text files into one sheet as opposed to multiple sheets? :)
 
Upvote 0
An alternative, using a slight modification to the code you supplied:

Code:
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

Each text file will go to a new row of the same sheet. To stop importing files, click Cancel in the Dialog Box.
 
Upvote 0

Forum statistics

Threads
1,224,616
Messages
6,179,908
Members
452,949
Latest member
beartooth91

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