converting number to date

daveyc18

Well-known Member
Joined
Feb 11, 2013
Messages
755
Office Version
  1. 365
  2. 2010
ok so the data i pull in has the date in this general form:

20190502 .....I want to covert that easily into a date format so that I can compare the number of days between the pulled in date and another date

is there an efficient way to do this? ideally via VBA.
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Try these steps:

- Select the column with imported dates
- Go to Data --> Text to Columns
- Select Delimited on Step 1 of 3
- Select Tab on Step 2 of 3
- Select Date: YMD on Step 3 of 3
- Finish
 
Upvote 0
I agree with Tetra's method.

If you insist on vba:
Code:
Sub Num2Date()
Dim cel As Range
Dim temp$


For Each cel In Selection
    temp = cel.Value
    cel = DateSerial(CLng(Left(temp, 4)), CLng(Mid(temp, 5, 2)), CLng(Right(temp, 2)))
Next cel
End Sub
 
Upvote 0
Try these steps:

- Select the column with imported dates
- Go to Data --> Text to Columns
- Select Delimited on Step 1 of 3
- Select Tab on Step 2 of 3
- Select Date: YMD on Step 3 of 3
- Finish

thanks, tetra...duh on my part...not thinking straight today
 
Upvote 0
I agree with Tetra's method.

If you insist on vba:
Code:
Sub Num2Date()
Dim cel As Range
Dim temp$


For Each cel In Selection
    temp = cel.Value
    cel = DateSerial(CLng(Left(temp, 4)), CLng(Mid(temp, 5, 2)), CLng(Right(temp, 2)))
Next cel
End Sub
Apparently the OP does not want a macro solution, but I thought you might find this non-looping macro to be of some interest (note that it handles blank cells whereas your code errors out if a blank cell is encountered in the selection)...
Code:
[table="width: 500"]
[tr]
	[td]Sub NumToDate()
  Selection = Evaluate(Replace("IF(@="""","""",0+TEXT(@,""0000-00-00""))", "@", Selection.Address))
  Selection.NumberFormat = "m/d/yyyy"
End Sub[/td]
[/tr]
[/table]
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,252
Members
452,623
Latest member
Techenthusiast

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