VBA - Calculate Age of entire column

blackgolf

Board Regular
Joined
Mar 1, 2012
Messages
65
Office Version
  1. 2021
Platform
  1. Windows
I need the age in years in Column T from data in Column K.

DOB (Col K) Age (Col T)
12 Jan 1978 ?
23 Mar 1966 ?
5 May 2001 ?

So the VBA macro must look up the DOB for each row of Column K and place the answer in Column T as age in years.
So the result must be:

DOB (Col K) Age (Col T)
12 Jan 1978 45
23 Mar 1966 56
5 May 2001 21

Many thanks
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Have not tested but try

VBA Code:
Sub calAge()
for each cell in range(“K:K”) ‘ change range to actual range
Cell.offset(0,9)=format(now()-cell,”YYYY”)
Next
End sub
 
Upvote 0
@hajiali Hi, I ran your code above but get an error on line 1 - for each .......
Compile Error and Syntax error, pointing at the : left and right of the K of range("K:K")
Thank you
 
Upvote 0
I need the age in years in Column T from data in Column K.

DOB (Col K) Age (Col T)
12 Jan 1978 ?
23 Mar 1966 ?
5 May 2001 ?

So the VBA macro must look up the DOB for each row of Column K and place the answer in Column T as age in years.
So the result must be:

DOB (Col K) Age (Col T)
12 Jan 1978 45
23 Mar 1966 56
5 May 2001 21

Many thanks
Hi Welcome to MrExcel - Is this the same format you're going to use like 12 Jan 1978 or is there any specific format that your sheet has ?
 
Upvote 0
Please do not mark a post as the solution when it doesn't have one, especially when you are still looking for help.
 
Upvote 0
@hajiali Hi, I ran your code above but get an error on line 1 - for each .......
Compile Error and Syntax error, pointing at the : left and right of the K of range("K:K")
Thank you
How about this,

VBA Code:
Sub calAge()
Dim LRow As Long
Dim Inc As Integer
LRow = Cells(Rows.Count, "K").End(xlUp).Row
For Inc = 2 To LRow
Cells(Inc, "T").Value = Format(Date - Cells(Inc, "K"), "YY")
Next
End Sub
 
Upvote 1
Solution
Hi @hajiali
I ran the code but get funny results like a long 16 and more digit number (104828736689815)
Different cell formats does not help
Thanks
 
Upvote 0
Thanks for your feedback, Glad to hear that!
 
Upvote 0

Forum statistics

Threads
1,215,201
Messages
6,123,621
Members
449,109
Latest member
Sebas8956

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