开发者

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 charact开发者_运维知识库ers. is there a way to make this efficient?


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