+ Reply to Thread
Results 1 to 6 of 6

AgeFunc No Longer Works

  1. #1
    Registered User
    Join Date
    01-05-2005
    Posts
    5

    AgeFunc No Longer Works

    I created a file in Excel 2003 and entered the code for the AgeFunc function, and it worked fine. I've now upgraded to Excel 2007, and the function no longer works (cells contain #NAME?). The code still exists in the file (ALT-F11) reveals the following code:

    Public Function AgeFunc(stdate As Variant, endate As Variant)

    ' Dim our variables.
    Dim stvar As String
    Dim stmon As String
    Dim stday As String
    Dim styr As String
    Dim endvar As String
    Dim endmon As String
    Dim endday As String
    Dim endyr As String
    Dim stmonf As Integer
    Dim stdayf As Integer
    Dim styrf As Integer
    Dim endmonf As Integer
    Dim enddayf As Integer
    Dim endyrf As Integer
    Dim years As Integer

    ' This variable will be used to modify string length.
    Dim fx As Integer
    fx = 0

    ' Calls custom function sfunc which runs the Search worksheet function
    ' and returns the results.
    ' Searches for the first "/" sign in the start date.
    stvar = sfunc("/", stdate)

    ' Parse the month and day from the start date.
    stmon = Left(stdate, sfunc("/", stdate) - 1)
    stday = Mid(stdate, stvar + 1, sfunc("/", stdate, sfunc("/", stdate) + 1) - stvar - 1)

    ' Check the length of the day and month strings and modify the string
    ' length variable.
    If Len(stday) = 1 Then fx = fx + 1
    If Len(stmon) = 2 Then fx = fx + 1

    ' Parse the year, using information from the string length variable.
    styr = Right(stdate, Len(stdate) - (sfunc("/", stdate) + 1) - stvar + fx)

    ' Change the text values we obtained to integers for calculation
    ' purposes.
    stmonf = CInt(stmon)
    stdayf = CInt(stday)
    styrf = CInt(styr)

    ' Check for valid date entries.
    If stmonf < 1 Or stmonf > 12 Or stdayf < 1 Or stdayf > 31 Or styrf < 1 Then
    AgeFunc = "Invalid Date"
    Exit Function
    End If

    ' Reset the string length variable.
    fx = 0

    ' Parse the first "/" sign from the end date.
    endvar = sfunc("/", endate)

    ' Parse the month and day from the end date.
    endmon = Left(endate, sfunc("/", endate) - 1)
    endday = Mid(endate, endvar + 1, sfunc("/", endate, sfunc("/", endate) + 1) - endvar - 1)

    ' Check the length of the day and month strings and modify the string
    ' length variable.
    If Len(endday) = 1 Then fx = fx + 1
    If Len(endmon) = 2 Then fx = fx + 1

    ' Parse the year, using information from the string length variable.
    endyr = Right(endate, Len(endate) - (sfunc("/", endate) + 1) - endvar + fx)

    ' Change the text values we obtained to integers for calculation
    ' purposes.
    endmonf = CInt(endmon)
    enddayf = CInt(endday)
    endyrf = CInt(endyr)

    ' Check for valid date entries.
    If endmonf < 1 Or endmonf > 12 Or enddayf < 1 Or enddayf > 31 Or endyrf < 1 Then
    AgeFunc = "Invalid Date"
    Exit Function
    End If

    ' Determine the initial number of years by subtracting the first and
    ' second year.
    years = endyrf - styrf

    ' Look at the month and day values to make sure a full year has passed.
    If stmonf > endmonf Then
    years = years - 1
    End If

    If stmonf = endmonf And stdayf > enddayf Then
    years = years - 1
    End If

    ' Make sure that we are not returning a negative number and, if not,
    ' return the years.
    If years < 0 Then
    AgeFunc = "Invalid Date"
    Else
    AgeFunc = years
    End If

    End Function


    Public Function sfunc(x As Variant, y As Variant, Optional z As Variant)
    sfunc = Application.WorksheetFunction.Search(x, y, z)
    End Function

  2. #2
    Forum Expert Palmetto's Avatar
    Join Date
    04-04-2007
    Location
    South Eastern, USA
    MS-Off Ver
    XP, 2007, 2010
    Posts
    3,978

    AgeFunc No Longer Works

    Are macros enabled in your 2007 Excel? The function uses VBA code and thus macros must be enabled. Go to the Trust Center to enable them.
    Last edited by shg; 07-25-2009 at 03:38 PM. Reason: OP reposted with code tags

  3. #3
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898

    Re: AgeFunc No Longer Works

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here
    Where there is a will there are many ways.

    If you are happy with the results, please add to the contributor's reputation by clicking the reputation icon (star icon) below left corner

    Please also mark the thread as Solved once it is solved. Check the FAQ's to see how.

  4. #4
    Registered User
    Join Date
    01-05-2005
    Posts
    5

    AgeFunc No Longer Works

    I created a file in Excel 2003 and entered the code for the AgeFunc function, and it worked fine. I've now upgraded to Excel 2007, and the function no longer works (cells contain #NAME?). The code still exists in the file (ALT-F11) reveals the following code:

    Please Login or Register  to view this content.

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: AgeFunc No Longer Works

    Thread merged.
    Entia non sunt multiplicanda sine necessitate

  6. #6
    Registered User
    Join Date
    01-05-2005
    Posts
    5

    Re: AgeFunc No Longer Works

    Thanks. Enabling the Macro's did it.
    Thanks again.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1