Results 1 to 5 of 5

Thread: [RESOLVED] Calculate Age from Range and Color Cell (VBA)

  1. #1

    Thread Starter
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    Resolved [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.


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  2. #2
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    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

  3. #3

    Thread Starter
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    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.


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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

  5. #5

    Thread Starter
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    Re: Calculate Age from Range and Color Cell (VBA)

    Thanks for the tip westconn.


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width