UDF convert to date not working

lynnsong986

Board Regular
Joined
May 24, 2014
Messages
146
Hello,

I'm trying to get this to work...

I have a string 20180115 and I need to convert it to Jan 15, 2018 (just to convert it to a date, doesn't matter what format of date).

this is what I have and it didn't work.

Function ConvertToDateRBC(Text As String) As Date
ConvertToDateRBC = Format(Application.WorksheetFunction.Date((Left(Text, 4)), (Mid(Text, 5, 2)), (Right(Text, 2))), "Short Date")
End Function

can someone please help?

thanks so much,
Lynn
 
Last edited:

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
thanks very much Fennek, it works, but I still would like to know why the codes didn't work? Can someone please let me know?
 
Upvote 0
this is what I have and it didn't work.

Function ConvertToDateRBC(Text As String) As Date
ConvertToDateRBC = Format(Application.WorksheetFunction.Date((Left(Text, 4)), (Mid(Text, 5, 2)), (Right(Text, 2))), "Short Date")
End Function

can someone please help?
The WorksheetFunction object does not have member named "Date"', probably because VB has an identical function named DateSerial. So, you could have written it this way and it would have worked...
Code:
[table="width: 500"]
[tr]
	[td]Function ConvertToDateRBC(Text As String) As Date
  ConvertToDateRBC = Format(DateSerial(Left(Text, 4), Mid(Text, 5, 2), Right(Text, 2)), "Short Date")
End Function[/td]
[/tr]
[/table]
Of course, Fennek's approach is much better...
Code:
Function ConvertToDateRBC(Text As String) As Date
  ConvertToDateRBC = Format(Text, "0000-00-00")
End Function
 
Last edited:
Upvote 0
Thank you so much Rick, I realize Fennek’s solution is much better, just trying to figure out where I did wrong.
Lynn
 
Upvote 0

Forum statistics

Threads
1,212,927
Messages
6,110,696
Members
448,293
Latest member
jin kazuya

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