Select Entire Sheets for Printing: Excel VBA -


i created vba macro export specific sheets of workbook pdf. simple enough. problem experiencing code selects part of sheet4, in pdf part of sheet missing.

sheet4 contains data in range a1:w80. however, when running code below range a1:w75 selected printing. have confirmed printarea contains entire page. looks great when printing.

i have searched endlessly solution, no success. page-layout setting issue? how can ensure selecting entire sheet, instead of part of it, when exporting pdf?

here code:

sub savereportpdf()      dim filepath string      filepath = "abc"      thisworkbook.sheets(array("sheet1", "sheet2", "sheet3", "sheet4")).select     selection.exportasfixedformat _         type:=xltypepdf, _         filename:=filepath, _         quality:=xlqualitystandard, _         includedocproperties:=true, _         ignoreprintareas:=false, _         openafterpublish:=false  end sub 

as practise, can set used range of each sheet print area, , fit page, using following sub:

sub scaleforprinting()     dim sh worksheet     ' stop printcommunication speed     application.printcommunication = false     ' cycle through each sheet     each sh in thisworkbook.sheets(array("sheet1", "sheet2", "sheet3", "sheet4"))         ' set print area used range of sheet         sh.pagesetup.printarea = sh.usedrange         ' remove zoom, scale sheet fit 1 page         sh.pagesetup             .centerhorizontally = true             .centervertically = true             .zoom = false             .fittopageswide = 1             .fittopagestall = 1         end     next sh     ' enable printcommunication apply settings     application.printcommunication = true end sub 

then want use activesheet object after selecting, rather selection object. perhaps counter-intuitive, want print sheets not you've selected in sheets.

so:

scaleforprinting thisworkbook.sheets(array("sheet1", "sheet2", "sheet3", "sheet4")).select activesheet.exportasfixedformat _     type:=xltypepdf, _     filename:=filepath, _     quality:=xlqualitystandard, _     includedocproperties:=true, _     ignoreprintareas:=false, _     openafterpublish:=false ' deselect sheets avoid nasty multiple sheet accidental editing! thisworkbook.sheets("sheet1").select 

Comments

Popular posts from this blog

inversion of control - Autofac named registration constructor injection -

verilog - Systemverilog dynamic casting issues -

ios - Change Storyboard View using Seague -