Date Function needed

S

smonczka

I'm running into a problem with putting a date in a form i'm making.
I want to have todays date populate a cell when the form is opened.
But only once. So if a person opens the form for the first time it
populates todays date... today(). But if they open the form up again
that date will not change. If you use =today() then each time you
open the form up it populates that cell with today’s date.

Any idea's how i can code a cell with today’s date upon creation but
not have it change each time the form is opened.

Thanks for the help,
Steve
 
G

Gary''s Student

Hi Steve:

This little Workbook event macro runs whenever the workbook is opened.

If Sheet1 cell A1 is empty, the date is inserted.
If Sheet1 cell A1 is not empty, no insertion is made.

So if the workbook is opened, saved, and re-opened, the first date is not
over-written:

Private Sub Workbook_Open()
With Sheets("Sheet1").Range("A1")
If .Value = "" Then
.Value = Date
End If
End With
End Sub

Because it is workbook code, it is very easy to install and use:

1. right-click the tiny Excel icon just to the left of File on the Menu Bar
2. select View Code - this brings up a VBE window
3. paste the stuff in and close the VBE window

If you save the workbook, the macro will be saved with it.

To remove the macro:

1. bring up the VBE windows as above
2. clear the code out
3. close the VBE window

To learn more about macros in general, see:

http://www.mvps.org/dmcritchie/excel/getstarted.htm

To learn more about Event Macros (worksheet code), see:

http://www.mvps.org/dmcritchie/excel/event.htm
 

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

Top