Dates not formating correctly on import

Orange Womble

New Member
Joined
Oct 13, 2009
Messages
11
Hi All

(and thanks for any previous help you may have inadvertently given me!! :cool:)
[I usually manage to get through by reading others' posts and/or breaking things into smaller chunks however this time i am stuck]....

I'm pulling in about 110k lines (will get bigger in time) from a ".txt" file and wanting the dates to all come in correctly....
(I also found and read http://www.mrexcel.com/forum/showthread.php?t=307804 - but can't figure out what I should steal from there if anything - i read the MSKB but that surely won't work for me as I'll be distributing the macro to others once written).
I am in the UK currently using UK settings throughout (so my dates are DD/MM/YY or DD/MM/YYYY). I don't want to use the reg edit as the fix as i will be giving the macro to others (continental Europe).

Also i found http://www.mrexcel.com/forum/<wbr>showthread.php?t=411801 so currently I'm using Chips' script un-adapted to pull in the lines one by ione until i understand it better[though I will tailor it later after i fix this] - the relevant parts [i think] are:

Dim InputLine As String ' The entire line of text read from the input file
Dim Arr As Variant ' Used with Split to break InputLine into an array, delimited by SplitChart
....
....
Line Input #FNum, InputLine
....
....
Arr = Split(expression:=InputLine, delimiter:=SplitChar, limit:=-1, compare:=vbTextCompare)
For Colndx = LBound(Arr) To UBound(Arr)
....
and it continues on

All my resulting issues are shown in this extract:
<table style="width: 604pt; border-collapse: collapse;" x:str="" border="0" cellpadding="0" cellspacing="0" width="805"><colgroup><col style="width: 97pt;" width="129"><col style="width: 30pt;" width="40"><col style="width: 71pt;" width="95"><col style="width: 72pt;" width="96"><col style="width: 77pt;" width="102"><col style="width: 84pt;" width="112"><col style="width: 62pt;" width="83"><col style="width: 63pt;" width="84"><col style="width: 48pt;" width="64"></colgroup><tbody><tr style="height: 12.75pt;" height="17"><td style="border: medium none rgb(236, 233, 216); background-color: transparent; width: 97pt; height: 12.75pt;" width="129" height="17">Installation</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent; width: 30pt;" width="40">
</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent; width: 71pt;" x:str="Install. Item " width="95">Install. Item </td><td style="border: medium none rgb(236, 233, 216); background-color: transparent; width: 72pt;" width="96">Inst.Type</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent; width: 77pt;" width="102">Inst. Date</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent; width: 84pt;" width="112">Ref #</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent; width: 62pt;" width="83">StartDt</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent; width: 63pt;" width="84">EndDt</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent; width: 48pt;" width="64">Cmp</td></tr><tr style="height: 12.75pt;" height="17"><td style="border: medium none rgb(236, 233, 216); background-color: transparent; height: 12.75pt;" height="17">R</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">
</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">AAAA</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">HAA121</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;" class="xl24" x:num="39820" align="right">07/01/2009</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;" x:str=" ">
</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;" x:str=" ">
</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;" x:str=" ">
</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;" x:num="" align="right">150</td></tr><tr style="height: 12.75pt;" height="17"><td style="border: medium none rgb(236, 233, 216); background-color: transparent; height: 12.75pt;" x:num="" align="right" height="17">80</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">
</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">BBBB</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">HDE999</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">29-05-2009</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;" x:str=" ">
</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;" x:str=" ">
</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;" x:str=" ">
</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;" x:num="" align="right">150</td></tr><tr style="height: 12.75pt;" height="17"><td style="border: medium none rgb(236, 233, 216); background-color: transparent; height: 12.75pt;" height="17">N1</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">
</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">CCCC</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">HAA050</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">21-07-2009</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;" x:str=" ">
</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;" x:str=" ">
</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;" x:str=" ">
</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;" x:num="" align="right">150</td></tr><tr style="height: 12.75pt;" height="17"><td style="border: medium none rgb(236, 233, 216); background-color: transparent; height: 12.75pt;" height="17">Q1</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">
</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">DDDD</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">HKC002</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">27-02-2009</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;" x:str=" ">
</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;" x:str=" ">
</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;" x:str=" ">
</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;" x:num="" align="right">171</td></tr><tr style="height: 12.75pt;" height="17"><td style="border: medium none rgb(236, 233, 216); background-color: transparent; height: 12.75pt;" height="17">R1D</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">
</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">EEEE</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">HDE007</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;" class="xl24" x:num="39971" align="right">07/06/2009</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;" x:str=" ">
</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;" x:str=" ">
</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;" x:str=" ">
</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;" x:num="" align="right">150</td></tr><tr style="height: 12.75pt;" height="17"><td style="border: medium none rgb(236, 233, 216); background-color: transparent; height: 12.75pt;" height="17">R1E</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">
</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">FFFF</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">HMP004</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">20-08-2009</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;" x:str=" ">
</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;" x:str=" ">
</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;" x:str=" ">
</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;" x:num="" align="right">130</td></tr><tr style="height: 12.75pt;" height="17"><td style="border: medium none rgb(236, 233, 216); background-color: transparent; height: 12.75pt;" x:num="" align="right" height="17">804</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">
</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">GGGG</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">ZAA999</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">26-02-2009</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">910441/002</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;" class="xl24" x:num="39815" align="right">02/01/2009</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">31-01-2010</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;" x:num="" align="right">160</td></tr><tr style="height: 12.75pt;" height="17"><td style="border: medium none rgb(236, 233, 216); background-color: transparent; height: 12.75pt;" height="17">4LCN</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">
</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">HHHH</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">HKC002</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">25-03-2009</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;" x:str=" ">
</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;" x:str=" ">
</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;" x:str=" ">
</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;" x:num="" align="right">150</td></tr><tr style="height: 12.75pt;" height="17"><td style="border: medium none rgb(236, 233, 216); background-color: transparent; height: 12.75pt;" x:num="" align="right" height="17">704</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">
</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">IIII</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;" x:num="9999999999" align="right">9999999999</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">19-01-2009</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">740359/001</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;" class="xl24" x:num="39814" align="right">01/01/2009</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">31-12-2009</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;" x:num="" align="right">140</td></tr><tr style="height: 12.75pt;" height="17"><td style="border: medium none rgb(236, 233, 216); background-color: transparent; height: 12.75pt;" x:num="" align="right" height="17">1048</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">
</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">JJJJ</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">ATERM</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">25-05-2009</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;" x:str=" ">
</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;" x:str=" ">
</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;" x:str=" ">
</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;" x:num="" align="right">140</td></tr><tr style="height: 12.75pt;" height="17"><td style="border: medium none rgb(236, 233, 216); background-color: transparent; height: 12.75pt;" x:num="" align="right" height="17">5719</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">
</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">KKKK</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">ZAA999</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">27-04-2009</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">920020/001</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;" class="xl24" x:num="39457" align="right">10/01/2008</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">30-09-2010</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;" x:num="" align="right">160</td></tr><tr style="height: 12.75pt;" height="17"><td style="border: medium none rgb(236, 233, 216); background-color: transparent; height: 12.75pt;" x:num="" align="right" height="17">5773</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">
</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">LLLL</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">ZAA999</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;" class="xl25">27-04-2009</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">920020/001</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;" class="xl24" x:num="39457" align="right">10/01/2008</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">30-09-2010</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;" x:num="" align="right">160</td></tr><tr style="height: 12.75pt;" height="17"><td style="border: medium none rgb(236, 233, 216); background-color: transparent; height: 12.75pt;" x:num="" align="right" height="17">7720</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">
</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">MMMM</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">ZAA999</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">27-04-2009</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">920020/001</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;" class="xl24" x:num="39457" align="right">10/01/2008</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">30-09-2010</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;" x:num="" align="right">160</td></tr><tr style="height: 12.75pt;" height="17"><td style="border: medium none rgb(236, 233, 216); background-color: transparent; height: 12.75pt;" x:num="" align="right" height="17">10095</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">
</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">NNNN</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">HGE007</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">14-05-2009</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;" x:str=" ">
</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;" x:str=" ">
</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;" x:str=" ">
</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;" x:num="" align="right">180</td></tr><tr style="height: 12.75pt;" height="17"><td style="border: medium none rgb(236, 233, 216); background-color: transparent; height: 12.75pt;" x:num="" align="right" height="17">155749</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">
</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">OOOO</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">HCA017</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">25-03-2009</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">C00609/002</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">26-03-2010</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">25-03-2012</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;" x:num="" align="right">150</td></tr><tr style="height: 12.75pt;" height="17"><td style="border: medium none rgb(236, 233, 216); background-color: transparent; height: 12.75pt;" x:num="" align="right" height="17">4834873</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">
</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">PPPP</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">HDE004</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;" class="xl24" x:num="39814" align="right">01/01/2009</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">612718/001</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;" class="xl24" x:num="39448" align="right">01/01/2008</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;" class="xl24" x:num="39448" align="right">01/01/2008</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;" x:num="" align="right">120</td></tr><tr style="height: 12.75pt;" height="17"><td style="border: medium none rgb(236, 233, 216); background-color: transparent; height: 12.75pt;" x:num="8532200800276" align="right" height="17">8.5322E+12</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">
</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">QQQQ</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;">HDB064</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;" class="xl24" x:num="40149" align="right">02/12/2009</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;" x:str=" ">
</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;" x:str=" ">
</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;" x:str=" ">
</td><td style="border: medium none rgb(236, 233, 216); background-color: transparent;" x:num="" align="right">160</td></tr></tbody></table>

1) The Dates in "Inst. Date", "StartDt", "EndDt" do not always convert to actual dates. Though if I enter into (simply with F2) any of these cells and then leave it then excel will actually apply the correct formatting with no instruction from me whatsoever. :confused:

2) The data in the Installation column (not always numbers) sometimes doesn't treat the value as text no matter how much space I give it. (as in the last row above). :eek:

