Dr. Demento
Member
I've got some code that to my inexperienced mind should run quickly, but it bogs the machine down for 1-2 minutes and I'm hoping for some pointers on how it can be adapted to run more efficiently. I've usually got less than five worksheets in a file and cells used < 1000, so it shouldn't be that challenging (but hey, what do I know?) .
I'm using Excel 2010.
Thanks so much.
I'm using Excel 2010.
Thanks so much.
Code:
Option Explicit
Sub HeaderFooter()
'
' Force_Header_Footer Macro
' Forces custom Header & Footer to all Excel workbooks/sheets
'
Dim SheetNumber As Integer
Dim NumberOfSheets As Integer
NumberOfSheets = Worksheets.Count
For SheetNumber = 1 To NumberOfSheets
With Worksheets(SheetNumber).PageSetup
.LeftMargin = Application.InchesToPoints(0.5)
.RightMargin = Application.InchesToPoints(0.5)
.TopMargin = Application.InchesToPoints(0.75)
.BottomMargin = Application.InchesToPoints(0.75)
.HeaderMargin = Application.InchesToPoints(0.25)
.FooterMargin = Application.InchesToPoints(0.25)
.LeftHeader = ""
.CenterHeader = "File: &F" & Chr(10) & "Sheet: &A"
.RightHeader = ""
.LeftFooter = ""
.CenterFooter = "Page &P of &N"
.RightFooter = "&D || &T"
.ScaleWithDocHeaderFooter = False
.DifferentFirstPageHeaderFooter = False
.PrintQuality = 600
.CenterHorizontally = True
.CenterVertically = False
If ActiveSheet.UsedRange.Width / ActiveSheet.UsedRange.Height > 0.85 Then '~~~> ActiveSheet will determine Orientation | http://stackoverflow.com/questions/24733215/excel-vba-how-to-detect-that-the-printed-width-is-greater-than-the-printed-height
.Orientation = xlLandscape
End If
.Draft = False
.PaperSize = xlPaperLetter
.FirstPageNumber = xlAutomatic
.Order = xlDownThenOver
.BlackAndWhite = False
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = False
End With
Next SheetNumber
End Sub