+ Reply to Thread
Results 1 to 10 of 10

Please help me with a simple VBA function

Hybrid View

  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):
    Function GA(edc As String, dd As String, today As String) As String
    'Gestational Age (GA)
    
    
    If IsMissing(today) Then
        today = Format(Now, "yyyymmdd")
    End If
    If IsMissing(edc) Then
        GA = "n/a"
    Else
        Select Case DateSerial(Left(dd, 4), Mid(dd, 5, 2), Right(dd, 2))
            Case Is = ""
                GA_Days_Total = 280 + Int(DateSerial(Left(edc, 4), Mid(edc, 5, 2), Right(edc, 2)) _
                    - DateSerial(Left(today, 4), Mid(today, 5, 2), Right(today, 2)))
            Case Else
                GA_Days_Total = 280 + Int(DateSerial(Left(edc, 4), Mid(edc, 5, 2), Right(edc, 2)) _
                    - DateSerial(Left(dd, 4), Mid(dd, 5, 2), Right(dd, 2)))
        End Select
    End If
    
    GA_Weeks = Fix(GA_Days_Total / 7)
    GA_days = GA_Days_Total Mod 7
    
    GA = Format(GA_Weeks, "0") & " " & Format(GA_days, "0") & "/7 weeks"
    
    End Function
    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,375
    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!

+ 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