使用vba读取整个文本文件

时间:2013-12-05 02:43:31

标签: excel vba

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

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

当我跑这个时,我收到了一个错误。

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

文本文件的内容是:

  

无法打开COM10。确保已连接
    加上其他东西
    还有更多的东西     更多的东西

提前感谢您的帮助。

8 个答案:

答案 0 :(得分:11)

您可以将整个文件读取到变量数组中,然后将其转储为单个文件,而不是逐个单元格循环。镜头

将路径从C:\temp\test.txt更改为适合。

Sub Qantas_Delay()
Dim objFSO As Object
Dim objTF As Object
Dim strIn 'As String
Dim X

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTF = objFSO.OpenTextFile("C:\temp\test.txt", 1)
strIn = objTF.readall
X = Split(strIn, vbNewLine)
[h12].Resize(UBound(X) + 1, 1) = Application.Transpose(X)
objTF.Close

End Sub

答案 1 :(得分:6)

以下代码将遍历文本文档中的每一行,并在范围H12中向下打印,并在UI工作表中向下打印。

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

答案 2 :(得分:6)

对于那些不喜欢VBA必须组成显式变量,然后浪费时间将数据传输给他们的人,稍微修改了一下。做好工作

Function LoadFileStr$(FN$)

  With CreateObject("Scripting.FileSystemObject")

          LoadFileStr = .OpenTextFile(FN, 1).readall

        End With

End Function

答案 3 :(得分:2)

brettdj的回答,略有调整

Public Function readFileContents(ByVal fullFilename As String) As String
    Dim objFSO As Object
    Dim objTF As Object
    Dim strIn As String

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objTF = objFSO.OpenTextFile(fullFilename, 1)
    strIn = objTF.readall
    objTF.Close

    readFileContents = strIn
End Function

答案 4 :(得分:2)

逐行阅读:

Public Sub loadFromFile(fullFilename As String)

    Dim FileNum As Integer
    Dim DataLine As String

    FileNum = FreeFile()
    Open fullFilename For Input As #FileNum

    While Not EOF(FileNum)
        Line Input #FileNum, DataLine
        Debug.Print DataLine
    Wend
End Sub

答案 5 :(得分:1)

Sub LoadFile() ' load entire file to string
' from Siddharth Rout
' http://stackoverflow.com/questions/20128115/
    Dim MyData As String
    Open "C:\MyFile" For Binary As #1
    MyData = Space$(LOF(1)) ' sets buffer to Length Of File
    Get #1, , MyData ' fits exactly
    Close #1
End Sub

答案 6 :(得分:0)

我认为更简单的选择是Data> From Text您可以指定在“属性”中刷新数据的频率。

答案 7 :(得分:0)

Fidel的答案(超过Brettdj的答案)针对ASCII或Unicode进行了调整,并且没有奇数:

import random
from tkinter import *
import time
class game:
    def __init__(self,widthh,heighht):

        self.tk=Tk()
        self.canvas=Canvas(self.tk, width=widthh, height=heighht )
        self.canvas.pack()
        self.background=PhotoImage(file="C:/Users/ryantan/Desktop/python/run for your life/player sprite/player1.gif")
        self.over=self.canvas.create_image(200,200,image = self.background, anchor = 'center')
        self.tk.update()

    def mainloop(self):
        while True:
            self.tk.update()
g=game(1000,400)
g.canvas.itemconfig(g.over,image = PhotoImage(file="C:/Users/ryantan/Desktop/python/run for your life/player sprite/player2.gif"))
g.mainloop()