converting number to date

daveyc18

Active Member
Joined
Feb 11, 2013
Messages
473
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.
 

Some videos you may like

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney

Tetra201

MrExcel MVP
Joined
Oct 14, 2016
Messages
3,639
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
 

CalcSux78

Well-known Member
Joined
Oct 15, 2013
Messages
1,120
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
 

daveyc18

Active Member
Joined
Feb 11, 2013
Messages
473
Office Version
  1. 365
  2. 2010
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
 

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
36,722
Office Version
  1. 2010
Platform
  1. Windows
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]
 

Watch MrExcel Video

Forum statistics

Threads
1,123,504
Messages
5,602,047
Members
414,497
Latest member
guitarmanz

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
Top