PC Review


Reply
Thread Tools Rate Thread

Date formatting from user form to worksheet

 
 
Steve Jones
Guest
Posts: n/a
 
      5th Aug 2010
I am having a problem transferring a date from a userform to a worksheet in
Excel 2003

This is an extract of the code I am using to enter data from the userform to
the worksheet.

Dim irow As Long
Dim ws As Worksheet
Set ws = Worksheets("Wagedata")

' finds first empty row in database

irow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row

ws.Cells(irow, 1).Value = Me.Cmbempno.Value
ws.Cells(irow, 2).Value = Me.Txtempname.Value
ws.Cells(irow, 3).Value = Me.TxtFrmcd.Value
ws.Cells(irow, 4).Value = Me.TxtDate.Value ' date not being entered in the
format required


All works as it should except I want the date displayed in DD/MM/YY format
on the worksheet rather than MM/DD/YY.


Thanks



 
Reply With Quote
 
 
 
 
Dave O
Guest
Posts: n/a
 
      5th Aug 2010
I didn't try to compile this solution, but it should be a matter of
adding a row similar to this one, just before the code assigns the
value to that cell:

ws.Cells(irow, 4).numberformat = "dd/mm/yyyy"

Dave O
Eschew obfuscation
 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      5th Aug 2010
The problem is in the way the user enters the date in the userform textbox and
the date order that the user specifies in his/her windows date settings. (I'm
guessing that the problem occurs when they use the short date format.)

If you use cdate() to convert the textbox string to a real date, excel/vba will
use that user's setting (see help for cdate for more info).

This is from xl2003 VBA's help for cDate:

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.

=====
But if they type something that isn't a date, you could still have trouble.

Maybe something like this will help:

Option Explicit
Sub testme()

Dim myStr As String

'a date or a warning message
Dim myDate As Variant

myStr = "01/02/03"

On Error Resume Next
myDate = CDate(myStr)
If Err.Number <> 0 Then
Err.Clear
myDate = "Not a date"
End If
On Error GoTo 0

With ActiveSheet.Range("A1")
'unambiguous format for testing!
.NumberFormat = "mmmm dd, yyyy"
.Value = myDate
End With

End Sub

Instead of using a textbox to get the date, you may want to remove any potential
problems by using a calendar control.

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

Or even 3 different controls (year, month, day) to get the date.

On 08/05/2010 09:58, Steve Jones wrote:
> I am having a problem transferring a date from a userform to a worksheet in
> Excel 2003
>
> This is an extract of the code I am using to enter data from the userform to
> the worksheet.
>
> Dim irow As Long
> Dim ws As Worksheet
> Set ws = Worksheets("Wagedata")
>
> ' finds first empty row in database
>
> irow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
>
> ws.Cells(irow, 1).Value = Me.Cmbempno.Value
> ws.Cells(irow, 2).Value = Me.Txtempname.Value
> ws.Cells(irow, 3).Value = Me.TxtFrmcd.Value
> ws.Cells(irow, 4).Value = Me.TxtDate.Value ' date not being entered in the
> format required
>
>
> All works as it should except I want the date displayed in DD/MM/YY format
> on the worksheet rather than MM/DD/YY.
>
>
> Thanks
>
>
>


--
Dave Peterson
 
Reply With Quote
 
Lynz
Guest
Posts: n/a
 
      9th Aug 2010
