How To Add page breaks to sorted data using VBA in Microsoft Excel?
Tags: toaddpagebreakstosor, vbamacros
Published: 2021-10-04 07:22:40

---
title: How To Add page breaks to sorted data using VBA in Microsoft Excel?
published: true
date: 2021-10-01 11:32:14 UTC
tags: ToAddPageBreaksToSor,VBAMacros
canonical_url: https://geekexcel.com/how-to-add-page-breaks-to-sorted-data-using-vba-in-microsoft-excel/
---

In this article, you will learn how to  **add page breaks to sorted data using VBA** in Microsoft Excel. Let’s see them below!! Get the official version of  **MS Excel**  from the following link: [https://www.microsoft.com/en-in/microsoft-365/excel](https://www.microsoft.com/en-in/microsoft-365/excel)
## Example

- Firstly, you need to **create a sample data** with **Agent name, Client name, Phone number and Email id.**

![](https://geekexcel.com/wp-content/uploads/2021/10/Sample-data-2.png)<figcaption>Sample data</figcaption>

- In the Excel Worksheet, you have to go to the  **Developer Tab.**
- Then, you need to ** ** select the  **Visual Basic**  option under the  **Code**  section.

![Select Visual Basic](https://geekexcel.com/wp-content/uploads/2021/09/Select-Visual-Basic-9-1024x180.png)<figcaption>Select Visual Basic</figcaption>

- Now, you have to  **copy and paste the code**  given below.

```

Sub InsertingPagebreak()
'Declaring variables
Dim LngCol As Long
Dim LngRow, MaxRow As Long
'Clear existing page breaks
ActiveSheet.ResetAllPageBreaks
LngCol = 1
'Getting row number of last cell
MaxRow = Range("A11").SpecialCells(xlCellTypeLastCell).Row
'Looping through all the rows starting from thirteenth row
For LngRow = 13 To MaxRow
    'Comparing values in cell of two consecutive rows for the specified column
    If Cells(LngRow, LngCol).Value <> Cells(LngRow - 1, LngCol).Value Then
        
        'Inserting page break
        ActiveWindow.SelectedSheets.HPageBreaks.Add Before:=Cells(LngRow, LngCol)
                                      
    End If    
Next LngRow
End Sub

```

- You need to  **save the code**  by selecting it and then  **close the window**.

![](https://geekexcel.com/wp-content/uploads/2021/10/Save-the-Code-1-1024x691.png)<figcaption>Save the Code</figcaption>

- Again, you have to go to the  **Excel Spreadsheet** , and click on the  **Developer Tab**.
- You need to choose the  **Macros option**  in the Code section.

![Choose Macro option](https://geekexcel.com/wp-content/uploads/2021/09/Choose-Macro-option-11-1024x184.png)<figcaption>Choose Macro option</figcaption>

- Now, you have to make sure that your  **macro name is selected**  and click the  **Run ** button.

![](https://geekexcel.com/wp-content/uploads/2021/10/Run-the-Code-2.png)<figcaption>Run the Code</figcaption>

- Finally, you will receive the  **text in header**  when printing as given below.

![](https://geekexcel.com/wp-content/uploads/2021/10/Output-2.png)<figcaption>Output</figcaption>

## Verdict

From this chapter, you can get some clarification about how to  **add page breaks to sorted data using VBA** in Microsoft Excel. Let me know if you have any  **doubts**  regarding this article or any other Excel/VBA topic.

Thank you so much for visiting  **[Geek Excel](https://geekexcel.com/)!! **If you want to learn more helpful formulas, check out [**Excel Formulas**](https://geekexcel.com/excel-formula/) **!!**

**Read Also:**

- **[Excel Formulas to Count Line Breaks in A Cell ~ A Complete Guide!!](https://geekexcel.com/excel-formulas-to-count-line-breaks-in-a-cell/)**
- **[Excel Formulas to Remove the Line Breaks Between Two Text Strings!!](https://geekexcel.com/excel-formulas-to-remove-the-line-breaks-between-two-text-strings/)**
- **[How to Use Excel SORTBY Function in Excel Office 365?](https://geekexcel.com/how-to-use-excel-sortby-function-in-excel-office-365/)**
- **[Insert Page Break Every X Rows in the Worksheet in Excel Office 365!!](https://geekexcel.com/insert-page-break-every-x-rows-in-the-worksheet-in-excel-office-365/)**
- **[Excel Formulas: Basic Timesheet Formula with Breaks ~ (Quick Formula)!!](https://geekexcel.com/excel-formulas-basic-timesheet-formula-with-breaks/)**