I am trying to make a macro that adjust the row and column height within the printarea so that they fit a certain number of pages before printing to pdf. The purpose of this macro is to generate the "same" pdf file no matter what computer (with differet resolution, print settings and so on) when printing.
The printing to pdf works well for me, but the fitting of the columns and rows not so much
I have realised that the following section adjusts the column width and that the input needed is in excel units:
Columns("A:HP").Select
Selection.ColumnWidth = x.xx
In my current setup the column width needs to be 2.14 (excel units). So far so good, now to determine what the width should be on any computer.
I get the page width in points:
pgWid = Application.InchesToPoints(8.267) '(8.267 beeing the width of A4 paper)
Subtract the margins and divide by 32 (beeing the number columns that I would like to fit on one page):
LeftMargin = ActiveSheet.PageSetup.LeftMargin
RightMargin = ActiveSheet.PageSetup.RightMargin
cWid = (pgWid - LeftMargin - RightMargin) / 32
Now I have the column width needed in points so I transform that to excel units by a factor:
factor = Columns("A:A").Width / Columns("A:A").ColumnWidth
cWid = cWid / factor
And apply the found width to the columns of interest.
Columns("A:HP").Select
Selection.ColumnWidth = cWid
What I get is a cWid of 2.0601 (excel units) for my setup instead of the needed 2.14. Same procedure is used for the rows (subtracting the header, footer, top and bottom margins from the page height of 11.692 inches) yielding a cHei of 1.841664 instead of 2.14 which is also needed here (50 rows on one page)
Margins are set to normal in the macro before any of the editing of the column and row width and height.
Any clue what I am doing wrong? Or if there is a better way to reach the same goal?
Regards Emil
The printing to pdf works well for me, but the fitting of the columns and rows not so much
I have realised that the following section adjusts the column width and that the input needed is in excel units:
Columns("A:HP").Select
Selection.ColumnWidth = x.xx
In my current setup the column width needs to be 2.14 (excel units). So far so good, now to determine what the width should be on any computer.
I get the page width in points:
pgWid = Application.InchesToPoints(8.267) '(8.267 beeing the width of A4 paper)
Subtract the margins and divide by 32 (beeing the number columns that I would like to fit on one page):
LeftMargin = ActiveSheet.PageSetup.LeftMargin
RightMargin = ActiveSheet.PageSetup.RightMargin
cWid = (pgWid - LeftMargin - RightMargin) / 32
Now I have the column width needed in points so I transform that to excel units by a factor:
factor = Columns("A:A").Width / Columns("A:A").ColumnWidth
cWid = cWid / factor
And apply the found width to the columns of interest.
Columns("A:HP").Select
Selection.ColumnWidth = cWid
What I get is a cWid of 2.0601 (excel units) for my setup instead of the needed 2.14. Same procedure is used for the rows (subtracting the header, footer, top and bottom margins from the page height of 11.692 inches) yielding a cHei of 1.841664 instead of 2.14 which is also needed here (50 rows on one page)
Margins are set to normal in the macro before any of the editing of the column and row width and height.
Any clue what I am doing wrong? Or if there is a better way to reach the same goal?
Regards Emil