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

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.

mdusoe

Active Member
Joined
Aug 27, 2004
Messages
428
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.
 

dsimcox

Board Regular
Joined
Dec 8, 2004
Messages
75
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:
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,167,832
Messages
5,855,890
Members
431,772
Latest member
dannyboi1

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
Top