You can open a text file from Excel with VBA and display its contents in a message box, a worksheet cell, or a userform using the built-in Open statement and either Line Input (line by line) or Input (whole file at once). No add-in or external library is needed. This works in Excel for Windows and Mac; the only thing you must get right is supplying a real file path and handling errors so the macro does not crash when the file is missing.
Macro security note: the workbook must be saved as .xlsm (or .xlsb) and macros enabled. If you cannot enable macros in your organisation, VBA will not run at all.
What You'll Need
- Excel with macros enabled (File > Options > Trust Center > Trust Center Settings > Macro Settings, or the equivalent on Mac). Labels can differ slightly by version.
- A text file you can point to, for example
C:\Data\notes.txt. On Mac the path uses slashes, e.g./Users/you/Documents/notes.txt. - The VBA editor open: press Alt+F11 (Windows) or Option+F11 (Mac), then Insert > Module.
- Basic comfort pasting code and pressing F5 to run it.
Step-by-Step
- Open the VBA editor. Press Alt+F11 in Windows, or Option+F11 on Mac. If nothing appears, click the Developer tab; if you don't see it, enable it in File > Options > Customize Ribbon.
- Insert a module. In the editor choose Insert > Module. The blank code window is where your macro goes.
- Type or paste the simplest version. This reads the file line by line and shows it in a message box:
Sub ShowTextFile() Dim fNum As Integer Dim filePath As String Dim allText As String Dim lineText As String filePath = "C:\Data\notes.txt" fNum = FreeFile Open filePath For Input As #fNum Do Until EOF(fNum) Line Input #fNum, lineText allText = allText & lineText & vbCrLf Loop Close #fNum MsgBox allText End Sub - Change the path. Replace
C:\Data\notes.txtwith the real location of your file, including the extension. Keep the quotes. A single mistyped backslash is the most common cause of failure. - Run it. Click inside the subroutine and press F5, or use Run > Run Sub/UserForm. A message box should appear with the file's text.
- Optional: let the user pick the file. Instead of a fixed path, use the file dialog so the macro works for any file:
Put this before theDim fd As FileDialog Set fd = Application.FileDialog(msoFileDialogFilePicker) fd.Title = "Choose a text file" fd.Filters.Clear fd.Filters.Add "Text files", "*.txt" If fd.Show = -1 Then filePath = fd.SelectedItems(1) Else Exit Sub End IfOpenstatement.FileDialogworks in Excel for Windows; on Mac it is available too in current versions, but check you are on a version that supports it. - Optional: show the text in a worksheet instead. If the file is long, a message box is awkward. Write each line to a cell:
This puts one line per row in column A. If the text file uses commas or tabs and you want columns, use File > Open and the Text Import Wizard instead of a hand-written parser.Dim r As Long r = 1 Open filePath For Input As #fNum Do Until EOF(fNum) Line Input #fNum, lineText Cells(r, 1).Value = lineText r = r + 1 Loop Close #fNum - Add error handling. Wrap the read in
On Error GoTo NoFileand at the end of the procedure print a friendly message, so a missing or locked file does not dump the user into the debugger. - Save as a macro-enabled workbook. Use File > Save As and pick .xlsm. Plain .xlsx silently drops the macro.
Common Mistakes
- Forgetting
Close #fNum. Leaving the file handle open can lock the file for other programs. - Fixing the path in code but not the extension. Windows hides extensions by default, so a file displayed as "notes" may really be
notes.txt.txt. - Using
Inputfor a whole file.Input #fNum, myStringstops at commas and line breaks in odd ways;Line Inputin a loop is more predictable for showing raw text. - Message box limits.
MsgBoxtruncates very long strings (roughly 1023 characters for the message text), so a big file appears cut off. Write to cells or a text box instead. - Unicode files. If the file shows garbled characters, it may be UTF-8. The classic
Openstatement reads ANSI by default; re-save the file as ANSI, or use a different read approach for UTF-8. - Relative paths. A bare name like
notes.txtlooks in the current directory, which may change. Always give the full path.
Tips
- Use
FreeFilerather than a hard-coded number such as#1; it avoids clashes with other file operations. - Test the path first with
Dir(filePath)returning a non-empty string before opening. - Keep the whole-file text in a variable if you plan to search it, for example with
InStr. - Store the path in a cell and read it with
Range("A1").Valueso users can change the file without editing code. - Use
vbCrLfto join lines; it displays correctly in a message box.
Troubleshooting
- "File not found" (runtime error 53). Check the path character by character, confirm the extension, and that the drive is available. Copy the path from File Explorer's address bar instead of typing.
- "Permission denied" (error 70). The file may be open in another program or you may lack read rights. Close it elsewhere and try again.
- "Bad file name or number" (error 52) or "Input past end of file" (error 62). A handle was not closed earlier, or you read beyond the file; make sure every
Openhas a matchingClose. - Nothing happens when you press F5. The cursor may be outside the subroutine, or the module is in a different project. Click inside the code, then Run.
- Macros disabled. Check the yellow security bar and the Trust Center; ask your IT team if policy blocks macros.
- Text looks wrong. Check the file's encoding and re-save it, or open it in Notepad to see whether it really is plain text.
Frequently Asked Questions
Can I read the entire file in one line of code?
Yes. After opening, use Line Input with LOF to read the whole file as a single string. For very large files this consumes memory, so loop line by line instead.
Will this work on Excel for Mac?
Yes. The Open statement, Line Input, and EOF work the same; only the path style differs. Use forward slashes.
Can I open a CSV the same way?
You can read it as text, but you will have to split it yourself. For a proper table, use File > Open with the Text Import Wizard.
How do I close the file if the macro errors mid-read?
Put Close #fNum in the error handler, or use a cleanup label that runs regardless of success.
Why does only the first part of the file show in the message box?
MsgBox has a length limit, so long text gets cut. Write to cells or a text box for the full content.
Can VBA open a file from OneDrive or a network share?
Yes, if the path is accessible and you have permission. Use the mapped drive or full UNC path and check it exists first with Dir.