How to browse a file txt and import all the info to a excel workbook

Akihitoc

New Member
Joined
Apr 11, 2016
Messages
16
Hi good day!
I need help please, I need a macro to be able to select a ".txt" file (using the windows browser) and import all the information on that txt to a new workbook. Then save that new workbook as "BOMNAV" on a file that I choose
Here is what I have so far but Im stuck....

Code:
Sub CopyData()
    
    Dim fileDialog As fileDialog
    Dim strPathFile As String
    Dim strFileName As String
    Dim strPath As String
    Dim dialogTitle As String
    Dim wbSource As Workbook
    Dim rngToCopy As Range
    Dim rngRow As Range
    Dim rngDestin As Range
    Dim lngRowsCopied As Long
    
    
    dialogTitle = "Navigate to and select required file."
    Set fileDialog = Application.fileDialog(msoFileDialogFilePicker)
    With fileDialog
        .InitialFileName = "C:\Users\User\Documents"
        '.InitialFileName = ThisWorkbook.Path & "\" 'Alternative to previous line
        .AllowMultiSelect = False
        .Filters.Clear
        .Title = dialogTitle
        
        
        
        If .Show = False Then
            MsgBox "File not selected to import. Process Terminated"
            Exit Sub
        End If
        strPathFile = .SelectedItems(1)
    End With
     
    Set wbSource = Workbooks.Open(Filename:=strPathFile)
    wbkCurrent.Activate
End Sub
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
How about this?

Code:
Sub csvImport()
Dim fSel        As FileDialog
Dim wb          As Workbook
Dim ws          As Worksheet


Set wb = Workbooks.Add
Set ws = Sheets("Sheet1")
Set fSel = Application.FileDialog(msoFileDialogFilePicker)
    
With fSel
    .InitialFileName = "C:\Users\User\Documents"
    .AllowMultiSelect = False
    .Filters.Add "Text Files", "*.csv; *.txt"
    .Title = "Navigate ro and select required file."
End With


fSel.Show


With ws.QueryTables.Add(Connection:= _
    "TEXT;" & fSel.SelectedItems(1), _
    Destination:=Range("$A$1"))
    .TextFileCommaDelimiter = True
    .Refresh BackgroundQuery:=False
End With


wb.SaveAs Environ("UserProfile") & "\Desktop\" & "BOMNAV.xlsx"
wb.Close True
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,053
Messages
6,122,888
Members
449,097
Latest member
dbomb1414

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