+ Reply to Thread
Results 1 to 5 of 5

#NAME? error for UDF

Hybrid View

  1. #1
    Registered User
    Join Date
    08-01-2011
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    10

    #NAME? error for UDF

    I have created a UDF for doing a simple calculation for my colleague. I have tested it and it works fine in my own file (.xlsm). So I copied it and pasted into a module in my colleague's file (also .xlsm) but I keep getting a #NAME? error when calculating it. It is inserted in a module that has a different name from the function itself and everything is copied exactly to his file, but I still get the problem and I don't know what the difference is. We're both running Office 2007. Can anyone tell me what I'm doing wrong?

    This is the UDF:
    Function LaborHours(StartTime As Double, StopTime As Double)
    
    LunchStart = 0.479167 '11:30
    LunchStop = 0.510416 '12:15
    Break1Start = 0.614585 '14:45
    Break1Stop = 0.6249 '15:00
    Break2Start = 0.732639 '17:35
    Break2Stop = 0.743056 '17:50
    
    
        If StartTime > StopTime Then
            LaborHours = "Error"
        ElseIf StartTime < LunchStart Then
            If StopTime < LunchStart Then
                LaborHours = 24 * (StopTime - StartTime)
            ElseIf StopTime > LunchStop And StopTime < Break1Start Then
                LaborHours = 24 * (StopTime - StartTime) - 0.75
            ElseIf StopTime > Break1Stop And StopTime < Break2Start Then
                LaborHours = 24 * (StopTime - StartTime) - 1
            ElseIf StopTime > Break2Stop Then
                LaborHours = 24 * (StopTime - StartTime) - 1.25
            Else
                LaborHours = "Error"
            End If
        ElseIf StartTime > LunchStop And StartTime < Break1Start Then
            If StopTime > LunchStop And StopTime < Break1Start Then
                LaborHours = 24 * (StopTime - StartTime)
            ElseIf StopTime > Break1Stop And StopTime < Break2Start Then
                LaborHours = 24 * (StopTime - StartTime) - 0.25
            ElseIf StopTime > Break2Stop Then
                LaborHours = 24 * (StopTime - StartTime) - 0.5
            Else
                LaborHours = "Error"
            End If
        ElseIf StartTime > Break1Stop And StartTime < Break2Start Then
            If StopTime > Break1Stop And StopTime < Break2Start Then
                LaborHours = 24 * (StopTime - StartTime)
            ElseIf StopTime > Break2Stop Then
                LaborHours = 24 * (StopTime - StartTime) - 0.25
            Else
                LaborHours = "Error"
            End If
        ElseIf StartTime > Break2Stop Then
            If StopTime > Break2Stop Then
                LaborHours = 24 * (StopTime - StartTime)
            Else
                LaborHours = "Error"
            End If
        Else
            LaborHours = "Error"
        End If
            
    LaborHours = Format(LaborHours, "#.000")
            
    End Function
    Last edited by OnlyTheSnake; 09-12-2012 at 04:31 AM.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,730

    Re: #NAME? error for UDF

    Copied into a standard module, it works fine for me.

    I can get a #NAME? error if I create a second identical code module ... but I get Ambiguous Name detected, so that's pretty obvious.

    I get a #NAME? error if the function call in the worksheet is spelt wrong.

    I don't get a #NAME? error if I make it a Private Function or a Private Module.

    But I do get a #NAME error if I put the function in a Sheet class module.

    So, some options to look at ...


    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: #NAME? error for UDF

    also if you have macros disabled ;-)
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  4. #4
    Registered User
    Join Date
    08-01-2011
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: #NAME? error for UDF

    Quote Originally Posted by JosephP View Post
    also if you have macros disabled ;-)
    Damn, it really was that simple...

    Signed the macro, trusted it on his computer and it works fine now. Thanks for the tip (and for making me kick myself).

  5. #5
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,730

    Re: #NAME? error for UDF

    @JP: Ah, never thought of that. Started out in an existing workbook (without code) and just pasted the code in ... obviously, I could then run it. I'll be annoyed if that's the answer.

+ 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