Solved

Insert a date automatically in a Worksheet.

Posted on 2011-02-21
14
791 Views
Last Modified: 2012-05-11
I am very new at this. This function works when I run it through VBA. I want to have the date inserting in the cell below when I open the workbook. I am assuming its the Openworkbook function. But I have no clue as to how to set this up. I know I use a Macro to call it. But I prefer to automate the function. An help is appreciated.

Function DateTime()
DateTime = Now
Range("F1").Value = Now
End Function
0
Comment
Question by:pyramid
  • 4
  • 4
  • 3
  • +2
14 Comments
 
LVL 50

Accepted Solution

by:
Dave Brett earned 100 total points
ID: 34947416
In the ThisWorkbook module of the VBE place this code

works on F1 of first sheet

hth

Dave
Private Sub Workbook_Open()
Sheets(1).Range("F1").Value = Now
End Sub

Open in new window

0
 
LVL 6

Expert Comment

by:KnutsonBM
ID: 34947429
an easy formula is =today(), you can put that in whatever cell and today's date will show up whenever the workbook is open.

-brandon
0
 
LVL 45

Expert Comment

by:patrickab
ID: 34947479
pyramid,

If you only want the date and not the date and time you could use this in the ThisWorkbook module.

Private Sub Workbook_Open()
Sheets(1).[F1] = Date
End Sub

Patrick
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 6

Expert Comment

by:KnutsonBM
ID: 34947969
why use vba when the formula accomplishes the same thing?  

-Brandon
0
 
LVL 3

Expert Comment

by:byronwall
ID: 34948051
You can also do

CTRL + ;

to insert the current date into the active cell.
0
 
LVL 45

Expert Comment

by:patrickab
ID: 34950052
>why use vba when the formula accomplishes the same thing?  

Because the OP specified 'automatically' in the question.
0
 
LVL 6

Expert Comment

by:KnutsonBM
ID: 34950885
putting the formula in there would be just as automatic as typing out your code. Any time you opened up the Workbook it would show today's date without any refreshing.....

just my opinion

-Brandon
0
 
LVL 1

Author Comment

by:pyramid
ID: 34952226
Hi Brettdj

I inserted the code in the My Workbook module and have attached of file image of what I did. When I saved the file and opened the workbook, the sheet did not have todays date in it. Anymore imput would be appreciated.

thank
Pyramid
Code-Image.doc
0
 
LVL 6

Expert Comment

by:KnutsonBM
ID: 34952416
have you tried putting the formula in the cell?  it is just as 'automatic' as vb code, there is no modification needed, you can put it wherever you want..........
0
 
LVL 1

Author Comment

by:pyramid
ID: 34956350
Hi KnutsonBM

I understand this but I just don't want a solution, I am looking to learn more about VBA in the Excel environment.

thanks

P.
0
 
LVL 45

Expert Comment

by:patrickab
ID: 34956484
pyramid,

>I understand this but I just don't want a solution, I am looking to learn more about VBA in the Excel environment.

If you only want the date and not the date and time you could use this in the ThisWorkbook module.

Private Sub Workbook_Open()
Sheets(1).[F1] = Date
End Sub

Patrick
0
 
LVL 50

Expert Comment

by:Dave Brett
ID: 34956693
> I inserted the code in the My Workbook module and have attached of file image of what I di

That looks right

1) Are you looking in F1 of the left most worksheet
2) Do you have a macro warning message when you open the file?

You can test the code by using F8 from the ThisWorkbook module, ie not just the open file test

Cheers
Dave
0
 
LVL 1

Author Comment

by:pyramid
ID: 34961179
Hi Dave

It will not run using F8 and when I try to run it using the run sub/userform  command button it demands that I create a macro. I eliminated all other sheets as well so that I only have Sheet 1.

thanks

P.
0
 
LVL 1

Author Comment

by:pyramid
ID: 34970869
BTW, I am running in Excel 2003. I put the code in 2007 and it works. The differences are these.
Excel 2003 environment on a IBM ThinkPad which I do not have admin rights
Excel 2007 environment on a Dell Lattitude 6400 which I have full admin rights
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvie…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question