How To Load A Text File In Excel VBA With Load Data Infile


So for reference sake, here is something easy.

This is how to load a local text file into a variable, so you can process it.

For example:

So you have some JSON file, and you put the JSON formatted text locally in a text file so you can experiment on parsing it before you actually run a lot of web queries.

Well here is how to do it:

Sub LoadFile()
    Dim strURL As String
    
    'this is the text file in the directory of your spreadsheet   
    strURL = ThisWorkbook.Path & "\currencies.txt"
    
    Dim strFileContent As String
    Dim intFile As Integer
    
    intFile = FreeFile
    Open strURL For Input As #intFile
    strFileContent = Input(LOF(intFile), intFile)
    Close intFile

End Sub

Now the contents of your text file are in the string strFileContent.

Questions?