How to set a VPageBreak between two specific columns?

冷暖自知 提交于 2020-01-25 19:03:10

问题


I programmed a little tool in VBA which generates a sheet with some statistics on it. I want to automatically have a printable worksheet, so I set the VPageBreak like this:

ActiveWindow.View = xlPageBreakPreview
ActiveSheet.VPageBreaks(1).DragOff Direction:=xlToRight, RegionIndex:=1  
ActiveWindow.View = xlNormalView

At first this worked just fine. But the content of this generated worksheet is based on data in another worksheet and when this data changed, the DragOff-Command threw an error. The error occured because the VPageBreak is now automatically set at a place, where the Dragoff is not possible.

I need reliable code, not depending on the origin-data... Is there a way to say "I want this VPageBreak after column E"?

Thanks in advance


回答1:


To get you started, try this ...

Sub SetPageBreaks()
Dim MySh As Worksheet

    Set MySh = ActiveWorkbook.Worksheets("Sheet1")
    MySh.VPageBreaks.Add MySh.[E1]
    MySh.VPageBreaks.Add MySh.[J1]

    MySh.HPageBreaks.Add MySh.[A5]
    MySh.HPageBreaks.Add MySh.[A10]

    ' removing breaks

    ' note ... page breaks are only counted and accessible as objects
    ' when they intersect with a Print range
    MySh.PageSetup.PrintArea = "A1:J10"
    MySh.HPageBreaks(1).Delete

End Sub

This adds vertical page seperations before columns E and J. No need to make VBA switch into PageBreakView, just set the VPageBreaks property.

You guessed it, ther's also a HPageBreak property for the Worksheet object ...

further reading



来源:https://stackoverflow.com/questions/25719060/how-to-set-a-vpagebreak-between-two-specific-columns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!