Static Date Formula or VBA

Grammarjunkie

Board Regular
Joined
Mar 22, 2016
Messages
86
Hey guys,

I'd really like a formula (I'm thinking this will require VBA-thing though) that would populate an unchanging date.

For instance, I want F2 to populate with =NOW(), when I manually fill G2 with blah blah, but I want the NOW() to not change when I open the file the next day.

So essentially column F will automatically fill with the time and date (that doesn't change) when the corresponding row in column G is filled.
(If it matters, G fields would be manually filled with a percentage.)

Thanks for any help!
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Try this. This needs to be copied to the VBAproject sheet that you want it to work with.

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'
    If (ActiveCell.Column = 7) Then
        If (Cells(ActiveCell.Row, ActiveCell.Column - 2) = "") Then
            FixedDate = "'" & Month(Now()) & "/" & Day(Now()) & "/" & Year(Now())
            If (Hour(Now()) > 12) Then
                FixedDate = FixedDate & " " & Hour(Now()) - 12 & ":" & Minute(Now()) & " PM"
            Else
                FixedDate = FixedDate & " " & Hour(Now()) & ":" & Minute(Now()) & " AM"
            End If
            Cells(ActiveCell.Row, ActiveCell.Column - 2) = FixedDate
        End If
    End If
End Sub
 
Upvote 0
Not sure why I can't edit my mistake above but this code is corrected
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'
    If (ActiveCell.Column = 7) Then
        If (Cells(ActiveCell.Row, ActiveCell.Column - 2) = "") Then
            FixedDate = "'" & Month(Now()) & "/" & Day(Now()) & "/" & Year(Now())
            If (Hour(Now()) >= 12) Then
                FixedDate = FixedDate & " " & Hour(Now()) - 12 & ":" & Minute(Now()) & " PM"
            Else
                FixedDate = FixedDate & " " & Hour(Now()) & ":" & Minute(Now()) & " AM"
            End If
            Cells(ActiveCell.Row, ActiveCell.Column - 2) = FixedDate
        End If
    End If
End Sub
 
Last edited:
Upvote 0
It's both easier and harder than that.

The supplied code from WhoCanDo doesn't provide leading zeros for any of the month, day, hour or minute figures as these functions don't return with leading zeroes, and the year function returns as yyyy. You can use the function Format(Now, "MM/dd/yy Hh:Nn AM/PM") which WILL return the date in the correct format, however, it may not be displayed as such depending on system settings.

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If (ActiveCell.Column = 7) Then
        If (Cells(ActiveCell.Row, ActiveCell.Column - 2) = "") Then
            Cells(ActiveCell.Row, ActiveCell.Column - 2) = Format(Now, "MM/dd/yy Hh:Nn AM/PM")
        End If
    End If
End Sub
 
Last edited:
Upvote 0
Try this.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    If Target.Column = 7 And Target.Row > 1 Then
        If Target.Value <> "" Then
            Application.EnableEvents = False
            With Target.Offset(, -1)
                .Value = Now
                .NumberFormat = "dd/mm/yy hh:mm AM/PM"    ' change as required
            End With
        End If
    End If
    
    Application.EnableEvents = True
    
End Sub
 
Upvote 0
@oriwitt has the right idea but that is entering date-like text into an excel cell which will auto-format it as it sees fit.
It would be better to format the Range that the text is to be entered into. e.g. in the Sheet Code Module enter

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 7 Then
    With Target.Offset(, -1)
        If .Value = "" Then
            .NumberFormat = "dd/mm/yy h:mm AM/PM"
            .Value = Now
        End If
    End With
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,100
Messages
6,128,824
Members
449,470
Latest member
Subhash Chand

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