View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.worksheet.functions
Dave Peterson Dave Peterson is offline
external usenet poster
 
Posts: 35,218
Default userform date issue

Something to try:

Option Explicit
Private Sub CommandButton1_Click()
With ActiveSheet.Range("A1")
.NumberFormat = "mmmm dd, yyyy"
.Value = CDate(Me.TextBox1.Value)
End With
End Sub
Private Sub UserForm_Initialize()
Me.TextBox1.Value = "11/12/2008"
End Sub

VBA's help (in xl2003) says this about the cDate() function:

CDate recognizes date formats according to the locale setting of your system.
The correct order of day, month, and year may not be determined if it is
provided in a format other than one of the recognized date settings. In
addition, a long date format is not recognized if it also contains the
day-of-the-week string.

Personally, I'd consider using something different than a textbox.

Either a calendar control...

Ron de Bruin has some notes:
http://www.rondebruin.nl/calendar.htm

Or even 3 textboxes/comboboxes/spinners/scrollbars.
One for the month, one for the day and one for the year.

Then you wouldn't have to worry about the user's intent when they typed:
01/02/03

(is it Jan 2, 2003 or 2001-Feb-03 or what.)



Woodi2 wrote:

Hi, I have a userform that can display a date in textbox 11 in a cell (not
same cell each time) and I can write back to that cell using textbox11.
If I enter i.e. 6/1/2009, the data is transferred from textbox11 to the
selected cell. The cell then changes the date to read 1/6/2009. I I
activate the userform again, textbox11 reads the date correctly.
I have tried using the following code
TextBox11.Text = Format(TextBox11.Value, "dd/mmm/yyyy") but the when I
activate the userform again, the cell reverts the date again.
Any ideas


--

Dave Peterson