What would be the best vba approach to setting the page setup as follows.
When the print button is activated, I want to print to 4 pages landscape (1cm margins).
Range A1:H91 - Pages 1 and 2
Range A93:H155 - Page 3
Range A157:H218 - Page 4.
Apologies if this seems a dull question, but I'm not even sure how to do this manually, and if I did, I know that the recorder spits out drivell in these situations.
Thanks for any help.
vba page-setup
-
- 5StarLounger
- Posts: 1185
- Joined: 24 Jan 2010, 12:02
- Location: Wales, UK.
vba page-setup
Nathan
There's no place like home.....
There's no place like home.....
-
- Administrator
- Posts: 78524
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: vba page-setup
Try code like this (you'll have to modify it to suit your needs):
ActiveSheet.HPageBreaks.Add Before:=Range("A93")
Set ActiveSheet.HPageBreaks(3).Location = Range("A157")
ActiveSheet.HPageBreaks.Add Before:=Range("A93")
Set ActiveSheet.HPageBreaks(3).Location = Range("A157")
Best wishes,
Hans
Hans
-
- 5StarLounger
- Posts: 1185
- Joined: 24 Jan 2010, 12:02
- Location: Wales, UK.
Re: vba page-setup
Hans, I get a 'run time error 9' on the second line. When previewing the print, the page break does not seem to do anything.
I don't know how I am to use this code, would you mind elaborating slightly. Thanks
I don't know how I am to use this code, would you mind elaborating slightly. Thanks
Nathan
There's no place like home.....
There's no place like home.....
-
- Administrator
- Posts: 78524
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: vba page-setup
Why do you want to do this in code? Wouldn't it be easier to set the page breaks in page break view?
Best wishes,
Hans
Hans
-
- 5StarLounger
- Posts: 1185
- Joined: 24 Jan 2010, 12:02
- Location: Wales, UK.
Re: vba page-setup
This is one of several daily text files that I receive that need to be converted to excel, reformatted etc.... and printed. I am trying to complete the entire task programatically.
Nathan
There's no place like home.....
There's no place like home.....
-
- Administrator
- Posts: 78524
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: vba page-setup
I'd switch to page break view, record a macro of setting the page breaks correctly, and when you've stopped recording, edit out the superfluous code.
Best wishes,
Hans
Hans
-
- 5StarLounger
- Posts: 1185
- Joined: 24 Jan 2010, 12:02
- Location: Wales, UK.
Re: vba page-setup
I was not familiar with working in page break view, but I am now.
Got there eventually...
Thanks.
Got there eventually...
Code: Select all
ActiveWindow.View = xlPageBreakPreview
ActiveSheet.PageSetup.PrintArea = "$A$1:$H$218"
Set ActiveSheet.HPageBreaks(1).Location = Range("A69")
Set ActiveSheet.HPageBreaks(2).Location = Range("A93")
Set ActiveSheet.HPageBreaks(3).Location = Range("A157")
Nathan
There's no place like home.....
There's no place like home.....
-
- Administrator
- Posts: 78524
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: vba page-setup
You could reset the original view, and minimize screen updating:
Code: Select all
Dim lngView As Long
Application.ScreenUpdating = False
' Store the current view
lngView = ActiveWindow.View
' Switch to page break view
ActiveWindow.View = xlPageBreakPreview
ActiveSheet.PageSetup.PrintArea = "$A$1:$H$218"
Set ActiveSheet.HPageBreaks(1).Location = Range("A69")
Set ActiveSheet.HPageBreaks(2).Location = Range("A93")
Set ActiveSheet.HPageBreaks(3).Location = Range("A157")
' Restore the original view
ActiveWindow.View = lngView
Application.ScreenUpdating = True
Best wishes,
Hans
Hans
-
- 5StarLounger
- Posts: 1185
- Joined: 24 Jan 2010, 12:02
- Location: Wales, UK.