Page break after keyword?

MARK858

MrExcel MVP
Joined
Nov 12, 2010
Messages
16,816
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
Hi there, I have found quite a lot of code for inserting page breaks before cells or specific values (like the code below for example, provided by Domenic in a previous thread).

I want to do something very similar but my problem is the only unique keyword I can find in the right position needs to be above the page breaks (i.e. after the keyword).

All the other criteria are the same i.e. the word is in a single column (say "A") and it is a single word (might as well stick to "A" as in Dominic's code).

Is this possible?

Code:
Option Explicit

Sub test()

    Dim FoundCell As Range
    Dim FirstAddress As String
    Dim PrevAddress As String
    Dim CurrAddress As String
    Dim SearchTerm As String

    SearchTerm = "A"

    With Columns("A")
        Set FoundCell = .Find(SearchTerm, LookIn:=xlValues, lookat:=xlWhole, MatchCase:=False)
        If Not FoundCell Is Nothing Then
            FoundCell.Name = "FirstAddress"
            Do
                PrevAddress = FoundCell.Address
                FoundCell.Resize(3).EntireRow.Insert
                ActiveSheet.HPageBreaks.Add before:=Range(PrevAddress)
                Set FoundCell = .FindNext(FoundCell)
            Loop While FoundCell.Address <> Range("FirstAddress").Address
        Else
            MsgBox "No search term found...", vbExclamation
        End If
    End With
        
End Sub
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Code:
Sub AddPageBreakAfterKeywordInColumnA()
    Dim FoundCell As Range
    Dim FirstAddress As String
    Dim PrevAddress As String
    Dim SearchTerm As String
    SearchTerm = "A"
    RemoveExistingManualPageBreaks
 
    With Columns("A")
        Set FoundCell = .Find(SearchTerm, LookIn:=xlValues, lookat:=xlWhole, MatchCase:=False)
        If Not FoundCell Is Nothing Then
            FoundCell.Name = "FirstAddress"
            Do
                PrevAddress = FoundCell.Address
                ActiveSheet.HPageBreaks.Add before:=Range(PrevAddress).Offset(1, 0)
                Set FoundCell = .FindNext(FoundCell)
            Loop While FoundCell.Address <> Range("FirstAddress").Address
        Else
            MsgBox "No search term found...", vbExclamation
        End If
    End With
End Sub
 
Sub RemoveExistingManualPageBreaks()
    Dim lX As Long
    For lX = ActiveSheet.HPageBreaks.Count To 1 Step -1
        ActiveSheet.HPageBreaks(lX).Delete
    Next
End Sub
 
Upvote 0
Sorry Phil, got wrapped up in a project and forgot I hadn't repled.
All works exactly as requested (All I have to do now (because someone has asked) is work out if you can do the same in word..ah well).
Thanks for all your solution and help
 
Upvote 0
Would the word appear alone in a paragraph? Or should the page break trigger no patter where it showed.
It would be easy to do a search and replace by using
Keyword^p as the Find What and
Keyword^m as the Replace With

That would replace the keyword followed by a paragraph mark with the keyword followed by a manual page break
 
Upvote 0
In this case it actually is 3 words "End of report" but it does appear by itself. afraid I am on my phone at present. and so I won't be able to test anything until tonight
 
Upvote 0
Cheers Phil, have got the page breaks sorted.
Now to learn a bit more about Word VBA.
Though I must admit at first look I much prefer Excel VBA, but you never know it might grow on me.
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,729
Members
452,939
Latest member
WCrawford

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top