GetADate from user.


Posted by Stuart Welsh on September 11, 2001 2:58 AM

When using the following VB code I'm getting the American date format returned. e.g. when the user keys in 12/02/2001 the VB code returns it 02/12/2001. I need the user to key in a date when promted and keep it in UK date format.
Code:
Sub GetADate()


Dim TheString As String
Dim RowNdx As Integer
Dim TheDate As Double
Dim cellValue As Variant

TheString = Application.InputBox("Enter your key date using the format dd/mm/yy to calculate service")
cellValue = TheString
If IsDate(TheString) Then
TheDate = DateValue(TheString)
Sheets("Sheet3").Select
ActiveSheet.Range("a1").Value = cellValue
Else
Sheets("Sheet1").Select
MsgBox "Invalid date. Try again using the format dd/mm/yy"
GetADate
End If

End Sub



Posted by Dax on September 11, 2001 5:53 AM

Dim TheDate as Date rather than double and also dim CellValue as date. And only assign the date to CellValue once you're routine has confirmed that the value entered is a date. By dimensioning the variables as date you're letting VBA know that you want the dates in the same format that your system is using (i.e. control panel settings). Here's the revised code:-
Sub GetADate()


Dim TheString As String
Dim RowNdx As Integer
Dim TheDate As Date
Dim cellValue As Date

TheString = Application.InputBox("Enter your key date using the format dd/mm/yy to calculate service")
If IsDate(TheString) Then
cellValue = CDate(TheString)
TheDate = DateValue(TheString)
Sheets("Sheet3").Select
ActiveSheet.Range("a1").Value = cellValue
Else
Sheets("Sheet1").Select
MsgBox "Invalid date. Try again using the format dd/mm/yy"
GetADate
End If

End Sub


Regards,
Dax.