问题
I am trying to prepare a list of credit card transactions for pivoting and further analysis in Excel 2010 (for personal needs). Problem is that my banking institution does not follow a standard date format, so sometimes dates will be coming as Jun. 1, 2013
, and sometimes as May 27, 2013
. The first is not recognized as a date by Excel, so its Pivot cannot Group on that (month, quarter etc.). There are no other unrecognized date occurrences in the list.
Is there a way to ensure there is always a valid date in a column?
If I were coding in .NET, one option would be use ParseExact with a format string MMM. d, yyyy
.
Another option is to do an If
, to convert any string to a date in a something like:
If Not TypeOf MyCell Is DateTime Then _
MyCell.Value = CDate(MyCell.Value.Substring(0,3) &
MyCell.Value.Substring(3))
(This is VB.NETish pseudo-syntax, hope it helps explain my intention). It assumes that a non-valid date will always be a string with a dot inside it, to be removed by the converter - which I am okay with.
How would I do this in Excel? Looking for the most simple approach.
回答1:
Assuming your date is in A1
, this formula will convert it to time-serial format:
=DATEVALUE(SUBSTITUTE(A1,".",""))
You can then format it to whatever format you want.
Another option is to use this UDF:
Function MorphDate(strDate As String) As Date
Application.Volatile
If VarType(strDate) <> 8 Then Exit Function
Res = Replace(strDate, ".", "")
MorphDate = Res
End Function
Use like so: =MorphDate(A1)
, and format it to whatever format you want.
Screenshots:



回答2:
A quick fix would be to do a global replace ctl h. Replace the . with nothing
回答3:
DateValue will only work with the right Regional Settings, We get an error if we try to open it in a computer with different settings. This will always work: (text date in G21)
=DATE(RIGHT(G21;4);MATCH(1;1*(LEFT(G21;3)={"Jan";"Feb";"Mar";"Apr";"May";"Jun";"Jul";"Aug";"Sep";"Oct";"Nov";"Dec"});0);MID(G21;FIND(",";G21)-2;2))
Depending on your regional settings you may need to replace field separator ";" by ","
来源:https://stackoverflow.com/questions/21956830/ensure-a-valid-date-inside-the-cell-in-excel