Matt hayward
Matt hayward

Reputation: 11

How to alter the time in vb to display a different date

I'm currently using this,

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Sheets("Sheet1").Range("K1").Value = Format(Now(), "mm-dd-yyyy")
End Sub

in order to print the last save to my spreadsheet. What I would like to do however is have it print the date from 12 hours ago. Would this require a simple change to the code or something else entirely?

Upvotes: 1

Views: 57

Answers (2)

Mitch
Mitch

Reputation: 544

I would use the DateAdd function:

Sheets("Sheet1").Range("K1").Value = DateAdd("h", -12, Now())

Upvotes: 1

L42
L42

Reputation: 19727

Just like what pnuts commented, you should subtract the hours from Now.
Try:

Sheets("Sheet1").Range("K1").Value = Format(Now - TimeValue("12:00"), "mm-dd-yyyy")

Upvotes: 0

Related Questions