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

Have you ever tried to print certain page or worksheet based on a cell value in Excel? And how to achieve it? Methods in this article can help you to solve it in details.

Print certain page based on cell value with VBA code

Print certain worksheet based on cell value with VBA code

Office Tab Enable Tabbed Editing and Browsing in Office, and Make Your Work Much Easier...
Kutools for Excel Solves Most of Your Problems, and Increases Your Productivity by 80%
  • Reuse Anything: Add the most used or complex formulas, charts and anything else to your favorites, and quickly reuse them in the future.
  • More than 20 text features: Extract Number from Text String; Extract or Remove Part of Texts; Convert Numbers and Currencies to English Words.
  • Merge Tools: Multiple Workbooks and Sheets into One; Merge Multiple Cells/Rows/Columns Without Losing Data; Merge Duplicate Rows and Sum.
  • Split Tools: Split Data into Multiple Sheets Based on Value; One Workbook to Multiple Excel, PDF or CSV Files; One Column to Multiple Columns.
  • Paste Skipping Hidden/Filtered Rows; Count And Sum by Background Color; Send Personalized Emails to Multiple Recipients in Bulk.
  • Super Filter: Create advanced filter schemes and apply to any sheets; Sort by week, day, frequency and more; Filter by bold, formulas, comment...
  • More than 300 powerful features; Works with Office 2007-2019 and 365; Supports all languages; Easy deploying in your enterprise or organization.

arrow blue right bubble 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:

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


arrow blue right bubble 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

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:

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 pf Kutools for Excel as below screenshot shown. You can go to free download the software with no limitation in 60 days.


arrow blue right bubbleRelated articles:


The Best Office Productivity Tools

Kutools for Excel Solves Most of Your Problems, and Increases Your Productivity by 80%

  • Reuse: Quickly insert complex formulas, charts and anything that you have used before; Encrypt Cells with password; Create Mailing List and send emails...
  • Super Formula Bar (easily edit multiple lines of text and formula); Reading Layout (easily read and edit large numbers of cells); Paste to Filtered Range...
  • Merge Cells/Rows/Columns without losing Data; Split Cells Content; Combine Duplicate Rows/Columns... Prevent Duplicate Cells; Compare Ranges...
  • Select Duplicate or Unique Rows; Select Blank Rows (all cells are empty); Super Find and Fuzzy Find in Many Workbooks; Random Select...
  • Exact Copy Multiple Cells without changing formula reference; Auto Create References to Multiple Sheets; Insert Bullets, Check Boxes and more...
  • Extract Text, Add Text, Remove by Position, Remove Space; Create and Print Paging Subtotals; Convert Between Cells Content and Comments...
  • Super Filter (save and apply filter schemes to other sheets); Advanced Sort by month/week/day, frequency and more; Special Filter by bold, italic...
  • Combine Workbooks and WorkSheets; Merge Tables based on key columns; Split Data into Multiple Sheets; Batch Convert xls, xlsx and PDF...
  • More than 300 powerful features. Supports Office/Excel 2007-2019 and 365. Supports all languages. Easy deploying in your enterprise or organization. Full features 30-day free trial. 60-day money back guarantee.
kte tab 201905

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!
officetab bottom
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    Ryan · 1 years ago
    This was very helpful! Is there a way to print multiple pages? For example, if cell A1 = 5, print pages 1-5.
    • To post as a guest, your comment is unpublished.
      crystal · 1 years ago
      Hi Ryan,
      If you want to print consecutive pages such as 1-5 in a worksheet based on a cell value, please specify a cell such as A1 as Text formatting, type 1-5 into it, keep this cell selecting, and then run the below VBA code.

      Sub Print_Pages()
      Dim xPage As String
      Dim xYesorNo As Integer
      Dim xI As String
      Dim xPArr() As String
      Dim xIS, xIE, xF, xNum As Integer
      xPage = ActiveCell.Value
      xYesorNo = MsgBox("Ready to print page" & xPage & " ", vbYesNo, "Kutools for Excel")
      If xYesorNo = vbYes Then
      xPArr() = Split(xPage, "-")
      If UBound(xPArr) = 0 Then
      If IsEmpty(xPage) And IsNumeric(xPage) Then
      MsgBox "Please specify a cell and enter a page in cell"
      Exit Sub
      End If
      xNum = Int(xPage)
      ActiveSheet.PrintOut from:=xNum, To:=xNum, preview:=True
      ElseIf UBound(xPArr) = 1 Then
      On Error GoTo Err01
      xIS = Int(xPArr(0))
      xIE = Int(xPArr(1))
      If xIS < xIE Then
      For xF = xIS To xIE
      ActiveSheet.PrintOut from:=xF, To:=xF, preview:=True
      Next
      Else
      For xF = xIE To xIS
      ActiveSheet.PrintOut from:=xF, To:=xF, preview:=True
      Next
      End If
      Else
      MsgBox "Please enter the valid data", vbOKOnly, "Kutools for Excel"
      Exit Sub
      End If
      Else
      Exit Sub
      End If
      Exit Sub
      Err01:
      MsgBox "Please enter the correct page scope", vbOKOnly, "Kutools for Excel"
      End Sub