VBA syntax for specifying page break locations based on cell contents

chraco

New Member
Joined
Sep 24, 2014
Messages
11
Hello,

I'd like to automate the process of moving page breaks within a fairly small pivot table. I just recorded the following macro while I manually adjusted page breaks to my liking. Here's the code:

ActiveWindow.View = xlPageBreakPreview
ActiveWindow.SmallScroll Down:=-33
ActiveSheet.HPageBreaks(3).DragOff Direction:=xlDown, RegionIndex:=1
Set ActiveSheet.HPageBreaks(2).Location = ActiveCell.Offset(-16, 0).Range("A1")
ActiveWindow.SmallScroll Down:=-87
Set ActiveSheet.HPageBreaks(1).Location = ActiveCell.Offset(-61, 0).Range("A1")
ActiveWindow.SmallScroll Down:=78
Set ActiveSheet.HPageBreaks(2).Location = ActiveCell.Offset(-9, 0).Range("A1")
ActiveWindow.SmallScroll Down:=-99

Rather than "ActiveCell.Offset(-61, 0). . ." etc., how can I instead specify a page break location based on the content of a cell? In the particular pivot table that I was working in, the top row (specified in the pivot table fields criteria) is LastName. So how could I specify page breaks at, for instance, Smith, Jones, Cooper?

Looking at the code and seeing "DragOff Direction," I'm guessing what I want to accomplish may be more complex, but I'll start there with my question.

Thanks so much for your time.

Raco
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Hello

Code:
Sub PBreaks()
Dim pt As PivotTable, s$, sh As Worksheet


Set sh = ActiveSheet
s = "Jan"                               ' name you want


Set pt = sh.PivotTables("pt5")          ' the table


ActiveWindow.View = xlPageBreakPreview


Set sh.HPageBreaks(1).Location = pt.TableRange1.Find(s, , xlValues, xlWhole)


End Sub
 
Upvote 0

Forum statistics

Threads
1,223,898
Messages
6,175,272
Members
452,628
Latest member
dd2

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