VBA: how to read text file

This is just example, how to read text file in Visual Basic for Applications (for example, in Excel macro).

It’s just a stub: there is no error processing here, the source file is really text file, no problems with permissions etc.

Sub read_from_text_file()

txtFile = Application.GetOpenFilename("Text Files (*.txt), *.txt")
If txtFile = False Then
MsgBox "No file to process"
Exit Sub
End If

Open txtFile For Input As #1
Input #1, myString
MsgBox myString
Close #1

End Sub

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.