Consulting

Results 1 to 14 of 14

Thread: Solved: Date format

  1. #1
    VBAX Regular
    Joined
    Sep 2011
    Posts
    15
    Location

    Solved: Date format

    I have following VBA: -

    [VBA]
    Dim DateVal As String
    DateVal = Format(Date, "dd-mm-yyyy")
    [/VBA]

    Regional Settings - Short Date - was already changed to dd-mm-yyyy

    I wanted to ensure that Col D all cells from 2nd row onwards to have same format as aforesaid. How can I do?

  2. #2
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Hi there,

    At least as posed, select all the cells from row 2 and downward in Col D, right-click and choose format cells... Then select the number tab and custom in the category box. If the format you want is not listed, build it there.

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,730
    Location
    Maybe ...

    [VBA]

    Columns("D").NumberFormatLocal = "dd-mm-yyyy"

    [/VBA]

    Paul

  4. #4
    VBAX Regular
    Joined
    Sep 2011
    Posts
    15
    Location
    Quote Originally Posted by GTO
    Hi there,

    At least as posed, select all the cells from row 2 and downward in Col D, right-click and choose format cells... Then select the number tab and custom in the category box. If the format you want is not listed, build it there.
    That's good idea. I can record Marco using your suggestion. But, I don't know why my system was not able to format Date.

  5. #5
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Did you try Paul's suggestion?

    Could you give a couple of examples of what exactly is being entered and how, where Excel does not coerce it?

  6. #6
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,730
    Location
    If you want Excel to use the Regional Settings for date formating,

    [vba]
    .NumberFormatLocal = "m/d/yyyy"
    [/vba]

    is the way to go

    Paul

  7. #7
    VBAX Regular
    Joined
    Sep 2011
    Posts
    15
    Location
    Quote Originally Posted by GTO
    Did you try Paul's suggestion?

    Could you give a couple of examples of what exactly is being entered and how, where Excel does not coerce it?

    Thanks Gentlemen,

    Please refer to attachment.

    Please view that LValue contains a string in format of DD-MM-YYYY

    But, it passes that string to cell in format of MM-DD-YYYY

    I don't know why?
    Attached Files Attached Files

  8. #8
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,730
    Location
    1. It's not clear to me why you're using the LValue string at all

    2. Once Excel has a Date in a cell, there after any numerical value gets treated as a date

    3. LValue = Format (Date, "dd-mm-yyyy") has nothing to do with WRS.

    4. You can 'override' WRS:

    [VBA]
    Option Explicit

    Private Sub Workbook_Open()

    Range("A:A").NumberFormatLocal = "dd-mm-yyyy"
    Range("A1").Value = Now
    Range("A2").Value = Now + 1
    Range("A3").Value = Now + 3
    Range("A4").Value = Now + 5
    Range("A5").Value = Now + 7
    End Sub
    [/VBA]

    5. Or you can use WRS

    [VBA]
    Option Explicit

    Private Sub Workbook_Open()

    Range("A:A").NumberFormatLocal = "m/d/yyyy"
    Range("A1").Value = Now
    Range("A2").Value = Now + 1
    Range("A3").Value = Now + 3
    Range("A4").Value = Now + 5
    Range("A5").Value = Now + 7
    End Sub
    [/VBA]


    Paul

  9. #9
    Hi,

    I hope this will solve your problem : -

    [VBA]
    Private Sub Workbook_Open()
    Dim LValue As String

    LValue = Format(Date, "Short Date")
    Range("A1").NumberFormat = "@"
    Range("A1").Value = LValue

    End Sub

    [/VBA]

    You have to first ensure that cell is of same data type where your data will go.

    Thanks,

  10. #10
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    I am concerned with putting a string into a cell and expecting it to become a number (date). I always hesitate to trust automatic conversion. I can't complain about my result if I don't ask for what I want.

    For example, this causes the date May 10, 2011 to be put in A1 and show as 10-05-2011, because the auto-coversion from text to date does not take the number formatting into account.

    [VBA]With Range("A1")
    .NumberFormat = "dd-mm-yyyy"
    .Value = Format(Date, "dd-mm-yyyy"): Rem Date = October 5, 2011
    MsgBox .Text: Rem 10-05-2011
    End With[/VBA]
    On the other hand, this causes October 5, 2011 to be put in A1 and show 05-10-2011 as desired.
    [VBA]With Range("A1")
    .NumberFormat = "dd-mm-yyyy"
    .Value = Date: Rem October 5, 2011
    MsgBox .Text: Rem 05-10-2011
    End With[/VBA]
    Last edited by mikerickson; 10-08-2011 at 10:25 AM.

  11. #11
    VBAX Regular
    Joined
    Sep 2011
    Posts
    15
    Location
    What does following do: -

    cells(1,1).NumberFormat = "@"

  12. #12
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    If you test that on a new/blank/throwaway wb, you will see that it changes the cell's numberformat to Text.

  13. #13
    VBAX Regular
    Joined
    Sep 2011
    Posts
    15
    Location
    I also wanted to know what this code will do: -

    [vba]
    Cells(1,1).NumberFormat = "[$-409]d/mmm/yy;@"
    [/vba]

    and why this code was used before formatting: -

    [vba]
    [$-409]
    [/vba]

    Is there any special meaning to using following at end of formatting: -

    [VBA]@[/VBA]

  14. #14
    VBAX Regular
    Joined
    Sep 2011
    Posts
    15
    Location
    I got it...

    The [$-409] is a locale code, given in hexadecimal. Prefixing a date with a certain locale code determines what is shown when you use the various date time format codes.

Posting Permissions

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