PDA

View Full Version : import txt to excel with VBA



alwayslearn
09-04-2008, 07:54 PM
I need the some code to copy the attached txt into excel with the click of a command button. Can anyone help? All I can find on the site is information on large txt files.

Thanks.

alwayslearn
09-04-2008, 08:25 PM
cannot attache text documents

here is how the data is formatted.

YM Buy 50 12800 1/2/2008
NQ Sell 30 1933.75 1/2/2008
YM Sell 70 12785 1/2/2008
NQ Buy 20 1932.75 1/2/2008
ES Buy 60 1408.50 1/2/2008

alwayslearn
09-06-2008, 11:39 AM
Can anyone take a look at this one for me?

mdmackillop
09-06-2008, 11:42 AM
You can attach a text file by zipping and posting with Manage Attachments in the Go Advanced reply section.

mdmackillop
09-06-2008, 11:53 AM
Sub Macro1()
Dim MyFile As String
MyFile = "C:\AAC\MySample.txt"

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

alwayslearn
09-06-2008, 12:02 PM
Md,

This is the same as I have been doing. Recording a macro to import the data. The problem is I want to take this data and copy and paste it under the command button click code area. I am getting compile errors when I try to do this.

mdmackillop
09-06-2008, 12:20 PM
Sample attached

alwayslearn
09-06-2008, 12:33 PM
You are the man!!!