Record count in a text file

dsimcox

Board Regular
Joined
Dec 8, 2004
Messages
75
I want to determine the number of records in a text file beforeimporting the file into an Excel worksheet.

Is there a way to query the file for this information using VBA?
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Depends. If you are talking about a file where one records = one line, then you could use something like this:

Code:
' Open the file.  Read the whole thing into one big string.
    Open "myfile.txt" For Binary Access Read As #1
    strFile = String(LOF(1), " ")
    Get #1, , strFile
    Close #1
    
' create an array from the records in the string
    aryRecords = Split(strFile, vbCrLf)

' The number of records is the UBound of the array + 1.
    RecCount = UBound(aryRecords) + 1

Does this work?
Mike.
 
Upvote 0
Perfect!

This is exactly what I had hoped to accomplish.

I didn't realize you could read in a string of 280000 records so quickly.

Thank you Mike!

:wink:
 
Upvote 0

Forum statistics

Threads
1,214,919
Messages
6,122,260
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top