On 6/08/2010 9:31 a.m., Dave Peterson wrote:
> The problem is in the way the user enters the date in the userform
> textbox and the date order that the user specifies in his/her windows
> date settings. (I'm guessing that the problem occurs when they use the
> short date format.)
>
> If you use cdate() to convert the textbox string to a real date,
> excel/vba will use that user's setting (see help for cdate for more info).
>
> This is from xl2003 VBA's help for cDate:
>
> 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.
>
> =====
> But if they type something that isn't a date, you could still have trouble.
>
> Maybe something like this will help:
>
> Option Explicit
> Sub testme()
>
> Dim myStr As String
>
> 'a date or a warning message
> Dim myDate As Variant
>
> myStr = "01/02/03"
>
> On Error Resume Next
> myDate = CDate(myStr)
> If Err.Number <> 0 Then
> Err.Clear
> myDate = "Not a date"
> End If
> On Error GoTo 0
>
> With ActiveSheet.Range("A1")
> 'unambiguous format for testing!
> .NumberFormat = "mmmm dd, yyyy"
> .Value = myDate
> End With
>
> End Sub
>
> Instead of using a textbox to get the date, you may want to remove any
> potential problems by using a calendar control.
>
> Ron de Bruin has some notes:
> http://www.rondebruin.nl/calendar.htm
>
> Or even 3 different controls (year, month, day) to get the date.
>
> On 08/05/2010 09:58, Steve Jones wrote:
>> I am having a problem transferring a date from a userform to a
>> worksheet in
>> Excel 2003
>>
>> This is an extract of the code I am using to enter data from the
>> userform to
>> the worksheet.
>>
>> Dim irow As Long
>> Dim ws As Worksheet
>> Set ws = Worksheets("Wagedata")
>>
>> ' finds first empty row in database
>>
>> irow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
>>
>> ws.Cells(irow, 1).Value = Me.Cmbempno.Value
>> ws.Cells(irow, 2).Value = Me.Txtempname.Value
>> ws.Cells(irow, 3).Value = Me.TxtFrmcd.Value
>> ws.Cells(irow, 4).Value = Me.TxtDate.Value ' date not being entered in
>> the
>> format required
>>
>>
>> All works as it should except I want the date displayed in DD/MM/YY
>> format
>> on the worksheet rather than MM/DD/YY.
>>
>>
>> Thanks
>>
>>
>>


Rons calendar is good, or
What about Range.value = Format("dd/mm/yy") in your code or smethiing
similar?
Lyn
 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      9th Aug 2010
Nope.

This line of code:
Range.value = Format("dd/mm/yy")

is essentially typing that string into the cell. And as soon as you do that,
then excel looks at the user's short date setting. So if the user likes mdy,
you won't get the correct date.




On 08/08/2010 18:31, Lynz wrote:
> On 6/08/2010 9:31 a.m., Dave Peterson wrote:
>> The problem is in the way the user enters the date in the userform
>> textbox and the date order that the user specifies in his/her windows
>> date settings. (I'm guessing that the problem occurs when they use the
>> short date format.)
>>
>> If you use cdate() to convert the textbox string to a real date,
>> excel/vba will use that user's setting (see help for cdate for more info).
>>
>> This is from xl2003 VBA's help for cDate:
>>
>> 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.
>>
>> =====
>> But if they type something that isn't a date, you could still have trouble.
>>
>> Maybe something like this will help:
>>
>> Option Explicit
>> Sub testme()
>>
>> Dim myStr As String
>>
>> 'a date or a warning message
>> Dim myDate As Variant
>>
>> myStr = "01/02/03"
>>
>> On Error Resume Next
>> myDate = CDate(myStr)
>> If Err.Number<> 0 Then
>> Err.Clear
>> myDate = "Not a date"
>> End If
>> On Error GoTo 0
>>
>> With ActiveSheet.Range("A1")
>> 'unambiguous format for testing!
>> .NumberFormat = "mmmm dd, yyyy"
>> .Value = myDate
>> End With
>>
>> End Sub
>>
>> Instead of using a textbox to get the date, you may want to remove any
>> potential problems by using a calendar control.
>>
>> Ron de Bruin has some notes:
>> http://www.rondebruin.nl/calendar.htm
>>
>> Or even 3 different controls (year, month, day) to get the date.
>>
>> On 08/05/2010 09:58, Steve Jones wrote:
>>> I am having a problem transferring a date from a userform to a
>>> worksheet in
>>> Excel 2003
>>>
>>> This is an extract of the code I am using to enter data from the
>>> userform to
>>> the worksheet.
>>>
>>> Dim irow As Long
>>> Dim ws As Worksheet
>>> Set ws = Worksheets("Wagedata")
>>>
>>> ' finds first empty row in database
>>>
>>> irow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
>>>
>>> ws.Cells(irow, 1).Value = Me.Cmbempno.Value
>>> ws.Cells(irow, 2).Value = Me.Txtempname.Value
>>> ws.Cells(irow, 3).Value = Me.TxtFrmcd.Value
>>> ws.Cells(irow, 4).Value = Me.TxtDate.Value ' date not being entered in
>>> the
>>> format required
>>>
>>>
>>> All works as it should except I want the date displayed in DD/MM/YY
>>> format
>>> on the worksheet rather than MM/DD/YY.
>>>
>>>
>>> Thanks
>>>
>>>
>>>

