Excel

Insert a Page Break Above All Cells With Specific Text

Ease of Use

Easy

Version tested with

2002 

Submitted by:

Jacob Hilderbrand

Description:

This macro will make a page break above each row that has a cell with a specified value. 

Discussion:

You want to add a page break above all occurance of a word, phrase, or value. This macro will do it automatically based on what you input. 

Code:

instructions for use

			

Option Explicit Sub PageBreaks() Dim c As Range Dim FirstAddress As String Dim Search As String Dim Prompt As String Dim Title As String Prompt = "What do you want to search for?" Title = "Search Term Input" Search = InputBox(Prompt, Title) If Search = "" Then Exit Sub End If With ActiveSheet.UsedRange Set c = .Find(What:=Search, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=True) If Not c Is Nothing Then FirstAddress = c.Address Do ActiveWindow.SelectedSheets.HPageBreaks.Add Before:=c Set c = .FindNext(c) Loop While Not c Is Nothing And c.Address <> FirstAddress End If End With End Sub

How to use:

  1. Open Excel.
  2. Alt + F11 to open the VBE.
  3. Insert | Module.
  4. Paste the code there.
  5. Close the VBE (Alt + Q or press the X in the top right corner).
 

Test the code:

  1. Tools | Macro | Macros...
  2. Select PageBreaks and press Run.
 

Sample File:

PageBreaks.zip 8.16KB 

Approved by mdmackillop


This entry has been viewed 125 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express