跳到主要内容

如何基于Excel中的单元格值打印某些页面或工作表?

您是否曾经尝试过基于Excel中的单元格值打印某些页面或工作表? 以及如何实现呢? 本文中的方法可以帮助您详细解决它。

使用VBA代码根据单元格值打印某些页面
使用VBA代码根据单元格值打印某些工作表


使用VBA代码根据单元格值打印某些页面

例如,当您在当前工作表的单元格A1中输入页码1时,您希望自动打印某个页面,例如当前工作表的页面1。 您可以执行以下操作来实现它。

1.选择包含要打印的页码的单元格,然后按 其他F11 同时打开 Microsoft Visual Basic应用程序 窗口。 然后点击 插页 > 模块.

2.然后 Microsoft Visual Basic应用程序 弹出窗口,请复制以下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 键来运行代码。 在弹出 Kutools for Excel 对话框中,单击 按钮以打印特定页面,或单击 没有 如果您不想打印,请按按钮退出对话框。 看截图:

然后将打开当前工作表的特定页面的预览窗口,请单击 打印 按钮开始打印。


使用VBA代码根据单元格值打印某些页面

假设您要在此工作表的单元格B2中的值等于1001时打印活动工作表。您可以使用以下VBA代码来实现它。 请执行以下操作。

1.右键单击 工作表标签 您要基于单元格值打印的内容,然后单击 查看代码 从右键单击菜单中。

2.然后 Microsoft Visual Basic应用程序 弹出窗口,请复制以下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应用程序 窗口。

在单元格B1001中输入数字2时, Kutools for Excel 弹出对话框,请点击 按钮开始打印。 或点击 没有 按钮直接退出对话框而不进行打印。 看截图:

提示: 如果要直接打印当前工作表中的非连续页面(例如第1、6和9页),或仅打印所有偶数或奇数页,则可以尝试使用 打印指定页面 实用程序pf Kutools for Excel 如下图所示。 你可以去 30天内免费下载软件,无限制.


相关文章:

最佳办公生产力工具

🤖 Kutools 人工智能助手:基于以下内容彻底改变数据分析: 智能执行   |  生成代码  |  创建自定义公式  |  分析数据并生成图表  |  调用 Kutools 函数...
热门特色: 查找、突出显示或识别重复项   |  删除空白行   |  合并列或单元格而不丢失数据   |   不使用公式进行四舍五入 ...
超级查询: 多条件VLookup    多值VLookup  |   跨多个工作表的 VLookup   |   模糊查询 ....
高级下拉列表: 快速创建下拉列表   |  依赖下拉列表   |  多选下拉列表 ....
列管理器: 添加特定数量的列  |  移动列  |  切换隐藏列的可见性状态  |  比较范围和列 ...
特色功能: 网格焦点   |  设计图   |   大方程式酒吧    工作簿和工作表管理器   |  资源库 (自动文本)   |  日期选择器   |  合并工作表   |  加密/解密单元格    按列表发送电子邮件   |  超级筛选   |   特殊过滤器 (过滤粗体/斜体/删除线...)...
前 15 个工具集12 文本 工具 (添加文本, 删除字符,...)   |   50+ 图表 类型 (甘特图,...)   |   40+ 实用 公式 (根据生日计算年龄,...)   |   19 插入 工具 (插入二维码, 从路径插入图片,...)   |   12 转化 工具 (小写金额转大写, 货币兑换,...)   |   7 合并与拆分 工具 (高级组合行, 分裂细胞,...)   |   ... 和更多

使用 Kutools for Excel 增强您的 Excel 技能,体验前所未有的效率。 Kutools for Excel 提供了 300 多种高级功能来提高生产力并节省时间。  单击此处获取您最需要的功能...

产品描述


Office Tab 为 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