Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Insert a date automatically in a Worksheet.

Posted on 2011-02-21
14
Medium Priority
?
795 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 400 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

926 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