+ Reply to Thread
Results 1 to 10 of 10

Please help me with a simple VBA function

  1. #1
    Registered User
    Join Date
    10-17-2006
    Posts
    5

    Please help me with a simple VBA function

    Hi! I'm trying to write a piece of code to calculate the gestational age of expectant mothers based on their estimated due date. This is a first attempt at making a user defined function in excel and I can't seem to figure out why it won't work. Any help you can give would be much appreciated.

    Thank you!
    Laurie

    Function GA(edc As Date, dd As Date, today As Date) As String
    'Gestational Age (GA)
    If IsMissing(today) Then
    today = Date
    End If
    If IsMissing(edc) Then
    GA = ""
    Else
    Select Case dd
    Case IsMissing
    GA_Days_Total = 280 + Int(edc - today)
    Case Is > today
    GA_Days_Total = 280 + Int(edc - dd)
    Case Is = today
    GA_Days_Total = 280 + Int(edc - dd)
    End Select

    GA_Weeks = Fix(GA_Days_Total / 7)
    GA_days = GA_Days_Total Mod 7

    GA = CStr(GA_Weeks) & " " & CStr(GA_days) & "/7 weeks"
    End If

    End Function
    Last edited by laurieb; 10-17-2006 at 10:12 PM.

  2. #2
    Registered User
    Join Date
    11-28-2004
    Posts
    11
    How about this (note that the inputs are dates entered as yyyymmdd - so October 1, 2006 would be 20061001):
    Please Login or Register  to view this content.
    Does this help?
    Barrie Davidson
    My Excel Web Page

    "You're only given a little spark of madness. You mustn't lose it." - Robin Williams

  3. #3
    Registered User
    Join Date
    10-17-2006
    Posts
    5
    For some reason I can't get it to work. Did it work for you?

    Thanks for your help!
    Laurie

  4. #4
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,432
    In what way is it not working? Error messages? Returning the wrong answer?

  5. #5
    Registered User
    Join Date
    10-17-2006
    Posts
    5
    It gives me a #name error. Not sure why.

    Thanks again for the help everyone.
    Laurie

  6. #6
    Registered User
    Join Date
    10-17-2006
    Posts
    5
    Also, I'd really like to keep the date in date format on my spreadsheet.

    Thanks!

  7. #7
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,432
    Where have you put the code? In order to be available to Excel, UDF code must be put in a regular module, not in the code page associated with a worksheet or this workbook.

  8. #8
    Registered User
    Join Date
    11-28-2004
    Posts
    11
    Quote Originally Posted by laurieb
    Also, I'd really like to keep the date in date format on my spreadsheet.

    Thanks!
    Then how about changing to:
    Please Login or Register  to view this content.
    Regards,

  9. #9
    Registered User
    Join Date
    10-17-2006
    Posts
    5
    IT'S WORKING!!!! THank you so much. I think the problem was that I was leaving today and delivery date blank. I need to somehow fix this so that they have defaults.

    Otherwise, it's great. Thanks so much for the help!!!
    Laurie

  10. #10
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,432
    It looks to me like what you want to do is make all of your arguments optional.

    Function GA(optional edc, optional dd, optional today)

    see VBA help for the function statement

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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