Environment
- Windows Specification: Windows 10 Pro
- Version: 20H2
Background
I decided to create a text box in a UserForm using VBA to enter a date. Initially, it was blank, but there was a request to “insert today’s date by default,” so I learned how to do it.
Select “Initialize” from the Event List and Use “Date”
Here is the procedure. After creating the UserForm, you will see two long dropdown bars at the top of the VBA code window. Click the dropdown on the right side of those bars and select the item “Initialize“. This will create Private Sub UserForm_Initialize(). Then, write the following program.
Private Sub UserForm_Initialize()
Me.TextBox_date.Value = Date
End Sub
Code Explanation
- Line 2: Enter the name of the text box after
Me.. In my case, I named it “TextBox_date”, so that is what I wrote.
This worked successfully.
Lessons Learned
At first, I did not use Initialize, but instead typed Me..... = Date directly into the main processing code of the UserForm, which caused confusion and did not work as intended.
