Results 1 to 3 of 3

help with dates format and vba code

Threaded View

  1. #1
    Forum Contributor
    Join Date
    03-12-2018
    Location
    London, England
    MS-Off Ver
    2016
    Posts
    118

    help with dates format and vba code

    Hello everyone. I need some help!
    An online platform I use exports .csv files where a column is a date. For some random reason, but I guess because I am located in London and the files are generated in US (so with mm-dd format) some of the dates are recognized as such, whilst others are considered as text. You can see what I mean at row 242 of the file I am attaching.


    As I need all dates to be in the number format (44227 for example), I created the following code which splits the column A into day month and year, recombine and get the number in column G.


    Sub Macro1()
    
    
     Dim OOF As Long
        OOF = Range("A1").End(xlDown).Row
    
        Columns("B:G").Select
        Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
        Selection.NumberFormat = "General"
        Range("B2").Select
        ActiveCell.FormulaR1C1 = "=TEXT(RC[-1],""DD/MM/yyyy"")"
        Range("C2").Select
        ActiveCell.FormulaR1C1 = "=NUMBERVALUE(LEFT(RC[-1],2))"
        Range("D2").Select
        ActiveCell.FormulaR1C1 = "=NUMBERVALUE(MID(RC[-2],4,2))"
        Range("E2").Select
        ActiveCell.FormulaR1C1 = "=NUMBERVALUE(RIGHT(RC[-3],2))"
        Range("F2").Select
        ActiveCell.FormulaR1C1 = _
            "=CONCATENATE(RC[-2],""/"",RC[-3],""/"",""20"",RC[-1])"
        Range("G2").Select
        ActiveCell.FormulaR1C1 = "=RC[-1]+0"
    
    
    Range("B2:g2").Select
        Selection.AutoFill Destination:=Range("b2:g" & OOF), Type:=xlFillDefault
    
    End Sub

    It works just fine.

    The problem I have is that it only works if I add the module directly in the csv file and run the macro there, whilst what I want is to start the macro from another file, pick the csv file and let it run the macro. If I do so, the format of the dates remain incorrect, ending up with #value error in the column G.

    This is the code I created:

    Sub example()
    Dim myFile As String
    myFile = Application.GetOpenFilename
    If myFile = "False" Then Exit Sub
    
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    
    
    Workbooks.Open filename:=myFile
    
    Dim OOF As Long
        OOF = Range("A1").End(xlDown).Row
    
        Columns("B:G").Select
        Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
        Selection.NumberFormat = "General"
        Range("B2").Select
        ActiveCell.FormulaR1C1 = "=TEXT(RC[-1],""DD/MM/yyyy"")"
        Range("C2").Select
        ActiveCell.FormulaR1C1 = "=NUMBERVALUE(LEFT(RC[-1],2))"
        Range("D2").Select
        ActiveCell.FormulaR1C1 = "=NUMBERVALUE(MID(RC[-2],4,2))"
        Range("E2").Select
        ActiveCell.FormulaR1C1 = "=NUMBERVALUE(RIGHT(RC[-3],2))"
        Range("F2").Select
        ActiveCell.FormulaR1C1 = _
            "=CONCATENATE(RC[-2],""/"",RC[-3],""/"",""20"",RC[-1])"
        Range("G2").Select
        ActiveCell.FormulaR1C1 = "=RC[-1]+0"
    
    
    Range("B2:g2").Select
        Selection.AutoFill Destination:=Range("b2:g" & OOF), Type:=xlFillDefault
    
    
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    
    
    
    End Sub

    I don't know if there is something I can do in the vba itself or it's a setting in the file with the macro perhaps?

    Can anyone help me?

    Thank you!
    Attached Files Attached Files

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Convert Dates from Apr 11, 2018 format to numeric format MM/DD/YYYY
    By worswick25 in forum Excel General
    Replies: 16
    Last Post: 04-17-2018, 06:38 AM
  2. Replies: 3
    Last Post: 02-27-2017, 04:38 PM
  3. [SOLVED] VBA code pull data from SQL db - need output dates in proper date format
    By seatejo in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-15-2014, 02:28 AM
  4. Convert Dates in 2011:08:17 format into Dates in Excel
    By JessRI in forum Excel - New Users/Basics
    Replies: 12
    Last Post: 09-11-2012, 09:55 AM
  5. Replies: 1
    Last Post: 04-26-2010, 08:14 AM
  6. Conditional Format Dates in a Calender when Matches dates in a list
    By Lungfish in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 05-14-2009, 06:23 AM
  7. [SOLVED] [SOLVED] the dates on cell format make different dates.
    By date formats morph the dates/chang case in forum Excel - New Users/Basics
    Replies: 7
    Last Post: 04-17-2005, 10:07 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