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

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
No need for VBA:
Book1
AB
1ProductDate
220221229 Rotator Test Protocol FLM231444924
320221229 Rotator Test Protocol FLM231412/29/2022
Sheet3
Cell Formulas
RangeFormula
B2:B3B2=DATEVALUE(MID(A2,5,2)&"/"&MID(A2,7,2)&"/"&LEFT(A2,4))

Row 3 is a copy of row 2 with cell B3 formatted as a date. Wanted to include that so you could see what you might get if the cell isn't formatted as a date.
 
Upvote 0
Hello, thanks for your response. In fact, the destination cell will be the next unpopulated row in the worksheet, so I do need VBA to find the last row. Also, somehow when I copy / paste your formula into the worksheet it comes up with a #Value error. ...not sure why that is.... I'm using Excel 365...
1673291951690.png
 
Upvote 0
Hello, thanks for your response. In fact, the destination cell will be the next unpopulated row in the worksheet, so I do need VBA to find the last row. Also, somehow when I copy / paste your formula into the worksheet it comes up with a #Value error. ...not sure why that is.... I'm using Excel 365...
View attachment 82366
Of course you can reference another worksheet in a formula. Using 365 is irrelevant. I literally cut this from the table I posted and pasted it to a different worksheet.
Book1
A
1Date
244924
312/29/2022
Sheet5
Cell Formulas
RangeFormula
A2:A3A2=DATEVALUE(MID(Sheet4!A2,5,2)&"/"&MID(Sheet4!A2,7,2)&"/"&LEFT(Sheet4!A2,4))

Turning to VBA as the first solution is a big mistake!
 
Upvote 0
That will still give the OP a #value! error. ;)
 
Upvote 0
Of course you can reference another worksheet in a formula. Using 365 is irrelevant. I literally cut this from the table I posted and pasted it to a different worksheet.
Book1
A
1Date
244924
312/29/2022
Sheet5
Cell Formulas
RangeFormula
A2:A3A2=DATEVALUE(MID(Sheet4!A2,5,2)&"/"&MID(Sheet4!A2,7,2)&"/"&LEFT(Sheet4!A2,4))

Turning to VBA as the first solution is a big mistake!
Yes I understand that turning to VBA for the first solution is not always a good idea, however, in my case the destination cell will always be different, hence the "Range("A" & lRow) line of code. And yes, the person will still get a #Value error. That's what I'm trying to find a solution to... I'm curious if it has something to do with the fact that my computer is set to Swiss German formatting...
 
Upvote 0
What is shNew in your code?
 
Upvote 0
What is shNew in your code?
shNew is a worksheet. I have a lot of other data going to that worksheet, and all the rest of the data is being correctly transferred. Just the formatting of this particular cell is giving me grief.
 
Upvote 0
That will still give the OP a #value! error. ;)
I hate to challenge you (it usually doesn't end well for me!), but that's directly from Excel. No #Value! I'm not getting that error. There was no mention of it being from another workbook (been there, ate that foot!). That said, I am having a hard time finding the bottom of the column. I'd be surprised if you couldn't work this one out!
Data:
Cell Formulas
RangeFormula
A4:A9A4="2021"&TEXT(RANDBETWEEN(1,12),"0#")&TEXT(RANDBETWEEN(1,28),"0#")&" Rotator Test Protocol FLM2314"

Still no #Value error:
shitstorm.xlsx
A
1Date
244299
304/13/2021
Sheet5
Cell Formulas
RangeFormula
A2A2=DATEVALUE(MID('Sheet 4'!A9,5,2)&"/"&MID('Sheet 4'!A9,7,2)&"/"&LEFT('Sheet 4'!A9,4))
A3A3=DATEVALUE(MID('Sheet 4'!A9,5,2)&"/"&MID('Sheet 4'!A9,7,2)&"/"&LEFT('Sheet 4'!A9,4))

I'm SURE there's an easy way to determine the last row of Column A! It just escapes me at the moment, and I have a lot of other distractions.
 
Upvote 0

Forum statistics

Threads
1,215,453
Messages
6,124,930
Members
449,195
Latest member
Stevenciu

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