Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Page Break Preview?

Tags:

excel

vba

I want to automatically set my page break preview.

I tried

Worksheets("Stack").Range("A2:M33").PageBreak = xlPageBreakManual

I get

Unable to set Pagebreak Property of the range Class

like image 342
excelguy Avatar asked Oct 28 '25 13:10

excelguy


1 Answers

I know it's almost two years late, but just not to leave this unanswered. I did the following:

Dim WSh As Worksheet, Rng1 As Range, Rng2 As Range

Set WSh = Sheets("Stack")
    Set Rng1 = Range("A5")
    Set Rng2 = Range("N34")

WSh.ResetAllPageBreaks
    'From
    If Rng1.Row <> 1 Then WSh.HPageBreaks.Add before:=Rng1
    If Rng1.Column <> 1 Then WSh.VPageBreaks.Add before:=Rng2
    'To
    WSh.HPageBreaks.Add before:=Rng2
    WSh.VPageBreaks.Add before:=Rng2

That worked. You would just need to ensure some handling when you manage column "A" or row "1", but that can be easily sorted out with a couple Ifs.

like image 152
Felipe Vidal Avatar answered Oct 30 '25 05:10

Felipe Vidal