Why does my text file open in a new workbook?

shakethingsup

Board Regular
Joined
May 21, 2017
Messages
64
Office Version
  1. 365
Platform
  1. Windows
Hello!
I googled a code to open a text file and I can't remember where I found the code. To be honest, I don't know what a querytable is yet. The file format is actually a "*.eft" which can be opened up in notepad.


I want to achieve 2 things:

  1. open the textfile in my CURRENT workbook and the current worksheet. - the code opens in a new second workbook.
  2. I want the information to populated in column G. - the code opens the text file in cell A1.

    Sub openeft()
    Dim filename
    filename = Application.GetOpenFilename("Text Files (*.EFT), *.EFT")
    If filename <> False Then
    With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & filename, Destination:=Range("$G$9"))
    Workbooks.Open filename
    End With
    End If
    Range("G:G").WrapText = True
    Columns("G").ColumnWidth = 200
    End Sub
At some point, I'd like to learn how to separate the garbled text into respective cells. In that text file, I have data such as a vendor name and the $amount but the problem is is that it all comes out into one cell. So in my other code, I am searching using instr to find certain text when ultimately, I'd like to do a vlookup. Anyway, let's do one step at a time.

What could I do differently? Please and thanks.

 
Last edited:

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
This is untested, but give it a shot:
Code:
Sub openeft()
    Dim filename
    
    filename = Application.GetOpenFilename("Text Files (*.EFT), *.EFT")
    
    If filename <> False Then
        With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & filename, Destination:=Range("$G$9"))
            .TextFileParseType = xlFixedWidth
            .TextFileColumnDataTypes = Array(1)
            .Refresh
        End With
    End If
    
    Range("G:G").WrapText = True
    Columns("G").ColumnWidth = 200
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,772
Members
452,353
Latest member
strainu

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