reading entire text file using vba

前端 未结 8 1282
暗喜
暗喜 2020-12-06 11:03

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$(LO         


        
8条回答
  •  时光说笑
    2020-12-06 11:25

    Rather than loop cell by cell, you can read the entire file into a variant array, then dump it in a single shot.

    Change the path from C:\temp\test.txt to suit.

    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
    

提交回复
热议问题