Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Excel VBA Print Statement: What It Does and How to Use It

· 6 min read · Home Tech

In this article
  1. What “Print” means in Excel VBA
  2. Use Debug.Print to inspect values
  3. Write output to a text file with Print #
  4. Print a worksheet on paper or to PDF
  5. Choosing the right method
  6. Common problems and misconceptions
  7. Frequently Asked Questions

In Excel VBA, the Print statement writes text or values to an output destination; it does not print a worksheet on paper. For quick debugging in Microsoft 365 for Windows, use Debug.Print in the Immediate window, or use Print # to write output to a text file.

What “Print” means in Excel VBA

The word “print” has several meanings in Excel, which is why the question can be confusing. Excel’s worksheet printing features send sheets or ranges to a printer or PDF. VBA’s Print statement instead outputs data to a destination such as a file or, through the Debug object, the Visual Basic Editor’s Immediate window.

VBA does not have a standalone Print command that automatically prints the active worksheet. To print a worksheet from a macro, use the worksheet or workbook PrintOut method. Use Debug.Print to inspect a value while a macro runs, and Print # when a text file is the intended output.

Use Debug.Print to inspect values

Debug.Print sends its output to the Immediate window in the Visual Basic Editor. In Excel for Windows, open the editor with Alt+F11, then show the Immediate window with Ctrl+G. Run the macro and read the output there. The Immediate window is a development tool, not a worksheet cell or a permanent log.

For example, this macro prints the active sheet’s name and the value in cell A1:

Sub CheckCellValue()
    Dim ws As Worksheet
    Dim cellValue As Variant

    Set ws = ActiveSheet
    cellValue = ws.Range("A1").Value

    ' Show the sheet name and cell value in the Immediate window.
    Debug.Print "Sheet: " & ws.Name
    Debug.Print "A1: " & cellValue
End Sub

VBA’s & operator joins text and values. Debug.Print can also display expressions, dates, and object properties, making it useful for checking whether a loop reaches a particular row or whether a variable contains the expected value. It does not change the workbook, and its output disappears when the editor session is closed.

Before running any macro that changes workbook data, save a copy of the file. Macro actions generally cannot be reversed with Ctrl+Z; printing diagnostic text with Debug.Print itself does not alter worksheet contents.

Write output to a text file with Print #

To keep output after the macro ends, VBA can write to a text file using the Print # statement. The file must be opened first and assigned a file number. The following example writes two lines to a file in Excel’s default file folder:

Sub WriteTextFile()
    Dim fileNumber As Integer
    Dim filePath As String

    filePath = ThisWorkbook.Path & Application.PathSeparator & "report.txt"
    fileNumber = FreeFile

    ' Save a copy of the workbook before running macros that change data.
    Open filePath For Output As #fileNumber
    Print #fileNumber, "Report generated by Excel VBA"
    Print #fileNumber, "Workbook: " & ThisWorkbook.Name
    Close #fileNumber
End Sub

FreeFile supplies an available file number. Open ... For Output creates the file or replaces its existing contents, so do not use it when you need to preserve earlier text. Close the file when finished; otherwise, output may not be fully written and the file can remain locked during the Excel session. If the workbook has not been saved, ThisWorkbook.Path can be empty, so save it first or choose a full file path explicitly.

For a file that should add new lines instead of replacing its contents, open it using For Append. File permissions and the destination folder matter: a protected or unavailable location can cause a runtime error. A text file is also not the same as a formatted Excel report; it contains plain text rather than cell formatting, formulas, or page layout.

If the goal is to print worksheet pages, use Excel’s printing method rather than VBA’s Print statement. For example, this prints the active worksheet using the current printer and print settings:

Sub PrintActiveSheet()
    Dim ws As Worksheet

    Set ws = ActiveSheet

    ' Save a copy before running macros that make workbook changes.
    ws.PrintOut
End Sub

PrintOut sends output to the selected printer by default. It can also accept arguments, including a printer name, page range, number of copies, and whether to display the print dialog. Check the worksheet’s print area, orientation, scaling, and page breaks first; a macro uses Excel’s print settings, so an unexpected layout can produce clipped or extra pages.

For a safer check, open File > Print to inspect the preview before printing. On Windows, Ctrl+P opens the print screen. On Mac, use Command+P to open printing options; VBA is available in desktop Excel for Mac, though printer dialogs and some VBA capabilities can differ from Windows. Excel for the web does not run VBA macros, so use desktop Excel to execute these examples.

Choosing the right method

  • Debug.Print: Use for temporary diagnostic messages while developing or troubleshooting a macro.
  • Print #: Use to create or append plain-text output that another tool or person can read.
  • Worksheet.PrintOut: Use when the macro should send worksheet pages to a printer or another available print destination.
  • Export to PDF: Use Excel’s PDF export features when you need a shareable document rather than paper or a plain-text log.

These approaches solve different problems. For example, writing “Processed row 12” to the Immediate window can help diagnose a loop, but it will not produce a report for colleagues. Conversely, printing the worksheet does not automatically show variable values that exist only inside the VBA procedure.

Common problems and misconceptions

  • Nothing appears on the sheet: Debug.Print does not write to cells. To put a value in a worksheet, assign it to a range, such as Range("B1").Value = "Done".
  • The Immediate window is blank: Open it in the Visual Basic Editor with Ctrl+G on Windows and make sure the code containing Debug.Print actually ran.
  • The text file overwrote earlier output: For Output replaces an existing file. Use For Append when adding output to the end is intended.
  • A macro prints the wrong pages: Check the active worksheet, print area, page setup, and print preview. A macro may target a different sheet than expected if it relies on ActiveSheet.
  • The macro will not run in a browser: Excel for the web can open macro-enabled workbooks but does not execute VBA. Open the workbook in desktop Excel.

Also distinguish the VBA statement from the old BASIC-style habit of calling every output operation “print.” Excel VBA’s relevant syntax depends on the destination: Debug.Print for the Immediate window, Print # for a file, and PrintOut for worksheet printing.

Frequently Asked Questions

What is the VBA equivalent of a print statement?
For debugging, use Debug.Print. To write to a text file, use Print # after opening a file. To print worksheet pages, call a worksheet’s PrintOut method.

Where does Debug.Print output go?
It appears in the Immediate window in the Visual Basic Editor. In Windows desktop Excel, open the editor with Alt+F11 and the window with Ctrl+G.

How do I print a worksheet using VBA?
Call PrintOut on the worksheet, for example ActiveSheet.PrintOut. Review print preview and page setup first to confirm the selected sheet and layout.

Does Print # print to my printer?
No. Print # writes text to an opened file. Worksheet printing uses PrintOut; the Immediate window uses Debug.Print.

Can I run VBA in Excel for the web?
No. Excel for the web does not run VBA macros. Open the workbook in desktop Excel for Windows or Mac to run VBA.