+ Reply to Thread
Results 1 to 7 of 7

Thread: Macro and date formating

  1. #1
    Registered User
    Join Date
    09-23-2011
    Location
    Montreal, Canada
    MS-Off Ver
    Excel 2003
    Posts
    4

    Macro and date formating

    HI,

    I have a very stressful problem:

    In my document, I have dates in the format dd.mm.yyyy and I need to change them to yyyy-mm-dd

    problem is excel dosen't recognize the dot. I wrote the following macro to try to do the change:

        Cells.Replace What:=".", Replacement:="/", LookAt:=xlPart, SearchOrder _
            :=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
            
        Dim h As Integer
        For h = 1 To nbr_of_rows
            Cells(h, 11).Select
            ActiveCell.FormulaR1C1 = "=RC[-7]+RC[1]"
            Cells(h, 4).Select
            ActiveCell.FormulaR1C1 = Cells(h, 11).Value
        Next h
        
            Columns("D:D").Select
            Selection.NumberFormat = "yyyy-mm-dd"
    The cumbersome addition and re-pluggin of value in the same cell is because otherwise excel would read the content of the cells and wouldn't apply the formating to it.
    that works for the dates with days above 13. Otherwise it recognizes the day as a month...

    __this______ becomes______that______
    13.09.2011 ____________ 2011-09-13
    13.09.2011 ____________ 2011-09-13
    12.09.2011 ____________ 2011-12-09
    12.09.2011 ____________ 2011-12-09

    What's wrong ?
    Last edited by Domo_Kun; 09-26-2011 at 08:12 AM. Reason: SOLVED

  2. #2
    Forum Guru Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    5,590

    Re: Macro and date formating

    You could try using Find & Replace

    Select your range of dates then
    Find what:= "." (dot without the quotes)
    Replace with:= "/" (without the quotes)
    Replace All

    or this formula, where your dates are in A2 down.
    =SUBSTITUTE(A2,".","/")*1
    Drag/Fill Down

    Then format the cells as you wish
    If you need any more information, please feel free to ask.

    However, if this takes care of your needs, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED. It helps everybody! ....
    Also
    If you are satisfied by any members response to your problem please consider using the small Star icon botom left of thier post to show your appreciation.

  3. #3
    Registered User
    Join Date
    09-23-2011
    Location
    Montreal, Canada
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Macro and date formating

    That's what the Cells.Replace was for...

    I guess I'll give a try to your Substitue thing. I have a feeling the problem is more in how excell reads date formats though.

  4. #4
    Registered User
    Join Date
    09-23-2011
    Location
    Montreal, Canada
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Macro and date formating

    The substitute function worked !!
    Thank you for eluding that dark mystery!

  5. #5
    Forum Guru Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    5,590

    Re: Macro and date formating

    Dates can be fickle in VBa
    Try this, assuming your dates are in A2 down.
    Option Explicit
    
    Sub ConvertToDate()
        Dim LastRow As Long
        Dim Cell As Range
        
        LastRow = Range("A" & Rows.Count).End(xlUp).Row
        For Each Cell In Range("A2:A" & LastRow)
            With Cell
            .Replace What:=".", Replacement:="/"
            .Value = CDate(.Value)
            .NumberFormat = "yyyy-mm-dd"
            End With
        Next
    End Sub
    If you need any more information, please feel free to ask.

    However, if this takes care of your needs, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED. It helps everybody! ....
    Also
    If you are satisfied by any members response to your problem please consider using the small Star icon botom left of thier post to show your appreciation.

  6. #6
    Registered User
    Join Date
    09-23-2011
    Location
    Montreal, Canada
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Macro and date formating

    It might have had with the regional or language settings. I live in Montreal and my employer installed a french version of excel but an english version of VBA (go figure). I think that date formating standards may differ in those two versions...

  7. #7
    Forum Guru Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    5,590

    Re: Macro and date formating

    I can't answer that last query. I'll see if someone else can help or explain.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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.2.0