I have a workbook where I allow the user to print certain sheets. (There are potentially 30 to choose from, and they can print as few or as many as they want.) They have the option of having the date & time appear on each sheet, in the right footer. The code places this info in the right footer just before printing (technically before a print preview). However, I've discovered it is not displaying the correct time on the sheets except for the first sheet.
Let's say I printed some sheets at 5:58 PM today. Then I went to print those same sheets again at 6:05 PM.
The right footer on the first page would say "Printed on 9/27/2019 @ 6:05 PM", just as it should. However, on all the rest of the pages, the right footer would say "Printed on 9/27/2019 @ 5:58 PM". That makes no sense, because my code is supposed to be setting the Right Footer on each page after the user has pressed the print button (a custom button I've made).
Here's the code I'm using to do this:
Here is the entire sub of SetPrintArea:
Can anyone see anything that would be causing this issue? I have looked at the code for a couple of hours now, but I'm not seeing anything wrong. The worst part is, if I step through the code, then the problem DOESN'T OCCUR. That's what makes this one so confusing. I can't see where things are going wrong when I'm debugging because it won't do it when stepping through the code. Has anyone else experienced that before?
I sincerely appreciate any help on this one. It's driving me bonkers.
Let's say I printed some sheets at 5:58 PM today. Then I went to print those same sheets again at 6:05 PM.
The right footer on the first page would say "Printed on 9/27/2019 @ 6:05 PM", just as it should. However, on all the rest of the pages, the right footer would say "Printed on 9/27/2019 @ 5:58 PM". That makes no sense, because my code is supposed to be setting the Right Footer on each page after the user has pressed the print button (a custom button I've made).
Here's the code I'm using to do this:
Code:
FirstSheetFound = False
SheetsToPrint = ""
SheetName = ""
For i = 1 to 30
SheetName = "Sheet" & i
If SheetWasChosenToPrint(i) = True Then
If FirstSheetFound = True Then
SheetsToPrint = SheetsToPrint & "," & SheetName
Else
SheetsToPrint = SheetsToPrint & SheetName
FirstSheetFound = True
End If
'this places the date & time on the Right Footer of the page, if the user has chosen for it to be there
ThisWorkbook.Sheets(SheetName).PageSetup.RightFooter = ""
If IncludeDateTimeFooter = True Then _
ThisWorkbook.Sheets(SheetName).PageSetup.RightFooter = _
"Printed on " & Format(Now(), DateFormatToUse) & " @ " _
& Format(Now(), "h:mm AM/PM")
Debug.Print ThisWorkbook.Sheets(SheetName).PageSetup.RightFooter
'this next sub sets the print area and sets which rows to repeat at the top of each page
Call SetPrintArea(SheetName)
'in case the sheet is not set to visible, this will do that
ThisWorkbook.Sheets(SheetName).Visible = xlSheetVisible
End If
Next i
Application.ScreenUpdating = True
ThisWorkbook.Sheets(Split(SheetsToPrint, ",")).PrintOut Preview:=True, IgnorePrintAreas:=False
Application.ScreenUpdating = False
Here is the entire sub of SetPrintArea:
Code:
Sub SetPrintArea(SheetName As String)
Dim LastColumn As Integer
Dim LastColumnLetter As String
Dim NumberOfColumnsDisplayed As Integer
Dim PrintAreaRange As String
NumberOfColumnsDisplayed = ThisWorkbook.Sheets(SheetName).Range("AS8").Value
LastColumn = 8 + NumberOfColumnsDisplayed
LastColumnLetter = Chr(LastColumn + 64)
On Error Resume Next
Application.PrintCommunication = False
On Error GoTo 0
PrintAreaRange = "C4:" & LastColumnLetter & "213"
With ThisWorkbook.Sheets(SheetName).PageSetup
.PrintArea = Range(PrintAreaRange).Address
.Orientation = xlLandscape
'.Zoom = False <-this was causing another issue, so I am commenting it out for now
.FitToPagesWide = 1
.FitToPagesTall = False
.PrintTitleRows = "$4:$9"
End With
End Sub
Can anyone see anything that would be causing this issue? I have looked at the code for a couple of hours now, but I'm not seeing anything wrong. The worst part is, if I step through the code, then the problem DOESN'T OCCUR. That's what makes this one so confusing. I can't see where things are going wrong when I'm debugging because it won't do it when stepping through the code. Has anyone else experienced that before?
I sincerely appreciate any help on this one. It's driving me bonkers.