Importing Text File

D

Dan

I need to import a text file (weekly), to my workbook and
I'm having a problem getting the macro to function
properly. The macro stops with the last line, Any help
would be greatly appreciated.

MyFile = Application.GetOpenFilename("Text Files,*.Txt")

With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;F:\MyFile.txt", Destination:=Range("A1"))
.Name = MyFile
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier =
xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False

End With
End Sub


Thanks,
Dan
 
B

Bob Phillips

Does it look the same on your machine as it does here in the NG?
Specifically, these lines

.TextFileTextQualifier =
xlTextQualifierDoubleQuote

.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)

should each be one single line.


--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 
C

Cecilkumara Fernando

Dan,
What is the error massage?
I got one when the file name is incorrect
Cecil
 
G

Guest

All are single lines except the .TextFileColumnDataTypes
= Array and it has the _ at the end of the line where
it's split.
 
G

Guest

Cecil,

I get run time error 1004, Cannot find the text file to
refresh this external data range.

I get this error even though the file exsists and I
select it with the myFile= . The debug routine highlights
the last line of code.

Thanks,
Dan
 
T

Tom Ogilvy

With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;F:\MyFile.txt", Destination:=Range("A1"))
.Name = MyFile

Looks for F:\MyFile.Txt

you need to change to

With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;" & MyFile, Destination:=Range("A1"))
.Name = "MyFile" & Activesheet.QueryTables.count + 1
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top