How to date stamp last Sunday's date

301 Views Asked by At

I'm looking to have a couple of dates stamped onto the spreadsheet on opening.

My current script is as follows:

Private Sub Workbook_Open()

'Automate start time on opening
    With Worksheets("Time Recording Estimate")
        If IsEmpty(.Range("B4")) Then .Range("B4").Value = "Start"
    End With
End Sub

I now wish to also add a stamp for the last Sunday, hoping it can look something like the following:

Private Sub Workbook_Open()

'Automate start time on opening
    With Worksheets("Time Recording Estimate")
        If IsEmpty(.Range("B4")) Then 
            .Range("B4").Value = "Start"
            .Range("V3").Value = Now() - Weekday(Now()) + 1
    End With
End Sub

I'm not sure which part of the formula is wrong and is failing to go through.

Much appreciated for any assistance offered.

1

There are 1 best solutions below

0
ashleedawg On BEST ANSWER

Looks like your code is missing an End If:

Private Sub Workbook_Open()

'Automate start time on opening
    With Worksheets("Time Recording Estimate")
        If IsEmpty(.Range("B4")) Then
            .Range("B4").Value = "Start"
            .Range("V3").Value = Now() - Weekday(Now()) + 1
        End If
    End With
End Sub

Once I added that (and changed the worksheet name to one that I actually have), it ran properly: if cell B4 is empty then then it puts "Start" in cell B4 and Sunday's date (today in this case, since it's Sunday) in cell V3.

I'd suggest that you [always] add this line to the top of any module:

Option Explicit

This will "force" proper declaration & usage of variables, objects, properties, etc, and will save you a lot of headaches in the future.

Also, you should compile the code, so you can see where problem lie. Here is a quick overview of the process.