To delete all page breaks in Excel using VBA, use the ResetAllPageBreaks method on the target worksheet. This removes both manual and automatic page breaks, ensuring consistent printing layout. No external tools or add-ins are required—just Excel with VBA enabled.
What You'll Need
- Microsoft Excel (2010 or later; works in 365, 2016, 2019, and 2021)
- A workbook with at least one worksheet containing page breaks
- VBA enabled (macro settings must allow code execution)
- Basic familiarity with the VBA editor (Alt+F11)
Step-by-Step
- Open your Excel workbook and ensure it is saved in a macro-enabled format (.xlsm) if you plan to keep the code. If the file is .xlsx, save a copy as .xlsm before proceeding.
- Press Alt + F11 to open the Visual Basic for Applications (VBA) editor.
- In the Project Explorer window (top-left), locate your workbook. If the Project Explorer is hidden, press Ctrl + R to display it.
- Right-click on the workbook name or any existing module, then choose Insert > Module to create a new blank module.
- Copy and paste the following VBA code into the module window:
This loop iterates through every worksheet in the active workbook and removes all page breaks.Sub DeleteAllPageBreaks() Dim ws As Worksheet For Each ws In ActiveWorkbook.Worksheets ws.ResetAllPageBreaks Next ws End Sub - Press F5 while in the VBA editor to run the macro, or return to Excel and run it via Alt + F8, selecting DeleteAllPageBreaks and clicking Run.
- Verify success by switching to Page Layout view (View > Page Layout). You should see no horizontal or vertical break lines (dashed blue lines) between pages. Alternatively, go to File > Print to preview the layout.
Common Mistakes
- Running the macro on the wrong workbook: Ensure the correct workbook is active when running the code. The macro operates on
ActiveWorkbook, not a specific saved file. - Macro security blocking execution: If Excel disables macros, the code won’t run. Go to File > Options > Trust Center > Trust Center Settings > Macro Settings, and select Enable all macros (temporarily, for testing).
- Using
ActiveWorksheetinstead of looping: A common error is writing code likeActiveSheet.ResetAllPageBreakswithout looping, which only clears breaks on the currently selected sheet—not all sheets. - Confusing manual and automatic breaks: Manual page breaks (inserted via Page Break Preview) behave differently than automatic ones.
ResetAllPageBreaksclears both, but only if applied correctly.
Tips
- Backup first: Save a copy of your workbook before running the macro. Though safe, unexpected corruption can occur in large or complex files.
- Target specific sheets only: To delete breaks on selected sheets, replace the loop with explicit sheet references, e.g.,
Worksheets("Sheet1").ResetAllPageBreaks. - Combine with print settings: After clearing breaks, use
ws.PageSetup.PrintArea = ""to remove print areas if needed for full-page layout control. - Use Page Layout view to verify: Dashed blue lines indicate page breaks. Their absence confirms success. Print Preview (Ctrl+P) also shows this visually.
- Automate with a button: Insert a form control button (Developer tab > Insert > Button), assign the macro, and click it for one-click cleanup.
Troubleshooting
If the macro doesn’t remove page breaks, follow these steps:
- Check if the macro ran: Look for a blank status bar after running. If Excel shows no error but breaks remain, ensure the code executed—add
MsgBox "Page breaks reset on " & ws.Nameinside the loop for confirmation. - Verify macro security: Go to File > Options > Trust Center > Macro Settings. Confirm macros are not disabled. If Disable all macros without notification is selected, enable them temporarily.
- Test on a new workbook: Create a blank workbook, insert a manual page break (Page Layout > Breaks > Insert Page Break), then run the macro. If it works here, the issue is file-specific (e.g., protected sheets).
- Check for sheet protection: If a worksheet is protected,
ResetAllPageBreaksmay fail silently. Unprotect sheets first (Review > Unprotect Sheet), then run the macro. To automate this, modify the code:
ReplaceSub DeleteAllPageBreaksWithUnprotect() Dim ws As Worksheet For Each ws In ActiveWorkbook.Worksheets ws.Unprotect Password:="" 'leave quotes empty if no password ws.ResetAllPageBreaks Next ws End Sub""with your actual password if protected. - Restart Excel: Rarely, Excel’s VBA engine locks up. Close all Excel windows, reopen, and retry.
Frequently Asked Questions
Does ResetAllPageBreaks affect page setup settings like margins or orientation?
No. It only clears horizontal and vertical break lines. Page setup properties (e.g., PageSetup.Orientation, PageSetup.Zoom) remain unchanged.
Can I delete only manual page breaks and keep automatic ones?
Not directly with a single method. Usews.HPageBreaks.Count and ws.VPageBreaks.Count to identify manual breaks, then remove them individually via ws.HPageBreaks(i).Delete or ws.VPageBreaks(i).Delete in a loop. Automatic breaks are recalculated by Excel and cannot be preserved while deleting manual ones.
Why does the macro run but page breaks reappear after saving and reopening?
Excel automatically reinserts page breaks based on paper size, scale, and print area when the workbook is opened. To prevent this, ensure no print areas are set (ws.PageSetup.PrintArea = "") and check Page Layout > Page Setup > Scale for consistent settings.