reading entire text file using vba

前端 未结 8 1265
暗喜
暗喜 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:07
    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
    
    0 讨论(0)
  • 2020-12-06 11:12

    To read line by line:

    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
    
    0 讨论(0)
  • 2020-12-06 11:12

    Fidel's answer, over Brettdj's answer, adjusted for ASCII or Unicode and without magical numbers:

    Public Function readFileContents(ByVal fullFilename As String, ByVal asASCII As Boolean) As String
        Dim objFSO As Object
        Dim objTF As Object
        Dim strIn As String
    
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        Set objTF = objFSO.OpenTextFile(fullFilename, IOMode:=ForReading, format:=IIf(asASCII, TristateFalse, TristateTrue))
        strIn = objTF.ReadAll
        objTF.Close
        readFileContents = strIn
    End Function
    
    0 讨论(0)
  • 2020-12-06 11:19

    More Slightly modified for those who do not like VBA to have to make up explicit variables and then waste time transfer data to them.. Let With. do the job

    Function LoadFileStr$(FN$)
    
      With CreateObject("Scripting.FileSystemObject")
    
              LoadFileStr = .OpenTextFile(FN, 1).readall
    
            End With
    
    End Function
    
    0 讨论(0)
  • 2020-12-06 11:19

    brettdj's answer, slightly adjusted

    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
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题