+ Reply to Thread
Results 1 to 3 of 3

Macro mess with the date formatting

Hybrid View

  1. #1
    Registered User
    Join Date
    06-24-2012
    Location
    Argentina
    MS-Off Ver
    Excel 2010
    Posts
    15

    Macro mess with the date formatting

    I'm working on my team metrics. I receive a bunch of incidents with the corresponding dates and times in the US format (mm/dd/yyyy HH:MM:SS)
    For some reason, when I move the data from the cvs file to the xls I work, it modify all dates over the 13th.
    I have been working this manually to get mm/dd/yyyy. But when I tried to move the manual steps to a macro it mess the formatting again

    Sub date_formatting4()
    '
    ' date_formatting4 Macro
    '
    
    '
        Range("B2:B106").Select
        Selection.Copy
        ActiveWindow.SmallScroll Down:=-131
        Range("M2").Select
        ActiveSheet.Paste
        Columns("B:C").Select
        Application.CutCopyMode = False
        Selection.Clear
        Columns("M:M").Select
        Selection.TextToColumns Destination:=Range("M1"), DataType:=xlFixedWidth, _
            FieldInfo:=Array(Array(0, 1), Array(10, 1), Array(25, 1)), TrailingMinusNumbers _
            :=True
        Columns("N:N").Select
        Selection.Delete Shift:=xlToLeft
        Columns("M:M").Select
        Selection.NumberFormat = "@"
        Range("O2").Select
        Selection.Formula = "=IF(ISNUMBER(M2),DATE(YEAR(M2),DAY(M2),MONTH(M2)),DATE(RIGHT(M2,4),LEFT(M2,2),MID(M2,4,2)))"
        Selection.AutoFill Destination:=Range("O2:O106"), Type:=xlFillDefault
        Range("O2:O106").Select
        Selection.NumberFormat = "mm/dd/yyyy"
        ActiveWindow.SmallScroll Down:=82
        Selection.Copy
        ActiveWindow.SmallScroll Down:=-102
        Range("B2").Select
        Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
            xlNone, SkipBlanks:=False, Transpose:=False
        Range("B1").Select
        Application.CutCopyMode = False
        ActiveCell.FormulaR1C1 = "Open Time"
        Range("C1").Select
        ActiveCell.FormulaR1C1 = "Monthly"
        Range("O2:O106").Select
        Selection.NumberFormat = "mm/yyyy"
        Selection.Copy
        Range("C2").Select
        Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
            xlNone, SkipBlanks:=False, Transpose:=False
        Columns("L:P").Select
        Application.CutCopyMode = False
        Selection.Delete Shift:=xlToLeft
        Range("L3").Select
    End Sub
    Basically the script do the same I do manually:
    - Copy the dates to an aux column
    - Using Text to columns, remove the time
    - Change the format to text
    - Use the formula to get the dates
    - Clean the format from the destination
    - Change the format to get mm/dd/yyyy
    - Copy the data
    - Remove the aux columns


    I don't know VBA language to script the macro, so I used the auto recording from excel.

    I have attached a test spreadsheet I have using.
    test formats.xlsx

    Any help will be appreciate

  2. #2
    Forum Expert Crooza's Avatar
    Join Date
    10-19-2013
    Location
    Hunter Valley, Australia
    MS-Off Ver
    Excel 2003 /7/10
    Posts
    2,082

    Re: Macro mess with the date formatting

    Not sure if I understand your issue properly but why don't you change this line and get it into dd/mm/yy format

    Range("O2:O106").Select
        Selection.NumberFormat = "mm/dd/yyyy"
    to this

    Range("O2:O106").Select
        Selection.NumberFormat = "dd/mm/yyyy"
    and while you're at it teh macro is trying to use column B in the first line I had to chnage that to A2 to A106 to get it to work
    Happy with my advice? Click on the * reputation button below

  3. #3
    Registered User
    Join Date
    06-24-2012
    Location
    Argentina
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Macro mess with the date formatting

    That won't work, because not all the dates are in dd/mm/yyyy.
    Only the dates after the 13th are in that format.

    I know the macro is using the column B. I attached a test spreadsheet to see the different dates I'm using.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Formula Mess
    By erikapalen in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 08-10-2014, 02:48 PM
  2. [SOLVED] Dates in both date and text format mess up autofilter and sumifs in VBA
    By Maxdehaas in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 04-22-2014, 12:01 PM
  3. Macro - Formatting date from mm/dd/yy to dd/mm/yy
    By Klara86 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-24-2012, 03:53 PM
  4. [SOLVED] Date Formatting in a Macro
    By gregersanderson@gmail.com in forum Excel General
    Replies: 3
    Last Post: 05-18-2006, 09:15 PM
  5. Crl-Ed : another way to mess-up and fix
    By Patricia Shannon in forum Excel General
    Replies: 0
    Last Post: 03-23-2006, 02:45 PM

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