Getting date from string

wpryan

Well-known Member
Joined
May 26, 2009
Messages
534
Office Version
  1. 365
Platform
  1. Windows
Hi All,
I have a data import that includes the date as part of a string in one cell (example: 20221229 Rotator Test Protocol FLM2314, where 2022 is ""yyyy", 12 is "mm" and 29 is "dd"). I want to extract the date from the string and format it as expected for my Swiss company (yyyy.mm.dd). It must be done in VBA, as this string is part of a data importation, and should follow the rest of the script. I tried various approaches, including:
VBA Code:
Range("A" & lrow) = Format(Left(shNew.Range("H1"), 8), "yyyy.mm.dd") 'sets date
This is being interpreted as 1906.05.02, but the expected date is 2022.12.18.

The next approach was to try a formula, where I tried to extract the date using the Concatenate, Left and Mid functions (which, by the way, is working in the Formula bar - with the direct cell references of course):
VBA Code:
Range("A" & lrow).Formula = "=CONCATENATE(LEFT(shNew.Range("H1");4);".";MID(shNew.Range("H1");5;2);".";MID(shNew.Range("H1");7;2))"
...but in this case VBA doesn't like the "." in the equation - I get a "expected - end of statement" error.
can anyone think of another way?
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
That cannot be the sheet name as it is too long.
 
Upvote 0
Not sure of the information but the following calculates the date from cell A1 or the file's name.
It is not sensitive to Regional Settings.
Format to your preference,

20221229 Rotator Test Protocol FLM2314.xlsm
ABC
1D:\Excel16\[20221229 Rotator Test Protocol FLM2314.xlsm]1a29-Dec-2022refers to Cell A1
2
329-Dec-2022refers to file's name
4
1a
Cell Formulas
RangeFormula
A1A1=CELL("filename",A1)
B1B1=DATE(MID(A1,FIND("[",A1)+1,4),MID(A1,FIND("[",A1)+5,2),MID(A1,FIND("[",A1)+7,2))
B3B3=LET(s,LET(n,CELL("filename",A1),MID(n,FIND("[",n)+1,8)),DATE(LEFT(s,4),MID(s,5,2),MID(s,7,2)))
 
Upvote 0
OK here's the solution... The mistake I made was when using the .formula approach, one must wrap the period (.) around double quotes:
VBA Code:
.Range("A" & lRow).Formula = "=LEFT(Temp!B1,4)&"".""&MID(Temp!B1,5,2)&"".""&MID(Temp!B1,7,2)" 'sets date
 
Upvote 0
Solution

Forum statistics

Threads
1,223,237
Messages
6,170,924
Members
452,366
Latest member
TePunaBloke

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