We can read CSV (Text) File data by ceating File System Object.Here is the function to perfrom that:
Function ReadCSV(Filepath,Row,Occurrence)
Set fso = CreateObject("Scripting.FileSystemObject")
Set tso = fso.OpenTextFile(Filepath,1,false,0)
'For Reaching the desired ROW
If Row <> 1 Then
For i =1 to (Row-1)
tso.SkipLine
Next
End If
'Capturing the entire desired ROW and its width
RequiredLine = tso.ReadLine
Width = tso.Column
z=1
'Loop for finding start and end position
For i=1 to (Occurrence+1)
y = Instr(z, RequiredLine , "," , 1)
'Special handling if the comma is last
If y = 0 Then
y = Width
End If
'Capturing Start Position
If i = Occurrence Then
StartPosition=y+1
End If
z=y+1
Next
'Special Handling of Condtion of Zero Occourence
If Occurrence = 0 Then
StartPosition = 1
End If
EndPosition = (y-StartPosition)
'The logic to read file without double quotes
ReadCSV = mid(RequiredLine,StartPosition+1,EndPosition-2)
Set tso = Nothing
Set fso = Nothing
End Function
Call to Function :
ReadCSV("C:\Test\CSVFile.txt",4,3)
This function return the data present after 3 occourance of the COMMA in the 4th Row of the CSV file that is present at the path we specified.
For example our file is like this :
"Tag1","Tag2","Tag3","Tag4","Tag5"
"1","12","123","1234","12345"
"a","ab","abc","abcd","abcde"
"x","xy","xyz","xyz1","xyz12"
"a","aa","aaa","aaaa","aaaaa"
We get the output of function = xyz1
Note that in function code itself we removed the double quotes also from data.
Wednesday, August 15, 2007
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment