Reputation: 447
I'm working on a VBA application where I want to open a CSV file, get the contents of a certain line, and store it in a string. I've got it working, but it seems to be to be a bit of a brute force approach and I feel there should be a more elegant way of doing it than loading every single line until the one I want into my string.
Dim RowCount As Long
Dim CurrentLine As Long
Dim objFSO, objFile
Const ForReading = 1
RowCount = 0
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(FileName, ForReading) 'Open specified file
CurrentLine = 0 'Start with line "0"
Message = objFile.ReadLine 'Read contents of first line
Do While CurrentLine < EventNumber 'If the current line being examined is not the specified line
CurrentLine = CurrentLine + 1 'Increment the current line counter
Message = objFile.ReadLine 'And copy the specified line to the Message string
Loop 'And repeat
objFile.Close 'Close the file when done
Can anyone suggest a better way?
Also - will I run into trouble if the entry I go to load is too long? How long would it have to be? In the PLC programming world a standard string is 82 characters, does that also apply to VBA?
Upvotes: 0
Views: 192
Reputation: 1716
Does the CSV file have a header line? You can use CreateObject("ADODB.Connection")
to execute an sql query with something like where EventNumber = something
.
Check this out: querying csv with vbs
Upvotes: 1