+ Reply to Thread
Results 1 to 3 of 3

help with dates format and vba code

Hybrid 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

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: help with dates format and vba code

    You could do the conversion manually by following these steps:

    1 Select the column with the dates.

    2 Goto Data>Text to columns...

    3 Choose Delimited, click Next, click Next.

    4 On the 3rd step choose MDY from the Date dropdown in the Column data format section.

    P.S. You could do something similar to this if you imported the data from CSV file into Excel rather than opening the file directly.
    If posting code please use code tags, see here.

  3. #3
    Forum Expert
    Join Date
    10-06-2017
    Location
    drevni ruchadlo
    MS-Off Ver
    old
    Posts
    2,157

    Re: help with dates format and vba code

    Quote Originally Posted by thoandros View Post
    ... something I can do in the vba itself ...
    You took on this matter from the most tedious and time-consuming side - use Nori's advice.
    In Excel, you have ready-made text import mechanisms, which can additionally be turned into a macro, e.g.:
    Option Explicit
    
    Sub strange_kind_of_date() ' :)
        With Application.FileDialog(msoFileDialogFilePicker)
            .AllowMultiSelect = False
            .Filters.Clear: .Filters.Add "CSV Files", "*.csv"
            .InitialFileName = ThisWorkbook.Path & "\"
            If .Show <> -1 Then Exit Sub
            Dim strbufpth: strbufpth = .SelectedItems(1)
        End With
        
        Dim nwbk As Object
        Set nwbk = Workbooks.Add
        
        With nwbk.ActiveSheet
            With .QueryTables.Add("TEXT;" & strbufpth, .Range("A1"))
                .FieldNames = True
                .AdjustColumnWidth = True
                .TextFilePlatform = 65001
                .TextFileStartRow = 1
                .TextFileParseType = xlDelimited
                .TextFileColumnDataTypes = Array(3)
                .Refresh BackgroundQuery:=False
                .Delete
            End With
            With .Range("A1")
                .EntireColumn.ColumnWidth = .EntireColumn.ColumnWidth + 5
            End With
        End With
        
        Set nwbk = Nothing
    End Sub

+ 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. [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] 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