|
-
Nov 26th, 2012, 04:20 AM
#1
Thread Starter
PowerPoster
[RESOLVED] Calculate Age from Range and Color Cell (VBA)
Hi,
I have a row (or selection) with birth-dates. I need to calculate the age and color the cell/row/text (whatever) where the person is younger then 18. Any change someone knows how to do this? I want to add the code to my existing (automation) code.
Thanks in advance.
-
Nov 26th, 2012, 07:22 AM
#2
Re: Calculate Age from Range and Color Cell (VBA)
Try something like this:
Code:
Sub bDates()
Dim i As Integer
For i = 1 To 7 'my sample data is in column A, rows 1 to 7
If Format((Now - Range("a" & i).Value) / 365, 0) > 18 Then
Range("a" & i).Interior.Color = vbRed
End If
Next i
End Sub
-
Nov 28th, 2012, 03:13 AM
#3
Thread Starter
PowerPoster
Re: Calculate Age from Range and Color Cell (VBA)
That's a nice start, however there seems to be an issue with the age-calculation. Not everyyear has 365 days. Perhaps I'm missing something.
-
Nov 28th, 2012, 05:28 AM
#4
Re: Calculate Age from Range and Color Cell (VBA)
Not everyyear has 365 days
use datediff function
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Dec 8th, 2012, 04:01 AM
#5
Thread Starter
PowerPoster
Re: Calculate Age from Range and Color Cell (VBA)
Thanks for the tip westconn.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|