>
> Rons calendar is good, or
> What about Range.value = Format("dd/mm/yy") in your code or smethiing
> similar?
> Lyn


--
Dave Peterson
 
Reply With Quote
 
Lynz
Guest
Posts: n/a
 
      9th Aug 2010
On 9/08/2010 12:21 p.m., Dave Peterson wrote:
> Nope.
>
> This line of code:
> Range.value = Format("dd/mm/yy")
>
> is essentially typing that string into the cell. And as soon as you do
> that, then excel looks at the user's short date setting. So if the user
> likes mdy, you won't get the correct date.
>

Right, I guess it only works for me cause that is what my windows
settings are. Thanks
Lynz

>
>
>
> On 08/08/2010 18:31, Lynz wrote:
>> On 6/08/2010 9:31 a.m., Dave Peterson wrote:
>>> The problem is in the way the user enters the date in the userform
>>> textbox and the date order that the user specifies in his/her windows
>>> date settings. (I'm guessing that the problem occurs when they use the
>>> short date format.)
>>>
>>> If you use cdate() to convert the textbox string to a real date,
>>> excel/vba will use that user's setting (see help for cdate for more
>>> info).
>>>
>>> This is from xl2003 VBA's help for cDate:
>>>
>>> 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.
>>>
>>> =====
>>> But if they type something that isn't a date, you could still have
>>> trouble.
>>>
>>> Maybe something like this will help:
>>>
>>> Option Explicit
>>> Sub testme()
>>>
>>> Dim myStr As String
>>>
>>> 'a date or a warning message
>>> Dim myDate As Variant
>>>
>>> myStr = "01/02/03"
>>>
>>> On Error Resume Next
>>> myDate = CDate(myStr)
>>> If Err.Number<> 0 Then
>>> Err.Clear
>>> myDate = "Not a date"
>>> End If
>>> On Error GoTo 0
>>>
>>> With ActiveSheet.Range("A1")
>>> 'unambiguous format for testing!
>>> .NumberFormat = "mmmm dd, yyyy"
>>> .Value = myDate
>>> End With
>>>
>>> End Sub
>>>
>>> Instead of using a textbox to get the date, you may want to remove any
>>> potential problems by using a calendar control.
>>>
>>> Ron de Bruin has some notes:
>>> http://www.rondebruin.nl/calendar.htm
>>>
>>> Or even 3 different controls (year, month, day) to get the date.
>>>
>>> On 08/05/2010 09:58, Steve Jones wrote:
>>>> I am having a problem transferring a date from a userform to a
>>>> worksheet in
>>>> Excel 2003
>>>>
>>>> This is an extract of the code I am using to enter data from the
>>>> userform to
>>>> the worksheet.
>>>>
>>>> Dim irow As Long
>>>> Dim ws As Worksheet
>>>> Set ws = Worksheets("Wagedata")
>>>>
>>>> ' finds first empty row in database
>>>>
>>>> irow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
>>>>
>>>> ws.Cells(irow, 1).Value = Me.Cmbempno.Value
>>>> ws.Cells(irow, 2).Value = Me.Txtempname.Value
>>>> ws.Cells(irow, 3).Value = Me.TxtFrmcd.Value
>>>> ws.Cells(irow, 4).Value = Me.TxtDate.Value ' date not being entered in
>>>> the
>>>> format required
>>>>
>>>>
>>>> All works as it should except I want the date displayed in DD/MM/YY
>>>> format
>>>> on the worksheet rather than MM/DD/YY.
>>>>
>>>>
>>>> Thanks
>>>>
>>>>
>>>>

>>
>> Rons calendar is good, or
>> What about Range.value = Format("dd/mm/yy") in your code or smethiing
>> similar?
>> Lyn

>


 
Reply With Quote
 
Steve Jones
Guest
Posts: n/a
 
      9th Aug 2010
Thanks all for your answers - I have managed to find a work around solution
which is not perfect but gets the desired result.

Thanks once again



"Lynz" <(E-Mail Removed)> wrote in message
news:i3nipr$1vn$(E-Mail Removed)...
> On 9/08/2010 12:21 p.m., Dave Peterson wrote:
>> Nope.
>>
>> This line of code:
>> Range.value = Format("dd/mm/yy")
>>
>> is essentially typing that string into the cell. And as soon as you do
>> that, then excel looks at the user's short date setting. So if the user
>> likes mdy, you won't get the correct date.
>>

