Run time error 1004

User avatar
VegasNath
5StarLounger
Posts: 1185
Joined: 24 Jan 2010, 12:02
Location: Wales, UK.

Run time error 1004

Post by VegasNath »

Code: Select all

Dim strFile As String

strFile = ThisWorkbook.Path & "\" & "Export.TXT"

    Sheets("1").Select
    With ActiveSheet.QueryTables.Add(Connection:=strFile, Destination:=Range("A1"))
? strFile
C:\Users\Nathan\Documents\Export.TXT

I'm getting Run time error 1004 on the above, please can anyone tell me why? Thanks
:wales: Nathan :uk:
There's no place like home.....

User avatar
HansV
Administrator
Posts: 78631
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Run time error 1004

Post by HansV »

As you can read in the help subject for the Add method of the QueryTables object, the connection string for a text file should begin with "TEXT;", so use

With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & strFile, Destination:=Range("A1"))
Best wishes,
Hans

User avatar
VegasNath
5StarLounger
Posts: 1185
Joined: 24 Jan 2010, 12:02
Location: Wales, UK.

Re: Run time error 1004

Post by VegasNath »

Thanks Hans, I appreciate your help!
:wales: Nathan :uk:
There's no place like home.....