PDA

View Full Version : Need help with writing a program for importing data into Excel worksheet!



Nunu
12-07-2008, 12:26 PM
I have to write a program that imports data-files from TSPLIB. It has to be possible for the user to choose the file which here after gets imported into a worksheet in Excel. Because the data from the file has to be used in further assignments, it would be it best if the data gets arranged so that the first 5 rows of text (from the imported data) are placed in the first column of the excel worksheet and each of the 3 columns of numbers (from the imported data) have each their own colum in the excel worksheet.)

I?ve gotten this far with the macro-recorder I just don?t know how :banghead::banghead::banghead: to make it dynamic, so the user is able to choose which file is to be imported. Any help would be greatly appreciated : pray2:: pray2:: pray2:!

Sub Makro1()

With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;C:\Documents and Settings\Nunu\Desktop\EksOpgDataE08\TSPLIB\a280.tsp" _
, Destination:=Range("A1"))
.Name = "a280"
.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 = xlFixedWidth
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1)
.TextFileFixedColumnWidths = Array(3, 4)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With

End Sub

rbrhodes
12-07-2008, 07:45 PM
Hi Nunu,

Try something like this with a COPY of your workbook.



Dim fName As String

fName = "TEXT;" & Application.GetOpenFilename

With ActiveSheet.QueryTables.Add(Connection:=fName, Destination:=Range("A1"))

' etc

Nunu
12-08-2008, 10:11 AM
Hi rbrhodes,

Thank you sooooooo much for your help:)

Nunu