VBA Page Break Based off Cell Value in Column

bmkelly

Board Regular
Joined
Mar 26, 2020
Messages
172
Office Version
  1. 365
Platform
  1. Windows
Hello,

I currently have this code which works no problem but I am wondering if I can somehow get the code to look for the Header (in row 1) that contains "Transaction Date" and then look in the column for *Total* and then create a Page Break below.

VBA Code:
Sub PageBreak()

ActiveSheet.ResetAllPageBreaks

 Dim fRng As Range, Faddr As String

    With ActiveSheet.Range("R2:R" & ActiveSheet.Range("R" & Rows.Count).End(xlUp).Row)

        Set fRng = .Cells.Find(What:="*Total*", LookIn:=xlFormulas, _
                               LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext)

        If Not fRng Is Nothing Then
            Faddr = fRng.Address
            Do
                If Not fRng Is Nothing Then
                    fRng.Offset(1).PageBreak = xlPageBreakManual
                End If

                Set fRng = .FindNext(fRng)

                If fRng Is Nothing Then
                    Exit Do
                End If

                If fRng.Address = Faddr Then
                    Exit Do
                End If

            Loop
        End If
    End With
End Sub

Thanks!
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Try this:

VBA Code:
Sub PageBreak()
  Dim hdr As Range, f As Range, r As Range
  Dim cell As String
  Set hdr = Rows(1).Find("Transaction Date", , xlValues, xlWhole, , , False)
  If Not hdr Is Nothing Then
    Set r = Columns(hdr.Column)
    Set f = r.Find("Total", , xlValues, xlPart, , , False)
    If Not f Is Nothing Then
      cell = f.Address
      Do
        f.Offset(1).PageBreak = xlPageBreakManual
        Set f = r.FindNext(f)
      Loop While Not f Is Nothing And f.Address <> cell
    End If
  End If
End Sub
 
Upvote 0
Solution
Try this:

VBA Code:
Sub PageBreak()
  Dim hdr As Range, f As Range, r As Range
  Dim cell As String
  Set hdr = Rows(1).Find("Transaction Date", , xlValues, xlWhole, , , False)
  If Not hdr Is Nothing Then
    Set r = Columns(hdr.Column)
    Set f = r.Find("Total", , xlValues, xlPart, , , False)
    If Not f Is Nothing Then
      cell = f.Address
      Do
        f.Offset(1).PageBreak = xlPageBreakManual
        Set f = r.FindNext(f)
      Loop While Not f Is Nothing And f.Address <> cell
    End If
  End If
End Sub
Thank you! This worked perfectly.
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
I have a similar problem, I wonder if you could give this a shot?

I have a varied list of stock numbers. After sorting, I would like to insert a page break after each change in stock number.

Thanks in advance.
 
Upvote 0
I have a similar problem, I wonder if you could give this a shot?

I have a varied list of stock numbers. After sorting, I would like to insert a page break after each change in stock number.
The problem is similar, you need a page break. But it represents a new code. Create a new thread.
 
Upvote 0

Forum statistics

Threads
1,214,549
Messages
6,120,149
Members
448,948
Latest member
spamiki

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