views:

49

answers:

2

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?

+1  A: 

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.

Kyra
great! how do i read line by line then?
I__
I haven't actually programmed in VB yet I googled "vba read file line by line" and came back to stackoverflow. If you go to the below url a Kenny Bones has the code for reading a file in line by line. http://stackoverflow.com/questions/938796/vba-read-lines-from-text-file-exclude-top-two-lines
Kyra
+3  A: 

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
Lost in Alabama