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

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
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,224,823
Messages
6,181,173
Members
453,021
Latest member
Justyna P

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