Dates with "." for a delimiter

Weaver

Well-known Member
Joined
Sep 10, 2008
Messages
5,197
I'm importing a text file via VB using a recorded version of the "import external data" routine. The data consists of dates and times but they're stored with "." rather than "/". So I used find/replace to switch the "." for "/" and all was okay, unless there's a possibility of interpreting the date in US format. So 31.05.2011 becomes 31/05/2011 (31st May), but 01.06.2011 (1st June) becomes 06/01/2011 (6th Jan) and so on. Assuming I'd rather not convert each cell one at a time, is there a way round this? The default date formatting for the system is the UK version and I set the numberformat for the column with the dates in to dd/mm/yyyy hh:mm before doing the search replace, to no effect.

Any ideas? I think must be missing something obvious.
 
I don't recall it being any different when I was on 2002 on XP, but I am not able to test that environment any more.
 
Upvote 0

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
If no unusual non-space or weird whitespace characters, any chance at posting some data? Else could you post the file (or a tidbit thereof) w/generic data subbed for sensitive?

I'm only up for a bit, but at home, so in 2000 (creeeeak, groan...)
 
Upvote 0
Well that's strange - there's no way I can get it to work in the way you suggest.
 
Upvote 0
The file's about 100k, here's a small sample with enough data to illustrate the problem- there's no sensitivities.

Code:
31.05.2011 12:30:00 ECM-Log           80980000.000 *
31.05.2011 12:45:00 ECM-Log           80980000.000 *
31.05.2011 13:00:00 ECM-Log           80980000.000 *
31.05.2011 13:15:00 ECM-Log           80980000.000 *
31.05.2011 13:30:00 ECM-Log           80980000.000 *
31.05.2011 13:45:00 ECM-Log           80980000.000 *
31.05.2011 14:00:00 ECM-Log           80980000.000 *
31.05.2011 14:15:00 ECM-Log           80980000.000 *
31.05.2011 14:30:00 ECM-Log           80980000.000 *
31.05.2011 14:45:00 ECM-Log           80980000.000 *
31.05.2011 15:00:00 ECM-Log           80980000.000 *
31.05.2011 15:15:00 ECM-Log           80980000.000 *
31.05.2011 15:30:00 ECM-Log           80980000.000 *
31.05.2011 15:45:00 ECM-Log           80980000.000 *
31.05.2011 16:00:00 ECM-Log           80980000.000 *
31.05.2011 16:15:00 ECM-Log           80980000.000 *
31.05.2011 16:30:00 ECM-Log           80980000.000 *
31.05.2011 16:45:00 ECM-Log           80980000.000 *
31.05.2011 17:00:00 ECM-Log           80980000.000 *
31.05.2011 17:15:00 ECM-Log           80980000.000 *
31.05.2011 17:30:00 ECM-Log           80980000.000 *
31.05.2011 17:45:00 ECM-Log           80990000.000 *
31.05.2011 18:00:00 ECM-Log           80990000.000 *
31.05.2011 18:15:00 ECM-Log           80990000.000 *
31.05.2011 18:30:00 ECM-Log           80990000.000 *
31.05.2011 18:45:00 ECM-Log           80990000.000 *
31.05.2011 19:00:00 ECM-Log           80990000.000 *
31.05.2011 19:15:00 ECM-Log           80990000.000 *
31.05.2011 19:30:00 ECM-Log           80990000.000 *
31.05.2011 19:45:00 ECM-Log           80990000.000 *
31.05.2011 20:00:00 ECM-Log           80990000.000 *
31.05.2011 20:15:00 ECM-Log           80990000.000 *
31.05.2011 20:30:00 ECM-Log           80990000.000 *
31.05.2011 20:45:00 ECM-Log           80990000.000 *
31.05.2011 21:00:00 ECM-Log           80990000.000 *
31.05.2011 21:15:00 ECM-Log           81000000.000 *
31.05.2011 21:30:00 ECM-Log           81000000.000 *
31.05.2011 21:45:00 ECM-Log           81000000.000 *
31.05.2011 22:00:00 ECM-Log           81000000.000 *
31.05.2011 22:15:00 ECM-Log           81000000.000 *
31.05.2011 22:30:00 ECM-Log           81000000.000 *
31.05.2011 22:45:00 ECM-Log           81000000.000 *
31.05.2011 23:00:00 ECM-Log           81000000.000 *
31.05.2011 23:15:00 ECM-Log           81000000.000 *
31.05.2011 23:30:00 ECM-Log           81000000.000 *
31.05.2011 23:45:00 ECM-Log           81000000.000 *
01.06.2011 00:00:00 ECM-Log           81000000.000 *
01.06.2011 00:15:00 ECM-Log           81000000.000 *
01.06.2011 00:30:00 ECM-Log           81000000.000 *
01.06.2011 00:45:00 ECM-Log           81000000.000 *
01.06.2011 01:00:00 ECM-Log           81000000.000 *
01.06.2011 01:15:00 ECM-Log           81000000.000 *
01.06.2011 01:30:00 ECM-Log           81000000.000 *
01.06.2011 01:45:00 ECM-Log           81000000.000 *
01.06.2011 02:00:00 ECM-Log           81000000.000 *
01.06.2011 02:15:00 ECM-Log           81000000.000 *
01.06.2011 02:30:00 ECM-Log           81000000.000 *
01.06.2011 02:45:00 ECM-Log           81000000.000 *
01.06.2011 03:00:00 ECM-Log           81000000.000 *
01.06.2011 03:15:00 ECM-Log           81000000.000 *
01.06.2011 03:30:00 ECM-Log           81000000.000 *
01.06.2011 03:45:00 ECM-Log           81000000.000 *
01.06.2011 04:00:00 ECM-Log           81000000.000 *
01.06.2011 04:15:00 ECM-Log           81000000.000 *
01.06.2011 04:30:00 ECM-Log           81000000.000 *
01.06.2011 04:45:00 ECM-Log           81000000.000 *
01.06.2011 05:00:00 ECM-Log           81000000.000 *
01.06.2011 05:15:00 ECM-Log           81000000.000 *
01.06.2011 05:30:00 ECM-Log           81000000.000 *
01.06.2011 05:45:00 ECM-Log           81000000.000 *
01.06.2011 06:00:00 ECM-Log           81000000.000 *
01.06.2011 06:15:00 ECM-Log           81010000.000 *
01.06.2011 06:30:00 ECM-Log           81010000.000 *
01.06.2011 06:45:00 ECM-Log           81020000.000 *
01.06.2011 07:00:00 ECM-Log           81020000.000 *
01.06.2011 07:15:00 ECM-Log           81020000.000 *
01.06.2011 07:30:00 ECM-Log           81020000.000 *
01.06.2011 07:45:00 ECM-Log           81020000.000 *
01.06.2011 08:00:00 ECM-Log           81020000.000 *
01.06.2011 08:15:00 ECM-Log           81020000.000 *
01.06.2011 08:30:00 ECM-Log           81020000.000 *
01.06.2011 08:45:00 ECM-Log           81020000.000 *
01.06.2011 09:00:00 ECM-Log           81020000.000 *
01.06.2011 09:15:00 ECM-Log           81020000.000 *
01.06.2011 09:30:00 ECM-Log           81020000.000 *
01.06.2011 09:45:00 ECM-Log           81030000.000 *
01.06.2011 10:00:00 ECM-Log           81030000.000 *
01.06.2011 10:15:00 ECM-Log           81030000.000 *
01.06.2011 10:30:00 ECM-Log           81030000.000 *
01.06.2011 10:45:00 ECM-Log           81030000.000 *
01.06.2011 11:00:00 ECM-Log           81030000.000 *
01.06.2011 11:15:00 ECM-Log           81030000.000 *
01.06.2011 11:30:00 ECM-Log           81030000.000 *
01.06.2011 11:45:00 ECM-Log           81030000.000 *
01.06.2011 12:00:00 ECM-Log           81030000.000 *
01.06.2011 12:15:00 ECM-Log           81030000.000 *
01.06.2011 12:30:00 ECM-Log           81030000.000 *
01.06.2011 12:45:00 ECM-Log           81030000.000 *
01.06.2011 13:00:00 ECM-Log           81030000.000 *
01.06.2011 13:15:00 ECM-Log           81030000.000 *

