pimar said:
Thanks Don for your reply, but this is not what I need.
I'd be more detailed: I use to generate a file (CSV) as result of my
daily job. Then. I use to count the nunber of rows in that file and to
report this number in my spreadsheet for my boss. What I'd like to do
is to let my workbook to automatically read the rows in my CSV file and
save this number in my spreadsheet.
Would be easy to run a very simple batch program from the workbook, if
possible, or to use the excel query. But since I'm very new with excel,
I don't know which is the fastest (best) way.
So I'm asking for sggestoins.
Thanks
If you can cope with (fairly simple) VBA, you could try this:
[note that you'll need a reference in your project to Microsoft
Scripting Runtime (scrrun.dll) and Split() is not defined in Excel 97
and before]
Public Function LinesInCSVFile(filepath As String)
Dim fs As FileSystemObject ' Need to create a reference to
Dim ts As TextStream ' Microsoft Scripting Runtime
Dim s As String ' holds the content of the file
temporarily
Set fs = New FileSystemObject ' set up
access to file system
Set ts = fs.OpenTextFile(filepath, ForReading) ' open the
file
s = ts.ReadAll ' get file
content for working
LinesInCSVFile = UBound(Split(s, vbCrLf)) ' create an
array of lines and count them
If Right(s, 2) <> vbCrLf Then ' if the file
ends in as vbCRLF there will be an
LinesInCSVFile = LinesInCSVFile + 1 ' unwanted
extra (empty) entry in the array but otherwise
End If ' we're one
short (Split arrays start at 0)
ts.Close ' tidy
Set ts = Nothing ' up
Set fs = Nothing ' objects used
End Function
HTH,
Mike