Consulting

Results 1 to 5 of 5

Thread: Date Format

  1. #1
    VBAX Regular
    Joined
    Nov 2007
    Posts
    11
    Location

    Date Format

    Hi Guys,
    I have a report that is exported into excel. In one cell is have a date like "As at 23-06-2008". What i want to know is how to convert this to yyyymmdd format

  2. #2
    in excel: right click on the cell, format cell, selec "number" and then "custom".
    here you can change the format

    I don't know the code in vba but I think the macro recorder can be a good poitner in this case

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    It is

    [vba]

    Activecell.NumberFormat = "yyyymmdd"
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    VBAX Tutor
    Joined
    Nov 2007
    Posts
    228
    Location
    Quote Originally Posted by xld
    It is

    [vba]

    Activecell.NumberFormat = "yyyymmdd"
    [/vba]
    Hi Conor,

    xld kindly provided me with a piece of code recently which deleted unwanted rows of data.

    I have edited that code to replace the "as at " in your cells with "", then format the cells as "yyyymmdd"

    This code does however assume that all of the cells containing this data are in one column (in the code below that is column 4)

    xld, thought this might be another way to use your original code (although you will no doubt know of a better way than this ).

    Sub ChngFormatUsingAutofilter()
         '-----------------------------------------------------------------
        Const TestColumn As Long = 4 ''<==== Change this to whichever column contains your data
        Dim cRows As Long
         
         'first, count the rows to operate on
        cRows = Cells(Rows.Count, TestColumn).End(xlUp).Row
         
         'finally, apply the autofilter for all matching cells
        Columns(TestColumn).AutoFilter Field:=1, Criteria1:="=*as at*", Operator:=xlAnd
         
         'we now have only matching rows visible, so we can
         'format these matching rows
        With Cells(2, TestColumn).Resize(cRows - 1)
            .SpecialCells(xlCellTypeVisible).Replace What:="as at ", Replacement:=""
            .SpecialCells(xlCellTypeVisible).NumberFormat = "yyyymmdd"
        End With
         
        Columns(TestColumn).AutoFilter
    End Sub
    Marshybid

  5. #5
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    If its actually text rather than a date you can use this in another column and copy down to change the dates to the format you want:
    =SUBSTITUTE(A1,"-","")
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

Posting Permissions

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