add printed date to cell using beforeprint based on active worksheet

teamramrod737

New Member
Joined
Nov 6, 2020
Messages
2
Office Version
  1. 2019
Platform
  1. Windows
Hello,

I have a workbook that consists of two worksheets (Purchase Orders, COR Back-Up) that I would like to paste the date that the latest page was printed using beforeprint. I was successful in getting it to work for the first worksheet when I was only running it for just one worksheet but because beforeprint is a workbook vba code, I am not sure how to write the code to run depending on the worksheet I am working off of. I will paste the code I am trying to use below but it keeps giving me errors.

Private Sub Workbook_BeforePrint(Cancel As Boolean)
If ActiveSheet = "Purchase Orders" Then
Sheets("Purchase Orders").Range("B39") = Now()
Else
If ActiveSheet = "COR Back-up" Then
Sheets("COR Back-up").Range("D17") = Now()
End If
End If
End Sub

Sorry if this in buried in an old post but I couldnt find anything that was an exact match to my problem. Thanks in advance.
 
Try:
VBA Code:
Private Sub Workbook_BeforePrint(Cancel As Boolean)
    If ActiveSheet.Name = "Purchase Orders" Then
        Sheets("Purchase Orders").Range("B39") = Now()
    ElseIf ActiveSheet.Name = "COR Back-up" Then
        Sheets("COR Back-up").Range("D17") = Now()
    End If
End Sub
 
Upvote 0
mumps -- thank you very much for the help. This works now. It looks like I was close but just had a few things wrong.
 
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top