Alex Gordon
Alex Gordon

Reputation: 60771

vba: read only first 1000 characters of file into string

i need to open a txt file and read it into a string in VBA, but i would like to only get the first 1000 characters.

the file itself is 20mb and i only need the first 1000 characters. is there a way to make this efficient?

Upvotes: 0

Views: 4437

Answers (2)

Lost in Alabama
Lost in Alabama

Reputation: 1653

Not sure if there is a more efficient way, but this method is pretty simple:

   Dim sText As String

   Open "C:\myfile.txt" For Input As #1

   sText = Input$(1000, 1)

   Close #1

Upvotes: 3

Kyra
Kyra

Reputation: 5407

How long is each of the lines in the file. What I would do is either read it in by character or by line (if the lines are shorter) and then set a cap of 1000 characters. This way you don't have to read in the whole file. You just read the first 1000 characters or a bit more if you are reading it in line by line.

Upvotes: 1

Related Questions