Solved

Insert a date automatically in a Worksheet.

Posted on 2011-02-21
14
788 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 6

Expert Comment

by:KnutsonBM
Comment Utility
why use vba when the formula accomplishes the same thing?  

-Brandon
0
 
LVL 3

Expert Comment

by:byronwall
Comment Utility
You can also do

CTRL + ;

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

Expert Comment

by:patrickab
Comment Utility
>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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Author Comment

by:pyramid
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
> 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
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now