Monday, February 16, 2009

Transfer data from text file to Excel file

Copy data from text file and past it into the excel file with the following code.This is just like transfer text from Notepad to excel file.


Const ForReading = 1, ForWriting = 2
Dim fso, MyFile,a, intLine, strExcelFilePath
strExcelFilePath="C:\SPQTP.Xls"
Set fso = CreateObject("Scripting.FileSystemObject")
Set xlObj = CreateObject("Excel.Application")
xlObj.DisplayAlerts = False
Set xlBook=xlObj.workbooks.add
Set xlSheet=xlBook.activesheet
xlBook.saveas strExcelFilePath

Set xlWB = xlObj.Workbooks.Open ( strExcelFilePath)
Set NewSheet = xlWB.Sheets("Sheet1")
xlObj.Visible = True
xlObj.DisplayAlerts = False
Set MyFile = fso.OpenTextFile("C:\SPQTP.txt", ForReading, True)

i = 0
Do While not MyFile.AtEndOfStream
' msgbox MyFile.Readline
NewSheet.Cells(i+1,1) = MyFile.Readline
i = i+1
Loop
xlWB.save
xlWB.close
set xlObj= nothing

No comments:

Post a Comment