Use vba to calculate birth

NeewBie

New Member
Joined
Aug 7, 2012
Messages
40
I want to calculate the age true date birth with VBA.
An example how the date birth is wrote: 196711141893 (the 4 last digits need to be splitted with “-“ so we got 19671114-1893). How to do that firstly true VBA?
After that I want it to calculate the age of the persons in range A:A.

Im looking for an VBA script and not an function.

Thank you!
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Code:
Sub a()
LR = Cells(Rows.Count, "A").End(xlUp).Row
For r = 1 To LR
  d = Cells(r, 1).Text
  sd = Mid(d, 7, 2) & "/" & Mid(d, 5, 2) & "/" & Left(d, 4)
  age = Date - CDate(sd)
  Cells(r, 2) = age / 365
Next
End Sub
 
Upvote 0
Hi,

To add the hypen use this...
Code:
Delimited_Result = Left(Source, Len(Source) - 4) & "-" & Right(Source, 4)

to turn the date into an excel date, use this.....
Code:
Result_XLDate = DateSerial(Left(Delimited_Result, 4), Mid(Delimited_Result, 5, 2), Mid(Delimited_Result, 7, 2))

to determine age from this excel date use this.......
Code:
Calculated_age = Format(Now - result_XLDate, "yy/mm/dd")
NB result will be return in years/months/days
 
Upvote 0
Code:
Sub a()
LR = Cells(Rows.Count, "A").End(xlUp).Row
For r = 1 To LR
  d = Cells(r, 1).Text
  sd = Mid(d, 7, 2) & "/" & Mid(d, 5, 2) & "/" & Left(d, 4)
  age = Date - CDate(sd)
  Cells(r, 2) = age / 365
Next
End Sub

Thank you so much! This script works GREAT!
I have a last question. How can I do for round down the result of the age?
 
Upvote 0
patel45;3605sotning said:
Cells(r, 2) = int(age / 365)[/CODE]
2 is column B
3 is column C
4 is column D

Hmm... Now its not working, I need to remove all data from column 1-3 and Place the birthdate in column 1 so the script runs correctly.
 
Upvote 0

Forum statistics

Threads
1,215,378
Messages
6,124,601
Members
449,173
Latest member
chandan4057

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