跳到主要内容

在Excel中更改值时如何插入分页符?

假设我有一个单元格区域,现在,当A列中的值发生变化时,我想在工作表中插入分页符,如左图所示。 当然,您可以一个接一个地插入分页符,但是有没有一种快速的方法可以根据一列的更改值一次插入分页符?

使用VBA代码更改值时插入分页符

借助功能强大的功能更改值时插入分页符


使用VBA代码更改值时插入分页符

以下VBA代码可以帮助您根据列数据的更改在下面插入分页符,请执行以下操作:

1. 按住 ALT + F11 键打开 Microsoft Visual Basic应用程序 窗口。

2。 点击 插页 > 模块,然后将以下代码粘贴到 模块 窗口。

VBA代码:值更改时插入分页符:

Sub insertpagebreaks()
'updateby Extendoffice
    Dim I As Long, J As Long
    J = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
    For I = J To 2 Step -1
        If Range("A" & I).Value <> Range("A" & I - 1).Value Then
            ActiveSheet.HPageBreaks.Add Before:=Range("A" & I)
        End If
    Next I
End Sub

3。 然后按 F5 键以运行此代码,当A列中的数据更改时,所有分页符都已插入到工作表中。请参见屏幕截图:

请注意:在上面的代码中, A 是要基于其插入分页符的列标题,可以根据需要进行更改。

借助功能强大的功能更改值时插入分页符

如果你有 Kutools for Excel,其 区分差异 功能,您可以根据值更改快速插入分页符,空白行,底部边框线或背景颜色。 点击下载Kutools for Excel!

提示:申请这个 区分差异 功能,首先,您应该下载 Kutools for Excel,然后快速轻松地应用该功能。

安装后 Kutools for Excel,请这样做:

1。 点击 库工具 > 格式 > 区分差异,请参见屏幕截图:

2。 在弹出 按关键列区分差异 对话框中,进行以下操作:

  • 选择要使用的数据范围和键列;
  • 分页符 来自 附加选项 框。

3。 然后点击 Ok 按钮,分页符已根据值更改插入到范围中,请参见屏幕截图:

立即下载并免费试用 Kutools for Excel!


更多相关文章:

  • 在Excel中单元格值更改时突出显示行
  • 如果工作表中有重复值的列表,则需要突出显示基于列A的行,该行的单元格值会发生变化,如下图所示。 实际上,您可以使用条件格式设置功能快速轻松地完成此工作。
  • 当值在另一列中更改时增加数字
  • 假设您在A列中有一个值列表,现在想在A列中的值更改时在B列中将数字递增1,这意味着B列中的数字递增直到A列中的值更改,然后数字增量再次从1开始,如左图所示。 在Excel中,可以使用以下方法解决此任务。
  • 值在另一列中更改时对单元格求和
  • 有时,当您处理Excel工作表时,可能需要根据另一列中的一组数据对单元格求和。 例如,在这里,我想对A列中的数据更改时B列中的订单求和,以获得以下结果。 您如何在Excel中解决此问题?
  • 在Excel中值更改时插入空白行
  • 假设您具有一系列数据,现在您想在值更改时在数据之间插入空白行,以便可以将一列中的顺序相同值分开,如下面的屏幕截图所示。 在本文中,我将为您解决一些技巧。
  • 在Excel中单元格值更改时运行宏
  • 通常,在Excel中,我们可以按F5键或“运行”按钮执行VBA代码。 但是,您是否曾经尝试在单元格值更改时运行特定的宏代码? 本文中,我将介绍一些快速技巧来处理Excel中的这项工作。

最佳办公生产力工具

🤖 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 (8)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Bonjour,

Merci pour le code qui fonctionne très bien, seulement je souhaite ajouter un saut de page après et non avant chaque changement de valeur... que faut-il changer dans le code ?

Merci beaucoup !
This comment was minimized by the moderator on the site
Tôi không muốn ngắt trang tại những hàng bị ẩn. Thì phải làm sao?
This comment was minimized by the moderator on the site
Hello, Lê Tuấn
To solve your problem, please apply the below VBA code:
Sub insertpagebreaks()
'updateby Extendoffice
Dim I As Long, J As Long
Dim xRg As Range
On Error Resume Next
J = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
Set xRg = Range("A1:A" & J).SpecialCells(xlCellTypeVisible)

For I = J To 2 Step -1
If Range("A" & I).Value <> Range("A" & I - 1).Value Then
If Not Intersect(xRg, Range("A" & I)) Is Nothing Then
ActiveSheet.HPageBreaks.Add Before:=Range("A" & I)
End If
End If
Next I
End Sub


Please try, hope it can help you!
This comment was minimized by the moderator on the site
Is it possible to skip the first 20 rows of the sheet? If been trying all kinds of stuff but my knowledge of VBA is to limited to adjuist the code myself.
This comment was minimized by the moderator on the site
Hi, Wilco,
To insert the page break but skip the first 20 rows, you just need to change one parameter as below code:

Sub insertpagebreaks()

'updateby Extendoffice

Dim I As Long, J As Long

J = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row

For I = J To 21 Step -1

If Range("A" & I).Value <> Range("A" & I - 1).Value Then

ActiveSheet.HPageBreaks.Add Before:=Range("A" & I)

End If

Next I

End Sub

Please try, thank you!
This comment was minimized by the moderator on the site
That is truly brilliant! I followed along on your VB example and it worked! I also am an owner of KUTools so I will play with that too.

Very impressed and thanks!

Mike
This comment was minimized by the moderator on the site
Sorry. EDIT: The first ROW is a column header.
This comment was minimized by the moderator on the site
Hello, Thank you! The thought of VBA makes me sweat. I do have one problem with the results of this code though. The first column is a column header. The code treats this as a change. My first page only shows the column header, but he following pages are fine. FYI: This is for a monthly report and the number of rows for each criteria change monthly. Any Ideas? Thank you in advance.
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations