vba读取文本文件中的所有文本? [英] vba read all text in a text file?

查看:963
本文介绍了vba读取文本文件中的所有文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用vba读取文本文件中的所有文本,并将其显示在excel消息框中。我有这个问题,而这是有效的工作,它显示每一行文本在一个单独的消息框,而不是我想要所有在一个?

I am trying to use vba to read all text in a text file and display it in an excel message box. the problem I have is whilst this is in effect working, it displays each line of text in a separate message box when instead I want it all in one?

可以有人请告诉我我在哪里错了谢谢

can someone please show me where I am going wrong. thanks

If Range("L" & ActiveCell.Row).Value = "Performance" Then
Dim FilePath As String
Dim strLine As String
FilePath = "\\UKSH000-FILE06\Purchasing\New_Supplier_Set_Ups_&_Audits\ATTACHMENTS\" & Range("C" & ActiveCell.Row).Value & "\performance.txt"
Open FilePath For Input As #1
While EOF(1) = False
    'read the next line of data in the text file
    Line Input #1, strLine
    'print the data in the current row
    MsgBox strLine
    'increment the row counter
    i = i + 1
Wend
Close #1

End If


推荐答案

在你的循环中,你必须连接所有的行字符串变量并在结尾输出结果。它基本上是这样的:

Within your loop you have to concatenate all the lines a string variable and output the result at the end. It's basically like this:

Dim Total As String
' ...
While EOF(1) = False
    'read the next line of data in the text file
    Line Input #1, strLine
    Total = Total & vbNewLine & strLine
    'increment the row counter
    i = i + 1
Wend

MsgBox Total

注意:虽然这个解决方案是有效的,但是对于大文件来说,这可能不是很有效率,因为在代码中看起来像一个连接,实际上意味着将现有内容复制到一个新的内存位置,然后插入新行的字符串。这是为每一行完成的。所以对于1000行,大量的总字符串复制了大约999次。

Note: While this solution is working, for large files it may not be very efficient due to the fact that what looks like a concatenation in code, in fact means copying the existing content to a new memory location, and then inserting the new line's string. That is done for every line. So for 1000 lines, the incresingly large total string is copied around 999 times.

这篇关于vba读取文本文件中的所有文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