跳到主要內容

如何基於Excel中的單元格值打印某些頁面或工作表?

您是否曾經嘗試過基於Excel中的單元格值打印某些頁面或工作表? 以及如何實現呢? 本文中的方法可以幫助您詳細解決它。

使用VBA代碼根據單元格值打印某些頁面
使用VBA代碼根據單元格值打印某些工作表


使用VBA代碼根據單元格值打印某些頁面

例如,當您在當前工作表的單元格A1中輸入頁碼1時,您希望自動打印某個頁面,例如當前工作表的頁面1。 您可以執行以下操作來實現它。

1.選擇包含要打印的頁碼的單元格,然後按 其他F11 同時打開 Microsoft Visual Basic for Applications 窗口。 然後點擊 插入 > 模塊.

2.然後 Microsoft Visual Basic for Applications 彈出窗口,請複制以下VBA代碼並將其粘貼到 推薦碼 窗口。

VBA代碼:根據單元格值打印頁面

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。 按 F5 鍵來運行代碼。 在彈出 Excel的Kutools 對話框中,單擊 按鈕以打印特定頁面,或單擊 沒有 如果您不想打印,請按按鈕退出對話框。 看截圖:

然後將打開當前工作表的特定頁面的預覽窗口,請單擊 列印 按鈕開始打印。


使用VBA代碼根據單元格值打印某些頁面

假設您要在此工作表的單元格B2中的值等於數字1001時打印活動工作表。您可以使用以下VBA代碼來實現它。 請執行以下操作。

1.右鍵單擊 工作表標籤 您要基於單元格值打印的內容,然後單擊 查看代碼 從右鍵單擊菜單中。

2.然後 Microsoft Visual Basic for Applications 彈出窗口,請複制以下VBA代碼並將其粘貼到“代碼”窗口中。

VBA代碼:根據單元格值打印工作表

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

備註:您可以根據需要在代碼中更改單元格和單元格的值。

3。 按 其他 + Q 關閉 Microsoft Visual Basic for Applications 窗口。

在單元格B1001中輸入數字2時, Excel的Kutools 彈出對話框,請點擊 按鈕開始打印。 或點擊 沒有 按鈕直接退出對話框而不進行打印。 看截圖:

小提示: 如果要直接打印當前工作表中的非連續頁面(例如第1、6和9頁),或僅打印所有偶數或奇數頁,則可以嘗試使用 打印指定頁面 實用程序pf Excel的Kutools 如下圖所示。 你可以去 30天內免費下載軟體,無限制.


相關文章:

最佳辦公生產力工具

🤖 Kutools 人工智慧助手:基於以下內容徹底改變數據分析: 智慧執行   |  生成代碼  |  建立自訂公式  |  分析數據並產生圖表  |  呼叫 Kutools 函數...
熱門特色: 尋找、突出顯示或識別重複項   |  刪除空白行   |  合併列或儲存格而不遺失數據   |   沒有公式的回合 ...
超級查詢: 多條件VLookup    多值VLookup  |   跨多個工作表的 VLookup   |   模糊查詢 ....
高級下拉列表: 快速建立下拉列表   |  依賴下拉列表   |  多選下拉列表 ....
欄目經理: 新增特定數量的列  |  移動列  |  切換隱藏列的可見性狀態  |  比較範圍和列 ...
特色功能: 網格焦點   |  設計圖   |   大方程式酒吧    工作簿和工作表管理器   |  資源庫 (自動文字)   |  日期選擇器   |  合併工作表   |  加密/解密單元格    按清單發送電子郵件   |  超級濾鏡   |   特殊過濾器 (過濾粗體/斜體/刪除線...)...
前 15 個工具集12 文本 工具 (添加文本, 刪除字符,...)   |   50+ 圖表 類型 (甘特圖,...)   |   40+ 實用 公式 (根據生日計算年齡,...)   |   19 插入 工具 (插入二維碼, 從路徑插入圖片,...)   |   12 轉化 工具 (數字到單詞, 貨幣兌換,...)   |   7 合併與拆分 工具 (高級合併行, 分裂細胞,...)   |   ... 和更多

使用 Kutools for Excel 增強您的 Excel 技能,體驗前所未有的效率。 Kutools for Excel 提供了 300 多種進階功能來提高生產力並節省時間。  點擊此處獲取您最需要的功能...

產品描述


Office選項卡為Office帶來了選項卡式界面,使您的工作更加輕鬆

  • 在Word,Excel,PowerPoint中啟用選項卡式編輯和閱讀,發布者,Access,Visio和Project。
  • 在同一窗口的新選項卡中而不是在新窗口中打開並創建多個文檔。
  • 將您的工作效率提高 50%,每天為您減少數百次鼠標點擊!
Comments (4)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
This was very helpful! Is there a way to print multiple pages? For example, if cell A1 = 5, print pages 1-5.
This comment was minimized by the moderator on the site
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
This comment was minimized by the moderator on the site
Thank you. I have the same issue I want to print pages based on cell value. Lets say variable changes i.e page 1-5 and then some time 2-6. So I have a field which will calculate and will put that in text format as suggested.But for some reason it doesnt work for me. It doesnt prints any pages.Can you please advise
This comment was minimized by the moderator on the site
Did you ever figure out how to get this working?
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations