=IF(ISNUMBER(NUMBERVALUE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(TRIM(CLEAN(A1))," ",""),"-",""),"/",""),".",""),"\",""))),SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(TRIM(CLEAN(A1))," ",""),"-",""),"/",""),".",""),"\",""),"Date has Text")
=IF(LEN(B1)=8,DATE(RIGHT(B1,4),MID(B1,3,2),LEFT(B1,2)),IF(LEN(B1)=7,DATE(RIGHT(B1,4),MID(B1,2,2),LEFT(B1,1)),"Not Date"))
=IF(LEN(B8)=8,DATE(RIGHT(B8,4),LEFT(B8,2),MID(B8,3,2)),IF(LEN(B8)=7,DATE(RIGHT(B8,4),LEFT(B8,1),MID(B8,2,2)),"Not Date"))
Function AdarshDateFromText(dateString As String, Optional Indian As Boolean = True) As String '''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Created by : Adarsh Madecha ' Created on : 15 December 2015 ' Version : 1.0 ' Copyright : All rights reserved. ' Website : madrecha.com ' Blog : adarshmadrecha.blogspot.com ' Twitter : @adarshMadrecha ' If you want to share this code, ask that person to visit - adarshmadrecha.blogspot.com '-----------Purpose of this fucntion------------------ ' Background: Many a times when we (CA/ CS/ ICWA) recieve data from client in excel or after conveting data to excel, Date columns are very difficult to deal with ' for this reason, I have created this fucntion, which will help in converting date from Text format to Actual date ' Using : Fuction accepts 2 parameters ' : 1st Fucntion - Text where data resides ' : 2nd Function - Whether Date in text is in Indian format or not ' : if Date is in format of DD MM YYYY (i.e. indian format), you need not specify any parameter. ' : if Date is in format of MM DD YYYY (i.e. American format), you need to specify 2nd parameter as FALSE. '''''''''''''''''''''''''''''''''''''''''''''''''''''' Dim tempText As String 'For Removing all caracters Dim tempNumber As Double 'For checking we have only text ' Clean and Trim tempText = Application.WorksheetFunction.Clean(dateString) tempText = Application.WorksheetFunction.Trim(tempText) ' Delete . \ / - and Space tempText = Replace(tempText, " ", "") tempText = Replace(tempText, "/", "") tempText = Replace(tempText, "\", "") tempText = Replace(tempText, ".", "") tempText = Replace(tempText, "-", "") 'tempNumber = Application.WorksheetFunction.NumberValue(tempText) Dim isTempNum As Boolean 'Variable to store result isTempNum = IsNumeric(tempText) 'If the result contains any text, then return and exit If isTempNum = False Then AdarshDateFromText = "Date has Text" Exit Function End If 'Continnue if all the result is Number Dim tempDate, tempMonth, tempYear As String ' Get Date If Indian Then If Len(tempText) = 8 Then tempDate = Left(tempText, 2) ElseIf Len(tempText) = 7 Then tempDate = Left(tempText, 1) Else AdarshDateFromText = "Date is not proper" Exit Function End If Else If Len(tempText) = 8 Then tempDate = Mid(tempText, 3, 2) ElseIf Len(tempText) = 7 Then tempDate = Mid(tempText, 2, 1) Else AdarshDateFromText = "Date is not proper" Exit Function End If End If 'Get Month If Indian Then If Len(tempText) = 8 Then tempMonth = Mid(tempText, 3, 2) ElseIf Len(tempText) = 7 Then tempMonth = Mid(tempText, 2, 1) Else AdarshDateFromText = "Date is not proper" Exit Function End If Else If Len(tempText) = 8 Then tempMonth = Left(tempText, 2) ElseIf Len(tempText) = 7 Then tempMonth = Left(tempText, 1) Else AdarshDateFromText = "Date is not proper" Exit Function End If End If 'Get year tempYear = Right(tempText, 4) AdarshDateFromText = DateSerial(tempYear, tempMonth, tempDate) End FunctionIf you don't know how to use this code / custom function, just download the below file.
Labels: Excel, Excel Tips