Skip to main content

How to insert page breaks when value changes in Excel?

Author: Xiaoyang Last Modified: 2020-06-03

Supposing, I have a range of cells, and now, I want to insert page breaks into the worksheet when values in column A changes as left screenshot shown. Of course, you can insert them one by one, but are there any quick ways to insert the page breaks at once based on the changed values of one column?

Insert page breaks when value changes with VBA code

Insert page breaks when value changes with a powerful feature


Insert page breaks when value changes with VBA code

The following VBA code can help you to insert page breaks below based on once column data changes, please do as follows:

1. Hold down the ALT + F11 keys to open the Microsoft Visual Basic for Applications window.

2. Click Insert > Module, and paste the following code in the Module Window.

VBA code: Insert page breaks when the value changes:

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. Then press F5 key to run this code, all page breaks have been inserted into the sheet when data changes in column A. See screenshot:

Note:In the above code, A is the column header that you want to insert page break based on, you can change it to your need.

Insert page breaks when value changes with a powerful feature

If you have Kutools for Excel, with its Distinguish differences feature, you can quickly insert page breaks, blank rows, bottom border lines or background color based on the value changes. Click to download Kutools for Excel!

Tips:To apply this Distinguish differences feature, firstly, you should download the Kutools for Excel, and then apply the feature quickly and easily.

After installing Kutools for Excel, please do as this:

1. Click Kutools > Format > Distinguish differences, see screenshot:

2. In the popped out Distinguish differences by key column dialog box, do the following operations:

  • Select the data range and key column that you want to use;
  • Choose Page Break from the Options box.

3. Then click Ok button, the page breaks have been inserted into the range based on the value changes see screenshot:

Download and free trial Kutools for Excel Now!


More relative articles:

  • Highlight Rows When Cell Value Changes In Excel
  • If there is a list of repeated values in your worksheet, and you need to highlight the rows based on column A which cell value changes as the following screenshot shown. In fact, you can quickly and easily finish this job by using the Conditional Formatting feature.
  • Increment Numbers When Value Changes In Another Column
  • Supposing, you have a list of values in column A, and now you want to increment number by 1 in column B when the value in column A changes, which means the numbers in column B increment until the value in column A changes, then the number increment starts from 1 again as left screenshot shown. In Excel, you can solve this job with the following method.
  • Sum Cells When Value Changes In Another Column
  • When you work on Excel worksheet, sometime, you may need to sum cells based on a group of data in another column. For example, here, I want to sum the orders in column B when the data changes in column A to get the following result. How could you solve this problem in Excel?
  • Insert Blank Rows When Value Changes In Excel
  • Supposing you have a range of data, and now you want to insert blank rows between the data when value changes, so that you can separate the sequential same values in one column as following screenshots shown. In this article, I will talk about some tricks for you to solve this problem.
  • Run Macro When Cell Value Changes In Excel
  • Normally, in Excel, we can press F5 key or Run button to execute the VBA code. But, have you ever tried to run the specific macro code when a cell value changes? This article, I will introduce some quick tricks to deal with this job in Excel.

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

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...

Description


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!
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