简体   繁体   中英

VBA Function in Excel Returning VALUE

I've plenty long excel formula like this

IF(ISNUMBER(E6),IF(E6<standard_hour,0,IF(((E6-standard_hour)*24*60)>90,2,CEILING(((E6-standard_hour)*24*60)/30*0.5,0.5))),VLOOKUP(E6,Refer,2,FALSE))

Because I use this formula a lot in spreadsheet, I decide to make a custom function for it. This is the function

Function morning_check(start_hour)
    Dim sheet As Worksheet
    Set sheet = ActiveWorkbook.Sheets("Setup")

    If WorksheetFunction.IsNumber(start_hour) Then
        If start_hour < sheet.Range("E1").Value Then
            morning_check = 0
        Else
            If ((start_hour - sheet.Range("E1").Value) * 24 * 60 > 90) Then
                morning_check = 2
            Else
                morning_check = Application.WorksheetFunction.Ceiling(((start_hour - sheet.Range("E1")) * 24 * 60) / 30 * 0.5, 0.5)
            End If
        End If
    Else
        morning_check = Application.WorksheetFunction.VLookup(start_hour, sheet.Range("Refer"), 2, False)
    End If

End Function

The input of this function could be string (example : "TS") or time (example : 07:00)

Using String as Input, this function work correctly, but when I using time it just throw #Value!

Your error is coming from the following lines:

Set standard_hour = TimeValue(sheet.Cells("E1"))
Set user_hour = TimeValue(start_hour)

Set in VBA is used for object creation, while you are simply trying to set a variable. This is why you're getting an "Object Required" error.

Just drop the word Set and you should be able to get on with your debugging.

As a matter of investigation (if your copy of Excel behaved as mine did), Excel highlighted the first line in yellow (which was not helpful), but it also automatically selected the text standard_hour (which identified the location on the problem).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM