Automatic Dates

  • Thread starter Thread starter Orf Bartrop
  • Start date Start date
O

Orf Bartrop

Is there a way of automatically entering today's date so that it does
not change when the program is opened on a later date. I tried the
TODAY() command but that updates the date to the current date.

Orf Bartrop
 
Hi Orf,

VBA will do it

Private Sub Worksheet_Change(ByVal Target As Range)

On Error GoTo ws_exit:
Application.EnableEvents = False
If Not Intersect(Target, Me.Range("A1:H10")) Is Nothing Then
With Target
.Value = Format(Date, "dd mmm yyyy")
End With
End If

ws_exit:
Application.EnableEvents = True
End Sub

This is worksheet event code, so right-click the sheet tab, select View
Code, and paste the code in.

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 
Thanks, Bob. I did as you directed and came up with an error:
Compile error: Ambiguous name detected: Worksheet-Change

I think this is happening because there is already a statement:
Private Sub Worksheet_Change(ByVal Target As Range
followed by End Sub (nothing between).

I tried deleting those two statements but then data entered in the sheet
was not correct.

I do not know what wording to change to get it to work.

Orf
 
Orf,

Your reasoning is correct.

Post the 2 change event codes and I will try and merge them for you.

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 
Bob, the codes that were present before I added your code are:


Private Sub Worksheet_Change(ByVal Target As Range)

End Sub


Private Sub Worksheet_SelectionChange(ByVal Target As Range)

End Sub


I then added your code:

Private Sub Worksheet_Change(ByVal Target As Range)

On Error GoTo ws_exit:
Application.EnableEvents = False
If Not Intersect(Target, Me.Range("A1:H10")) Is Nothing Then
With Target
.Value = Format(Date, "dd mmm yyyy")
End With
End If

ws_exit:
Application.EnableEvents = True
End Sub



and that is when an error message came up.

Orf
 
Hey Orf,

That is so easy.

The other two do nothing, so just get rid of them.

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 
Thanks Bob, as you can see I know little about coding. I have done as
you suggested and so far so good. I will wait until tomorrow to see if
it works and retains the given date.

Orf
 
It will be okay - have faith.

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 
Whoops. That change caused the date to come up in every cell in all
columns and rows.

Orf
 
Nonsense<vbg>

Just tried it, it works fine.

Send me your workbook.

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Excel Message box with days of month & date. 1
Undo button 2
Excel Vba to change displayed year automatically. 14
today's date static 1
Excel Excel Show Countdown Date 7
Repeat Entries 3
Macro for Dates 5
Conditional Validation In Excel 1

Back
Top