Hi there,
I have dates in column B (DATE) which are in text format and the length of each cell (using the LEN(Text) function) can vary from 5, 21 and 22 as shown below.
DATE | Column C | Character Length |
2/16/2010 12:00:00 AM | 21 | |
12/20/1991 12:00:00 AM | 22 | |
10/23/2019 12:00:00 AM | 22 | |
6/26/1985 12:00:00 AM | 21 | |
06/04/1987 0:00 | 5 |
In cell C2 (Column C), I usually use the formula below and copy down to the last record.
=IF(LEN(B2)=5,"=TEXT((LEFT((DATE(MID((TEXT("&""""&B2&""""&","&"""DD/MM/YYYY"""&")),7,4),LEFT((TEXT("&""""&B2&""""&","&"""DD/MM/YYYY"""&")),2),MID((TEXT("&""""&B2&""""&","&"""DD/MM/YYYY"""&")),4,2))),10)),"&"""DD/MM/YYYY"""&")", IF(LEN(B2)=21,"=MID((LEFT("&""""&B2&""""&",FIND("&""" """&","&""""&B2&""""&",1)-1)),3,3)"&"&"&"""0"""&"&"&"LEFT((LEFT("&""""&B2&""""&",FIND("&""" """&","&""""&B2&""""&",1)-1)),1)"&"&"&"RIGHT((LEFT("&""""&B2&""""&",FIND("&""" """&","&""""&B2&""""&",1)-1)),5)", IF(LEN(B2)=22,"=MID((LEFT("&""""&B2&""""&",FIND("&""" """&","&""""&B2&""""&",1)-1)),4,3)"&"&"&"LEFT("&""""&B2&""""&",2)"&"&"&"RIGHT((LEFT("&""""&B2&""""&",FIND("&""" """&","&""""&B2&""""&",1)-1)),5)")))
I then use Excel's Text to Columns wizard to convert the dates to DD/MM/YYYY.
I'm trying to create a macro to do the above but when I record the macro to just insert the formula in C2, it gives me a "Compile Error, Syntax Error". Below is code that is generated after recording.
Range("C2").Select
ActiveCell.FormulaR1C1 = _
"=IF(LEN(RC[-1])=5,""=TEXT((LEFT((DATE(MID((TEXT(""&""""""""&RC[-1]&""""""""&"",""&""""""DD/MM/YYYY""""""&"")),7,4),LEFT((TEXT(""&""""""""&RC[-1]&""""""""&"",""&""""""DD/MM/YYYY""""""&"")),2),MID((TEXT(""&""""""""&RC[-1]&""""""""&"",""&""""""DD/MM/YYYY""""""&"")),4,2))),10)),""&""""""DD/MM/YYYY""""""&"")"", IF(LEN(RC[-1])=21,""=MID((LEFT(""&""""""""&RC[-1]&""""""""&""& _
""&"""""" """"""&"",""&""""""""&RC[-1]&""""""""&"",1)-1)),3,3)""&""&""&""""""0""""""&""&""&""LEFT((LEFT(""&""""""""&RC[-1]&""""""""&"",FIND(""&"""""" """"""&"",""&""""""""&RC[-1]&""""""""&"",1)-1)),1)""&""&""&""RIGHT((LEFT(""&""""""""&RC[-1]&""""""""&"",FIND(""&"""""" """"""&"",""&""""""""&RC[-1]&""""""""&"",1)-1)),5)"", IF(LEN(RC[-1])=22,""=MID((LEFT(""&""""""""&RC["& _
""""&"",FIND(""&"""""" """"""&"",""&""""""""&RC[-1]&""""""""&"",1)-1)),4,3)""&""&""&""LEFT(""&""""""""&RC[-1]&""""""""&"",2)""&""&""&""RIGHT((LEFT(""&""""""""&RC[-1]&""""""""&"",FIND(""&"""""" """"""&"",""&""""""""&RC[-1]&""""""""&"",1)-1)),5)"")))"
Any idea where the issue is? as i can't seem to locate it. I tried inserting line breaks "& _" but getting errors (i.e. "Compile error. Expected end of statement").
Appreciate your help.