Macro to add worksheet, get date from .txt file

wpryan

Well-known Member
Joined
May 26, 2009
Messages
534
Office Version
  1. 365
Platform
  1. Windows
Hi All,
I need to import some data from a .txt file into a new worksheet, in an existing workbook. I would like it such that a user can browse and select a file. I tried running the macro recorder, selecting Data > Get Data > From File > From Text/CSV, but it wants to create a data connection. That's not what I want. I just want to grab the data and put it in a worksheet.

...any help is appreciated...
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Hi wpryan,

Try this:

VBA Code:
Option Explicit
Sub Macro1()

    Application.ScreenUpdating = False
    
    Dim objFD As Object
    Dim strTextString As String
    Dim wbText As Workbook
    
    Application.ScreenUpdating = False
    
    Set objFD = Application.FileDialog(msoFileDialogFilePicker)
    With objFD
        .AllowMultiSelect = False
        .Title = "Please select the text file to import:"
        .InitialFileName = "C:\Path Containing Text Files\" 'Path containing the text files. Obviously change to suit.
        .Filters.Clear
        .Filters.Add "Text", "*.txt"
        If .Show = True Then
           Set wbText = Workbooks.Open(CStr(.SelectedItems(1)))
           wbText.Sheets(1).Cells.Copy ThisWorkbook.Sheets(1).Cells 'Copies thre data from the selected text file (can only have one worksheet) to the first (furthest left) tab in the active workbook. Change the later to suit if necessary.
           wbText.Close SaveChanges:=False
           Application.ScreenUpdating = True
           MsgBox Dir(CStr(.SelectedItems(1))) & " has been imported.", vbInformation
        Else
            Application.ScreenUpdating = True
            Exit Sub
        End If
    End With

End Sub

Regards,

Robert
 
Upvote 0
Solution
Hi wpryan,

Try this:

VBA Code:
Option Explicit
Sub Macro1()

    Application.ScreenUpdating = False
   
    Dim objFD As Object
    Dim strTextString As String
    Dim wbText As Workbook
   
    Application.ScreenUpdating = False
   
    Set objFD = Application.FileDialog(msoFileDialogFilePicker)
    With objFD
        .AllowMultiSelect = False
        .Title = "Please select the text file to import:"
        .InitialFileName = "C:\Path Containing Text Files\" 'Path containing the text files. Obviously change to suit.
        .Filters.Clear
        .Filters.Add "Text", "*.txt"
        If .Show = True Then
           Set wbText = Workbooks.Open(CStr(.SelectedItems(1)))
           wbText.Sheets(1).Cells.Copy ThisWorkbook.Sheets(1).Cells 'Copies thre data from the selected text file (can only have one worksheet) to the first (furthest left) tab in the active workbook. Change the later to suit if necessary.
           wbText.Close SaveChanges:=False
           Application.ScreenUpdating = True
           MsgBox Dir(CStr(.SelectedItems(1))) & " has been imported.", vbInformation
        Else
            Application.ScreenUpdating = True
            Exit Sub
        End If
    End With

End Sub

Regards,

Robert
Thanks for your help!
 
Upvote 0
Thanks again for the code. There is one tweak I would like to make, is to populate a cell with the name of the file imported (the files that will be imported contain the date of a particular test that was done, and it's important for later data analysis. Could anyone help with that...? Thanks in advance.
 
Upvote 0
Try this:

VBA Code:
Option Explicit
Sub Macro1()

    Application.ScreenUpdating = False
    
    Dim objFD As Object
    Dim strTextString As String, strFileName As String
    Dim wbText As Workbook
    
    Application.ScreenUpdating = False
    
    Set objFD = Application.FileDialog(msoFileDialogFilePicker)
    With objFD
        .AllowMultiSelect = False
        .Title = "Please select the text file to import:"
        .InitialFileName = "C:\Path Containing Text Files\" 'Path containing the text files. Obviously change to suit.
        .InitialFileName = "D:\Robert\MS_Office\Excel\Samples\"
        .Filters.Clear
        .Filters.Add "Text", "*.txt"
        If .Show = True Then
            strFileName = CStr(.SelectedItems(1))
            Set wbText = Workbooks.Open(CStr(.SelectedItems(1)))
            wbText.Sheets(1).Cells.Copy ThisWorkbook.Sheets(1).Cells 'Copies thre data from the selected text file (can only have one worksheet) to the first (furthest left) tab in the active workbook. Change the later to suit if necessary.
            wbText.Close SaveChanges:=False
            ThisWorkbook.Sheets("Sheet1").Range("A1").Value = strFileName 'Sheet name and cell reference for the file name. Change to suit.
            Application.ScreenUpdating = True
            MsgBox strFileName & " has been imported.", vbInformation
        Else
            Application.ScreenUpdating = True
            Exit Sub
        End If
    End With


End Sub
 
Upvote 0
Try this:

VBA Code:
Option Explicit
Sub Macro1()

    Application.ScreenUpdating = False
   
    Dim objFD As Object
    Dim strTextString As String, strFileName As String
    Dim wbText As Workbook
   
    Application.ScreenUpdating = False
   
    Set objFD = Application.FileDialog(msoFileDialogFilePicker)
    With objFD
        .AllowMultiSelect = False
        .Title = "Please select the text file to import:"
        .InitialFileName = "C:\Path Containing Text Files\" 'Path containing the text files. Obviously change to suit.
        .InitialFileName = "D:\Robert\MS_Office\Excel\Samples\"
        .Filters.Clear
        .Filters.Add "Text", "*.txt"
        If .Show = True Then
            strFileName = CStr(.SelectedItems(1))
            Set wbText = Workbooks.Open(CStr(.SelectedItems(1)))
            wbText.Sheets(1).Cells.Copy ThisWorkbook.Sheets(1).Cells 'Copies thre data from the selected text file (can only have one worksheet) to the first (furthest left) tab in the active workbook. Change the later to suit if necessary.
            wbText.Close SaveChanges:=False
            ThisWorkbook.Sheets("Sheet1").Range("A1").Value = strFileName 'Sheet name and cell reference for the file name. Change to suit.
            Application.ScreenUpdating = True
            MsgBox strFileName & " has been imported.", vbInformation
        Else
            Application.ScreenUpdating = True
            Exit Sub
        End If
    End With


End Sub
Thanks so much for your help, and I hate to be a bother... The
strFileName = CStr(.SelectedItems(1))
line returns the entire path. I would like to show only the filename (not path). How can that be done?
 
Upvote 0
Change the line to this:

VBA Code:
strFileName = CStr(Dir(.SelectedItems(1)))

Note you can also delete this line...

VBA Code:
.InitialFileName = "D:\Robert\MS_Office\Excel\Samples\"

...as that was just me doing some extra testing.

Regards,

Robert
 
Upvote 0
Change the line to this:

VBA Code:
strFileName = CStr(Dir(.SelectedItems(1)))

Note you can also delete this line...

VBA Code:
.InitialFileName = "D:\Robert\MS_Office\Excel\Samples\"

...as that was just me doing some extra testing.

Regards,

Robert
Thanks so much!
 
Upvote 0

Forum statistics

Threads
1,215,684
Messages
6,126,200
Members
449,298
Latest member
Jest

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