> Right, I guess it only works for me cause that is what my windows
> settings are. Thanks
> Lynz
>
>>
>>
>>
>> On 08/08/2010 18:31, Lynz wrote:
>>> On 6/08/2010 9:31 a.m., Dave Peterson wrote:
>>>> The problem is in the way the user enters the date in the userform
>>>> textbox and the date order that the user specifies in his/her windows
>>>> date settings. (I'm guessing that the problem occurs when they use the
>>>> short date format.)
>>>>
>>>> If you use cdate() to convert the textbox string to a real date,
>>>> excel/vba will use that user's setting (see help for cdate for more
>>>> info).
>>>>
>>>> This is from xl2003 VBA's help for cDate:
>>>>
>>>> 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.
>>>>
>>>> =====
>>>> But if they type something that isn't a date, you could still have
>>>> trouble.
>>>>
>>>> Maybe something like this will help:
>>>>
>>>> Option Explicit
>>>> Sub testme()
>>>>
>>>> Dim myStr As String
>>>>
>>>> 'a date or a warning message
>>>> Dim myDate As Variant
>>>>
>>>> myStr = "01/02/03"
>>>>
>>>> On Error Resume Next
>>>> myDate = CDate(myStr)
>>>> If Err.Number<> 0 Then
>>>> Err.Clear
>>>> myDate = "Not a date"
>>>> End If
>>>> On Error GoTo 0
>>>>
>>>> With ActiveSheet.Range("A1")
>>>> 'unambiguous format for testing!
>>>> .NumberFormat = "mmmm dd, yyyy"
>>>> .Value = myDate
>>>> End With
>>>>
>>>> End Sub
>>>>
>>>> Instead of using a textbox to get the date, you may want to remove any
>>>> potential problems by using a calendar control.
>>>>
>>>> Ron de Bruin has some notes:
>>>> http://www.rondebruin.nl/calendar.htm
>>>>
>>>> Or even 3 different controls (year, month, day) to get the date.
>>>>
>>>> On 08/05/2010 09:58, Steve Jones wrote:
>>>>> I am having a problem transferring a date from a userform to a
>>>>> worksheet in
>>>>> Excel 2003
>>>>>
>>>>> This is an extract of the code I am using to enter data from the
>>>>> userform to
>>>>> the worksheet.
>>>>>
>>>>> Dim irow As Long
>>>>> Dim ws As Worksheet
>>>>> Set ws = Worksheets("Wagedata")
>>>>>
>>>>> ' finds first empty row in database
>>>>>
>>>>> irow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
>>>>>
>>>>> ws.Cells(irow, 1).Value = Me.Cmbempno.Value
>>>>> ws.Cells(irow, 2).Value = Me.Txtempname.Value
>>>>> ws.Cells(irow, 3).Value = Me.TxtFrmcd.Value
>>>>> ws.Cells(irow, 4).Value = Me.TxtDate.Value ' date not being entered in
>>>>> the
>>>>> format required
>>>>>
>>>>>
>>>>> All works as it should except I want the date displayed in DD/MM/YY
>>>>> format
>>>>> on the worksheet rather than MM/DD/YY.
>>>>>
>>>>>
>>>>> Thanks
>>>>>
>>>>>
>>>>>
>>>
>>> Rons calendar is good, or
>>> What about Range.value = Format("dd/mm/yy") in your code or smethiing
>>> similar?
>>> Lyn

>>

>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
opening user form when worksheet activated OR embedding a userform ina worksheet jason Microsoft Excel Discussion 1 31st Aug 2009 05:53 PM
new worksheet formatting is DATE Lawrence Microsoft Excel Misc 1 9th Apr 2009 12:35 PM
Date field in user form & Loading a user form on opening workbook Balan Microsoft Excel Programming 1 24th May 2008 03:40 PM
Worksheet event to change formatting when date expires. DDawson Microsoft Excel Programming 5 17th Jan 2008 12:08 PM
Date formatting issue in user form =?Utf-8?B?SmVubmlmZXI=?= Microsoft Excel Programming 3 16th Apr 2005 09:49 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:18 PM.