+ Reply to Thread
Results 1 to 5 of 5

Quick date format problem...

Hybrid View

  1. #1
    Registered User
    Join Date
    10-11-2012
    Location
    Copenhagen
    MS-Off Ver
    Excel 2003
    Posts
    32

    Quick date format problem...

    Hi All.
    I have a problem. Actually I have found the solution to it but I don't understand why my original code wasn't working...
    Here it is. It changes my inputdate "dd/mm/yyyy" to "mm/dd/yyyy"... When I use CDate then it works...
    The code is to be used by different users. How do I avoid that the different language settings on each computer maintaine the date format "dd/mm/yyyy"?
    Sub input_date()
    Dim UserEntry_end As String
    Dim dd As Date
    Dim Msg_end As String
    Msg_end = "Enter end date as dd/mm/yyyy"
      UserEntry_end = ""
            Do Until IsDate(UserEntry_end) And (Len(Trim(UserEntry_end)) = 10) And (Len(Trim(UserEntry_end)) >= 8)
            UserEntry_end = InputBox(Msg_end)
            If IsDate(UserEntry_end) And (Len(Trim(UserEntry_end)) <= 10) And (Len(Trim(UserEntry_end)) >= 8) Then
                ActiveSheet.Range("C4").Value = Format(UserEntry_end, "dd/mm/yyyy")
            '    ActiveSheet.Range("C4").Value = CDate(UserEntry_end)
            Exit Do
            ElseIf UserEntry_end = "" Then
            Exit Do
            Else
                Msg_start = "Please try again.  Enter date as dd/mm/yyyy"
            End If
            Loop
    End Sub
    Best regards

  2. #2
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Quick date format problem...

    You have to realise that a displayed date does not mean it is actually understood by Excel to be a date, as it could also be a text string.

    When it is a text string you will notice that the displayed value does not change when you change the formatting of the cell (for example from dd/mm/yyyy to mm/dd/yyyy). In order for Excel to treat the value as a date, the Cdate() instruction actually converts the date into a date serial number, which can be displayed in any date format desired.

    You can check this by changing the cell format to text for a date. Excel will in that case display a number.

    Hope this explains it a bit.
    If you like my contribution click the star icon!

  3. #3
    Registered User
    Join Date
    10-11-2012
    Location
    Copenhagen
    MS-Off Ver
    Excel 2003
    Posts
    32

    Re: Quick date format problem...

    So if I use Cdate as above how do I then ensure that the format is "dd/mm/yyyy"?

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646
    CDate has nothing to do with formatting

    It will convert a valid string to a date value.

    The formatting is kind of up to you.
    If posting code please use code tags, see here.

  5. #5
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Quick date format problem...

    Cdate generates a proper date value so the cell format becomes leading. In other words set your cell format to display the date in the desired format

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1