3) Lower importance - some of the entries have spaces at the front or end of the piece of data - i would like to remove them all (preferably as the data comes in but could be run on each sheet after all the imports I guess [then I'll have to learn about counting worksheets or some such too! :() - I'm sure I canfind something to help on this if no-one has "pet script" they like.

Many thanks for any assistance anyone can offer!
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
For the dates:

Code:
Sub Macro1()
'
' Macro1 Macro
' Macro recorded 20/10/2009 by apoulsom
'
    Workbooks.OpenText Filename:="P:\TEMP\MrExcel\TextFiles\Datesetc.txt", _
        Origin:=xlWindows, StartRow:=1, DataType:=xlDelimited, TextQualifier:= _
        xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, _
        Comma:=False, Space:=False, Other:=False, FieldInfo:=Array(Array(1, 1), _
        Array(2, 1), Array(3, 1), Array(4, 4), Array(5, 1), Array(6, 4), Array(7, 4), Array(8, 1))
End Sub

Change the file name to suit.
 
Upvote 0
Andrew,
Thanks for the thought - the reason i can't use the usual code Workbooks.OpenText FileName:=............... is that the file is too large so i need to split onto another sheet as needed.
So i'm reading in the data line by line using the input method that i found in the other thread mentioned above....... unless I'm missing something then I'm unsure how to insert the suggested code into the other macro to deal with the format errors.
(Incidentally i have had this issue in the past, just not in exactly the same circumstances so i could deal with it personally [manually] or by doing multiply by 1 if it was numeric data that wasn't working quite right....)

I just tried the x1 and reformat - it doesn't reformat to a date :(
 
Upvote 0
You should be able to use Text To Columns after the import to convert the dates to serial dates. Otherwise you can use code like this:

Code:
    Dim c As Range
    For Each c In Range("D2:D" & Range("D65536").End(xlUp).Row)
        c.Value = CLng(DateValue(c.Text))
        c.NumberFormat = "dd/mm/yyyy"
    Next c
 
Upvote 0
You should be able to use Text To Columns after the import to convert the dates to serial dates. Otherwise you can use code like this:

Being more familiar with the "basic" Text To Columns then i tried that first - It works! and i feel like i should have though of that :rolleyes: (I have to add to reformat with full the YYYY for consiteny).- So a "thank-you" is in order here!

Wishing to further my knowledge, and for efficiency, i decided to try the code too... it would "break" on the 3rd line starting "c.value..." I had a look arround to see if i should be changing elements of that code and didn't think i should (presuming i am trying to apply to only column D).
If it's easy to explain then i'm interested :p - if not then no matter i have an answer to #1 now!

I was about to ask if anyone had any ideas on questions #2 and #3, but thought i might try the same logic on #2 - so.... one "Text To Columns" including format as text on column A and (y)!!! so that's very cool indeed!!

As for #3, the annoying spaces in column A, i can macro a find " " replace with "" and that will get rid of them.

I think "I'm sorted" (maybe not the most efficient - but i think it'll all work).
:biggrin:
 
Upvote 0
The code will fail if the cell doesn't contain a date that is acceptable to the DateValue function or is blank. You can trap that:

Code:
Sub Test()
    Dim c As Range
    For Each c In Range("D2:D" & Range("D65536").End(xlUp).Row)
        On Error Resume Next
        c.Value = CLng(DateValue(c.Text))
        If Err = 0 Then
            c.NumberFormat = "dd/mm/yyyy"
        Else
            Err.Clear
        End If
        On Error GoTo 0
    Next c
End Sub
 
Upvote 0
Thanks again - I think I'm persuaded to take a basic VBA course so that i understand first principles.
I tried both methods and timed them - the text to columns approach takes only about half the time which some-what reassures me that not all the code i've ever written is horendously inefficient just because i started from a point i knew and could understand :)
Thanks for your help.
 
Upvote 0

Forum statistics

Threads
1,215,398
Messages
6,124,699
Members
449,180
Latest member
craigus51286

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