I have an excel VBA print function that is called once for each sheet I want to print in my workbook
I cycle through the sheets in VBA and call this function.
Sub PrintSheet
With ActiveSheet
'lastRow is worked out here....
'Autofit YTD and SLY
.Columns("J:J").AutoFit
.Columns("K:K").AutoFit
'Autofit email column
.Columns("F:F").AutoFit
With .PageSetup
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = False
.Orientation = xlLandscape
.LeftMargin = Application.InchesToPoints(0.4)
.RightMargin = Application.InchesToPoints(1)
.TopMargin = Application.InchesToPoints(0.5)
.BottomMargin = Application.InchesToPoints(0.5)
.RightFooter = "&P of &N"
.PrintArea = "$A$1:$O$" & CStr(lastRow)
End With
.PrintOut
End With
End Sub
The issue I am having is that sometimes (random behaviour and not always the same sheet) the columns that I am autofitting stretch really wide forcing other columns off the page. This is not data related as you can run the print routine again and it prints the same sheet which was previously stretching columns out fine. I am trimming all column values in the sheet as they get inputted with VBA code in the first place
I have not been able to find any pattern with this behaviour apart from as I said the columns that stretch are the ones that are getting autofitted.
Any thoughts?
I have experienced this many times when using
autofit; so I try to use it as little as possible now.Things you can do:
1.Use a set width e.g if you know that column J will always be ok at 47 width then use
Columns("D:D").ColumnWidth = 472. You could add some tests to the end of your sub-routine like so: