I'm trying to get a code which will automatically set the print view mode around a certain range of cells (selected cells - as it has to be dynamic) and remove the page number (PAGE 1 in black superposed on cells).
I have tried the following (below), however it doesn't work as it grabs all the sheet data and puts it on one page instead of just showing the selected range on one page.
Dim myRange As Range
Set myRange = Selection
ActiveWindow.View = xlPageBreakPreview
myRange.Select
Application.PrintCommunication = False
With ActiveSheet.PageSetup
.PrintTitleRows = ""
.PrintTitleColumns = ""
End With
Application.PrintCommunication = True
ActiveSheet.PageSetup.PrintArea = ""
Application.PrintCommunication = False
With ActiveSheet.PageSetup
.LeftHeader = ""
.CenterHeader = ""
.RightHeader = ""
.LeftFooter = ""
.CenterFooter = ""
.RightFooter = ""
.LeftMargin = Application.InchesToPoints(0.708661417322835)
.RightMargin = Application.InchesToPoints(0.708661417322835)
.TopMargin = Application.InchesToPoints(0.748031496062992)
.BottomMargin = Application.InchesToPoints(0.748031496062992)
.HeaderMargin = Application.InchesToPoints(0.31496062992126)
.FooterMargin = Application.InchesToPoints(0.31496062992126)
.PrintHeadings = False
.PrintGridlines = False
.PrintComments = xlPrintNoComments
.PrintQuality = 600
.CenterHorizontally = False
.CenterVertically = False
.Orientation = xlPortrait
.Draft = False
.PaperSize = xlPaperA4
.FirstPageNumber = xlAutomatic
.Order = xlDownThenOver
.BlackAndWhite = False
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = 1
.PrintErrors = xlPrintErrorsDisplayed
.OddAndEvenPagesHeaderFooter = False
.DifferentFirstPageHeaderFooter = False
.ScaleWithDocHeaderFooter = True
.AlignMarginsHeaderFooter = True
.EvenPage.LeftHeader.Text = ""
.EvenPage.CenterHeader.Text = ""
.EvenPage.RightHeader.Text = ""
.EvenPage.LeftFooter.Text = ""
.EvenPage.CenterFooter.Text = ""
.EvenPage.RightFooter.Text = ""
.FirstPage.LeftHeader.Text = ""
.FirstPage.CenterHeader.Text = ""
.FirstPage.RightHeader.Text = ""
.FirstPage.LeftFooter.Text = ""
.FirstPage.CenterFooter.Text = ""
.FirstPage.RightFooter.Text = ""
End With
Application.PrintCommunication = True
End Sub
So basically I want the selected cells to be highlighted (white) and put only on one page (no page breaks) and anything else in the sheet to be put outside of the print view (in that gray/black view).
Any thoughts would be welcome.
Thank you !
One thing to remember when recording macros is that default settings don't usually need to be mentioned, unless you're changing the figure.
So things like .LeftHeader = ""
aren't needed unless you're specifically removing text from the left header - by default it's blank, and this code is just saying keep it blank.
The page number superimposed on the sheet is just because you're in Page Break Preview (your code puts you in that at the start). You only need to look at that view if you're manually changing the print - using code just stay in the Normal view (it's rare you have to Select anything with VBA before working with it).
This code worked for me:
Sub Test()
Dim PrintRange As Range
Set PrintRange = Selection
With PrintRange.Parent.PageSetup 'The Parent of Selection is the sheet.
Application.PrintCommunication = True
.PrintArea = PrintRange.Address
Application.PrintCommunication = False
.FitToPagesWide = 1
.FitToPagesTall = 1
End With
End Sub