Skip to main content

Kutools for Office β€” One Suite. Five Tools. Get More Done.

How to print certain pages or worksheet based on cell value in Excel?

Author Siluvia Last modified

Have you ever needed to print specific pages or worksheets in Excel based on the value of a cell? Whether it's printing a particular page number or an entire worksheet when a condition is met, this guide provides step-by-step methods to accomplish it.

Print certain page based on cell value with VBA code
Print certain worksheet based on cell value with VBA code


Print certain page based on cell value with VBA code

For example, you want a certain page such as page 1 of current worksheet to be printed automatically when entering the page number 1 into cell A1 in current worksheet. You can do as follows to achieve it.

1. Select the cell contains the page number you want to print based on, then press Alt and F11 keys simultaneously to open the Microsoft Visual Basic for Applications window. Then click Insert > Module.

2. Then the Microsoft Visual Basic for Applications window pops up, please copy and paste the following VBA code into the Code window.

VBA code: Print page based on cell value

Option Explicit
Sub Print_Pages()
Dim xPage As Integer
Dim xYesorNo As Integer
With ActiveCell
    If Not IsEmpty(.Value) And IsNumeric(.Value) Then
        xPage = .Value
    Else: MsgBox "Please specify a cell and enter a page in cell"
        Exit Sub
    End If
End With
xYesorNo = MsgBox("Ready to print page" & xPage & " ", vbYesNo, "Kutools for Excel")
If xYesorNo = vbYes Then
    ActiveSheet.PrintOut from:=xPage, To:=xPage, preview:=True
Else
    Exit Sub
End If
End Sub

3. Press F5 key to run the code. In the popping up Kutools for Excel dialog box, click the Yes button to print the certain page, or click the No button to exit the dialog box if you don’t want to print. See screenshot:

A screenshot of the Kutools for Excel dialog box for printing a specific page based on a cell value

Then the preview window of the certain page of current worksheet opens, please click the Print button to start printing.

A screenshot of the Print Preview window


Print certain page based on cell value with VBA code

Supposing you want to print the active worksheet when value in cell B2 of this sheet equals to number 1001. You can achieve it with the following VBA code. Please do as follows.

1. Right click the Sheet Tab that you want to print based on cell value, and then click View Code from the right-clicking menu.

2. Then the Microsoft Visual Basic for Applications window pops up, please copy and paste the following VBA code into the Code window.

VBA code: Print worksheet based on cell value

Private Sub Worksheet_Change(ByVal Target As Range)
Dim xCell As Range, xYesorNo As Integer
Set xCell = ActiveSheet.Range("B2")
If Application.Intersect(Target, xCell) Is Nothing Then Exit Sub
    If xCell.Value = 1001 Then
        xYesorNo = MsgBox("Ready to print the specified worksheet? ", vbYesNo, "Kutools for Excel")
        If xYesorNo = vbYes Then
          ActiveSheet.PrintOut
        Else
           Exit Sub
        End If
End If
End Sub

A screenshot showing where to insert VBA code for printing based on a cell value in Excel

Note: You can change the cell and cell value in the code as you need.

3. Press Alt + Q to close the Microsoft Visual Basic for Applications window.

When entering number 1001 into cell B2, a Kutools for Excel dialog box pops up, please click the Yes button to start printing. Or click the No button to exit the dialog box directly without printing. See screenshot:

A screenshot of the Kutools for Excel dialog box for confirming worksheet printing based on cell value

Tip: If you want to directly print non-consecutive pages such as page 1, 6 and 9 in current worksheet, or print only all even or odd pages, you can try the Print Specified Pages utility of Kutools for Excel as below screenshot shown.

A screenshot of the Kutools for Excel Print Specified Pages utility for selective printing

Kutools for Excel - Supercharge Excel with over 300 essential tools. Enjoy permanently free AI features! Get It Now


Related articles:

Best Office Productivity Tools

πŸ€– Kutools AI Aide: Revolutionize data analysis based on: Intelligent Execution   |  Generate Code  |  Create Custom Formulas  |  Analyze Data and Generate Charts  |  Invoke Kutools Functions…
Popular Features: Find, Highlight or Identify Duplicates   |  Delete Blank Rows   |  Combine Columns or Cells without Losing Data   |   Round without Formula ...
Super Lookup: Multiple Criteria VLookup    Multiple Value VLookup  |   VLookup Across Multiple Sheets   |   Fuzzy Lookup ....
Advanced Drop-down List: Quickly Create Drop Down List   |  Dependent Drop Down List   |  Multi-select Drop Down List ....
Column Manager: Add a Specific Number of Columns  |  Move Columns  |  Toggle Visibility Status of Hidden Columns  |  Compare Ranges & Columns ...
Featured Features: Grid Focus   |  Design View   |   Big Formula Bar    Workbook & Sheet Manager   |  Resource Library (Auto Text)   |  Date Picker   |  Combine Worksheets   |  Encrypt/Decrypt Cells    Send Emails by List   |  Super Filter   |   Special Filter (filter bold/italic/strikethrough...) ...
Top 15 Toolsets12 Text Tools (Add Text, Remove Characters, ...)   |   50+ Chart Types (Gantt Chart, ...)   |   40+ Practical Formulas (Calculate age based on birthday, ...)   |   19 Insertion Tools (Insert QR Code, Insert Picture from Path, ...)   |   12 Conversion Tools (Numbers to Words, Currency Conversion, ...)   |   7 Merge & Split Tools (Advanced Combine Rows, Split Cells, ...)   |   ... and more
Use Kutools in your preferred language – supports English, Spanish, German, French, Chinese, and 40+ others!

Supercharge Your Excel Skills with Kutools for Excel, and Experience Efficiency Like Never Before. Kutools for Excel Offers Over 300 Advanced Features to Boost Productivity and Save Time.  Click Here to Get The Feature You Need The Most...


Office Tab Brings Tabbed interface to Office, and Make Your Work Much Easier

  • Enable tabbed editing and reading in Word, Excel, PowerPoint, Publisher, Access, Visio and Project.
  • Open and create multiple documents in new tabs of the same window, rather than in new windows.
  • Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day!

All Kutools add-ins. One installer

Kutools for Office suite bundles add-ins for Excel, Word, Outlook & PowerPoint plus Office Tab Pro, which is ideal for teams working across Office apps.

Excel Word Outlook Tabs PowerPoint
  • All-in-one suite β€” Excel, Word, Outlook & PowerPoint add-ins + Office Tab Pro
  • One installer, one license β€” set up in minutes (MSI-ready)
  • Works better together β€” streamlined productivity across Office apps
  • 30-day full-featured trial β€” no registration, no credit card
  • Best value β€” save vs buying individual add-in