PDA

View Full Version : Solved: Userform TextBoxes / Inserting Dates



Rob342
11-17-2011, 07:15 AM
Hi guys

I get totally confused with dates in vba come somebody pls advise

I have a userform with 3 textboxes that i'm using for dates what i require is:
1 when the user clicks on the box and either enters a date, it formats it to **/**/**** or can a dropdown with a calender be created?
2 how to validate the date entered.

if anybody has got some examples it would help

Rob

monarchd
11-17-2011, 07:59 AM
Do you want it to initialize with a date? Here's what I use to prefill a UserForm text box with the previous date with / :


Private Sub UserForm_Initialize()
TextBox1.Value = DateTime.Month(Date) & "/" & DateTime.Day(Date - 1) & "/" & DateTime.Year(Date)
End Sub

If you want current date you could just take out the -1 under Day

monarchd
11-17-2011, 08:01 AM
Oh, and then to use that value, I use this on a button on the same UserForm:



Private Sub CommandButton1_Click()
Dim myFormula As String
Dim wks As Worksheet
Dim LastRow As Long
Dim Rng1 As Range
Set wks = ActiveSheet
'you could put this in any cell
wks.Range("G2").Value = Me.TextBox1.Value
'rest of your code here
End Sub

Rob342
11-17-2011, 11:01 AM
monarchd

Thanks for reply
The textbox will be blank when initialized, the user will have to put a date in the textbox, but in the correct format.

Rob

monarchd
11-17-2011, 11:27 AM
Ah, thanks for clarifying. Check out this post at OZgrid which I think is what you are looking for:

http://www.ozgrid.com/forum/showthread.php?t=27977

Rob342
11-17-2011, 12:44 PM
Thanks for the link i will chk it out tomorrow & let you know

Rob

Rob342
11-20-2011, 03:04 AM
Going to use another method re calender on K Base

Rob