PS I'm using fixed width to include the date/time in one column, removing the ECM-log and the * fields. Column 1 set to DMY format.
 
Last edited:
Upvote 0
Can you import the date and time separately? Just to test that DMY format on the date column works OK for you.
 
Upvote 0
All

I've just discovered that if you import the date and the time separately, it works as per Jon's suggestion. That still means there's an extra stage required to splice them back together again, but at least if anyone else asks the question, I'll be able to give an accurate answer.

Also, if the date and time are separate, the replace method works too.

PS Glenn - I only spotted your post after I'd posted this comment - a good spot nonetheless.
 
Upvote 0
Hi Weaver,

Sounds as if fixed :)

Rich (BB code):
Option Explicit
    
Sub exa()
Dim REX             As Object ' RegExp
Dim rexSM           As Object ' SubMatches
Dim wks             As Worksheet
Dim rngData         As Range
Dim aryVals         As Variant
Dim i               As Long
Dim strConnection   As String
    
    Set wks = ThisWorkbook.Worksheets.Add
    
    '// Change path to suit...  //
    strConnection = "TEXT;C:\Documents and Settings\MARK\Desktop\2011-08-03\2011-08-08\test.txt"
    
    With wks
        With .QueryTables.Add(Connection:=strConnection, Destination:=wks.Range("A1"))
            .Name = "test"
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .TextFilePromptOnRefresh = False
            .TextFilePlatform = xlWindows
            .TextFileStartRow = 1
            .TextFileParseType = xlFixedWidth
            .TextFileTextQualifier = xlTextQualifierDoubleQuote
            .TextFileConsecutiveDelimiter = False
            .TextFileTabDelimiter = True
            .TextFileSemicolonDelimiter = False
            .TextFileCommaDelimiter = False
            .TextFileSpaceDelimiter = False
            .TextFileColumnDataTypes = Array(2, 9, 1, 9)
            .TextFileFixedColumnWidths = Array(19, 19, 12)
            .Refresh BackgroundQuery:=False
            .Delete
        End With
        
        Set rngData = Range(.Cells(1, 1), .Cells(.Rows.Count, 1).End(xlUp)).Resize(, 2)
        aryVals = rngData.Value
    
        Set REX = CreateObject("VBScript.RegExp")
        REX.Global = True
        '31.05.2011 16:30:00
        REX.Pattern = "(\d{2})(\.)(\d{2})(\.)(\d{4})(\ {1,2})(\d{2})(\:)(\d{2})(\:)(\d{2})"
        
        For i = 1 To UBound(aryVals, 1)
            Set rexSM = REX.Execute(aryVals(i, 1))(0).SubMatches
            aryVals(i, 1) = DateSerial(rexSM(4), rexSM(2), rexSM(0)) _
                            + TimeSerial(rexSM(6), rexSM(8), rexSM(10))
        Next
        
        rngData.NumberFormat = "General"
        rngData.Value = aryVals
        rngData.Columns(1).NumberFormat = "dd/mm/yyyy hh:mm:ss"
        rngData.Columns(2).NumberFormat = "#0.000"
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,493
Messages
6,125,119
Members
449,206
Latest member
burgsrus

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top