View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Stan Scott Stan Scott is offline
external usenet poster
 
Posts: 34
Default Count rows in text file by loading into array

Larry,

The following routine works for me:

Function DoLineCount(txtFilepath)
Dim FSO As Object
Dim objTextFile As Object
Dim fileText As String
Dim rowBound As Integer
Dim t As Integer
Dim fileTextArray
Dim newArray()

Set FSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = FSO.OpenTextFile(txtFilepath, 1)
fileText = objTextFile.readAll
fileTextArray = Split(fileText, vbCrLf)
rowBound = UBound(fileTextArray)

If rowBound + 1 32000 Then
MsgBox "WARNING: File length exceeds 32000 lines"
Exit Function
Else
ReDim newArray(rowBound, 0)
For t = 0 To rowBound
newArray(t, 0) = fileTextArray(t)
Next
ActiveWorkbook.Sheets(1).Range("A1").Resize(rowBou nd + 1, 1).Value =
newArray
End If

objTextFile.Close
Set objTextFile = Nothing
Set FSO = Nothing
End Function

The first part of the routine uses the FileSystemObject to read the text
into a variable, which is then converted to an array, fileTextArray, using
the Split function and vbCrLf. The number of lines in the file is
uBound(fileTextArray) + 1.

If the file passes the 32000 line test, a second array, newArray, is
created. Unfortunately, fileTextArray can't be written directly to the
worksheet (it's more like a list), so it's read into newArray. Once that's
done, though, the array can be passed to the worksheet in a single step.

Hope this helps,

Stan Scott
New York City

"L Mehl" wrote in message
...
Hello --

The app can allow no more than 32,000 data points (1 column of numbers) to
be imported using a text file.

[1]
Is there a way to load the file into an array, count the records loaded,

and
warn the user if 32,000 has been exceeded?

[2]
Then, if not exceeded, the app would then put the array values into a

column
in a worksheet.

Can anyone help with ideas or sample code for doing [1] and [2]?

Thanks for any help.

Larry Mehl
mehl_at_cyvest.com