Results 1 to 5 of 5

Thread: CDate
Thanks Thanks: 0 Likes Likes: 0

  1. #1
    Board Regular
    Join Date
    Jul 2014
    Posts
    614
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default CDate

    Greetings,

    I have a I column of values of the format " Aug 30, 2018" and so on. I would like to be able to convert these into the format "MM/DD/YYYY"

    Code:
    Dim A as Long, LastRow as Long
    LastRow = Cells(Rows.Count,"A").End(xlUp).Row
    For K = LastRow to 1 Step -1
    CDate(Cells(K,"A").value) = Cells(K,"A").Value
    Next K
    I am receiving a syntax error on the CDate line. How can I correctly write this code?

  2. #2
    Board Regular
    Join Date
    Dec 2008
    Posts
    6,641
    Post Thanks / Like
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)

    Default Re: CDate

    It should be
    Code:
    Cells(K,"A").value = Cdate(Cells(K,"A").Value)
    Check Dim A as Long as well

  3. #3
    Board Regular Kenneth Hobson's Avatar
    Join Date
    Feb 2007
    Location
    Tecumseh, OK
    Posts
    3,050
    Post Thanks / Like
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)

    Default Re: CDate

    CDATE() converts a string value to a numeric. If the cells are strings, then CDATE() should be right side of equality sign. In either case, the number format will need to be set. A1 is usually a text field.

    Uncomment the lines if they are string values.
    Code:
    Sub Main()
        'Dim A As Long, LastRow As Long
        'LastRow = Cells(Rows.Count, "A").End(xlUp).Row
        'For K = LastRow To 1 Step -1
        '    Cells(K, "A") = CDate(Cells(K, "A"))
        'Next K
        Range("A1", Cells(Rows.Count, "A").End(xlUp)).NumberFormat = "MM/DD/YYYY"
    End Sub

  4. #4
    Board Regular
    Join Date
    Oct 2013
    Posts
    312
    Post Thanks / Like
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Default Re: CDate

    Cells(K,"A").value = CDate(Cells(K,"A").Value)
    and pay attention to the cell format.
    Using: Windows 10 + Microsoft Excel Professional 2016 (English version ! ! !)
    and also an Acer Chromebook with Google spreadsheet (Online).

  5. #5
    Board Regular
    Join Date
    Jul 2014
    Posts
    614
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: CDate

    Thanks, got it running smoothly.

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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