使用vba读取整个文本文件 [英] reading entire text file using vba

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

问题描述

我正在尝试使用vba读取文本文件。我尝试以下代码

I'm trying to read a text file using vba. I tried the below code

Open "C:\tester.txt" For Input As #1
Worksheets("UI").Range("H12").Value = Input$(LOF(1), 1)
Close #1

当我运行这个我收到一个错误。

When I run this I'm getting an error.

运行时错误'62'。输入文件末尾。

Run-time error '62'. Input past end of file.

文本文件的内容是:


无法打开COM10。确保已连接

加上其他东西

更多的东西

更多的东西

Unable to open COM10. Make sure it is connected
Plus other stuff
And more stuff
way more stuff

提前感谢帮助。

推荐答案

以下代码将循环遍历文本中的每一行文件并从范围 H12 中打印,并在UI表中向下。

The following code will loop through each line in the text document and print these from range H12 and downward in the UI-sheet.

Sub ImportFromText()
    Open "C:\tester.txt" For Input As #1
    r = 0
    Do Until EOF(1)
        Line Input #1, Data
        Worksheets("UI").Range("H12").Offset(r, 0) = Data
        r = r + 1
    Loop
    Close #1
End Sub

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

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