VBA to insert page break above specific text

Hwatsonnn

Board Regular
Joined
Nov 14, 2018
Messages
58
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Down column B when there is the letter "i", or the letter "g" found I would like a Page Break to be inserted above the cell in which it is found.

Thanks for any suggestions :)
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Try this

VBA Code:
Sub InsertPageBreak()
    Dim cel As Range
        With ActiveSheet
            .ResetAllPageBreaks
            For Each cel In .Range("B2", .Range("B" & .Rows.Count).End(xlUp))
                If cel = "i" Or cel = "g" Then .HPageBreaks.Add Before:=.Rows(cel.Row)
            Next cel
        End With
End Sub
 
Upvote 0
Try this

VBA Code:
Sub InsertPageBreak()
    Dim cel As Range
        With ActiveSheet
            .ResetAllPageBreaks
            For Each cel In .Range("B2", .Range("B" & .Rows.Count).End(xlUp))
                If cel = "i" Or cel = "g" Then .HPageBreaks.Add Before:=.Rows(cel.Row)
            Next cel
        End With
End Sub
Sorry for the late response, just tried it and it did not work
 
Upvote 0
Try this

VBA Code:
Sub InsertPageBreak()
    Dim cel As Range
        With ActiveSheet
            .ResetAllPageBreaks
            For Each cel In .Range("B2", .Range("B" & .Rows.Count).End(xlUp))
                If cel = "i" Or cel = "g" Then .HPageBreaks.Add Before:=.Rows(cel.Row)
            Next cel
        End With
End Sub
sorry I forgot to say there isnt just an "i" or a "g", it is one of those letters followed by numbers or sometimes text.
e.g "i10" "g12" "itop". Apologies
 
Upvote 0
VBA Code:
Sub InsertPageBreak()
    Dim cel As Range, x As String
        With ActiveSheet
            .ResetAllPageBreaks
            For Each cel In .Range("B2", .Range("B" & .Rows.Count).End(xlUp))
                x = LCase(Left(cel.Value, 1))
                If x = "i" Or x = "g" Then .HPageBreaks.Add Before:=.Rows(cel.Row)
            Next cel
        End With
End Sub
 
Upvote 0
VBA Code:
Sub InsertPageBreak()
    Dim cel As Range, x As String
        With ActiveSheet
            .ResetAllPageBreaks
            For Each cel In .Range("B2", .Range("B" & .Rows.Count).End(xlUp))
                x = LCase(Left(cel.Value, 1))
                If x = "i" Or x = "g" Then .HPageBreaks.Add Before:=.Rows(cel.Row)
            Next cel
        End With
End Sub
Hello just managed to get round to this sorry, the vba highlights ".HPageBreaks.Add Before:=.Rows(cel.Row)" in yellow as an error.

Thanks for your time :)
 
Upvote 0
Hello just managed to get round to this sorry, the vba highlights ".HPageBreaks.Add Before:=.Rows(cel.Row)" in yellow as an error.

Thanks for your time :)
application defined or object defined error
 
Upvote 0

Forum statistics

Threads
1,214,940
Messages
6,122,356
Members
449,080
Latest member
Armadillos

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