Solved

Insert a date automatically in a Worksheet.

Posted on 2011-02-21
14
793 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

628 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