How to force Excel 2010 with VBA to recalculate page breaks ?

Hi friends,

I do some Excel (2010) VBA magic to fill a worksheet with data, set a print area depeding on the size of gathered data (with aome data outside of tha print area) and afterwards I'd like to fiddle around with page breaks for conditional formatting issues.

To make that work correct, I need torce Excel (with VBA) to recalculate the (horizontal) page breaks, but I found no reliable way to do so.

Any hint ?
LVL 14
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Roy CoxGroup Finance ManagerCommented:
To do this with VBA you need to know where the breaks need to be.
frankhelkAuthor Commented:
Sorry, but not really ... I'd be completely satisfied with automatic page breaks.
But I need to know where they are.

They are (or closer defined: should be ....) listed in ActiveSheet.HPageBreaks, but that list is usually not up-to-date.

What I need is a way for brute-force Excel to recalculate that list of page breaks.
Roy CoxGroup Finance ManagerCommented:
The only way that | can think of would be to check and make a note of some page breaks in a standard document and then use that as a template.
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Roy CoxGroup Finance ManagerCommented:
This is some code based on what I think you need. I've tested it on some large sets of data.

Sub Set_PrintArea_and_PageBreaks()
''/// suggested rows per sheet, adjust to suit
    Const iRws As Integer = 40
    Dim rRng As Range
    Dim iX As Integer, iBreaks As Integer
    Dim lRw As Long

    With ActiveSheet

        Set rRng = .Range("A1").CurrentRegion
        lRw = rRng.Rows.Count
        ''/// clear existing page breaks

        If lRw > iRws Then
            iBreaks = Int(lRw / iRws)
            For iX = 1 To iBreaks
                .HPageBreaks.Add (rRng.Cells((iRws * iX) + 1, 1))
            Next iX
        End If
        .PageSetup.PrintArea = rRng.Address
    End With
End Sub

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ejgil HedegaardCommented:
Define the page orientation, and Excel adds the automatic page breaks

With ActiveSheet.PageSetup
    .Orientation = xlPortrait
End With

Open in new window

frankhelkAuthor Commented:
Thanks ...
Roy CoxGroup Finance ManagerCommented:
Pleased to help
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.