Reputation: 1051
I have a dialog box that appears when the user clicks a macro button. This dialog box is mostly filled out (date, email, producer, website, etc are filled) and all the user needs to do is enter their name. The problem is that the date entered is static, so if I entered "3/3/11" it'll stay that way until someone changes it.
I was wondering if there was some way for that text box to always display the current date (unless the user decides to change it for whatever reason). I've tried putting different things into the "Value" section of the text box (such as "getDate()" and "= Date()") but so far haven't been successful.
Thank you,
Jesse Smothermon
Upvotes: 5
Views: 119734
Reputation: 61
I know this is extremely old post, but I've used this before
You can always adjust to activate. Now method is another way. Just an option
Private Sub UserForm_Initialize()
Textbox1.Text = Format(Now(), "mmddyyyhhmmss")
End Sub
Upvotes: 0
Reputation: 9
Actually, it is less complicated than it seems.
Sub
today_1()
ActiveCell.FormulaR1C1 = "=TODAY()"
ActiveCell.Value = Date
End Sub
Upvotes: 0
Reputation: 1747
You were close. Add this code in the UserForm_Initialize()
event handler:
tbxDate.Value = Date
Upvotes: 0
Reputation: 1
Here's a more simple version. In the cell you want the date to show up just type
=Today()
Format the cell to the date format you want and Bob's your uncle. :)
Upvotes: 0
Reputation: 166306
Set the value from code on showing the form, not in the design-timeProperties for the text box.
Private Sub UserForm_Activate()
Me.txtDate.Value = Format(Date, "mm/dd/yy")
End Sub
Upvotes: 1
Reputation: 15214
Use the form Initialize event, e.g.:
Private Sub UserForm_Initialize()
TextBox1.Value = Format(Date, "mm/dd/yyyy")
End Sub
Upvotes: 13
Reputation: 22842
The easy way to do this is to put the Date function you want to use in a Cell, and link to that cell from the textbox with the LinkedCell property.
From VBA you might try using:
textbox.Value = Format(Date(),"mm/dd/yy")
Upvotes: 1