UDF. If "not a date" set output to "0"

AusSteelMan

Board Regular
Joined
Sep 4, 2009
Messages
208
Hi guys,

Just a small one I can't get right.

Code:
Public Function OtHours(Workday As Date, Hours As Single)
 
Dim DayNumber As Integer
 
DayNumber = Weekday(Workday, vbSunday)
 
If Hours = 0 Then OtHours = 0
If Workday = 0 Then OtHours = 0
 
If DayNumber = 1 Then
    OtHours = Hours * 2
Else
    If Hours > 2 Then
        OtHours = 3 + ((Hours - 2) * 2)
    Else
        OtHours = Hours * 1.5
    End If
End If
End Function

The problem is if the "Workday" cell is left empty, the date is seen as 12:00:00 AM according to the watch, not 0.

What do I need to change in the following line to make it valid
Code:
If Workday = 0 Then OtHours = 0

I tried a few things (incl Nothing, "12:00:00 am" amongst others) but can't get it.

Everything is OK if the hours are 0 and the calcs work properly when both are valid arguments, just doesn't work how I intended if there is no date (or even if it is text: although I am happy if text is entered for the result to be #VALUE!)

Any help appreciated

Thanks
ASM
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Oh wait, I see...

You need to tell it to stop executing the rest of the code in the function...

Try
Rich (BB code):
If Not Isdate(Workday) Then 
    OtHours = 0
    Exit Function
End if
 
Upvote 0
Hi Jonmo1,

2 things.

1. I tried to End Function not Exit Function, so thanks for clearing that up.

2. Still doesn't work as it does recognise a blank cell as a date. I think that is because it identifies it "Workday As Date" in the 1st line.

When I step through the Function, I can see that the test is applied, but it does not fail so proceeds to the the step.

Can you see any other way around it?

Thanks,
ASM
 
Upvote 0
Hmm, ok it's becaue workday is dimmed as date..

You can either

1. Dim worday as Variant
2. Or try If workday <=1 Then

You might also consider changing the name of that variable...
Workday may be confused with other built in functions...

Try wkdy
 
Upvote 0
Here is what I ended up with. Seems to work OK. Will only get a #VALUE! result if text is entered - and I am ok with that.

I just wanted to be able to enter the date and have it figure out what day of the week it was.

Note: I changed from workday, to wrkday but it had no impact.

Code:
Public Function OtHours(Wrkday As Date, Hours As Single)
 
Dim DayNumber As Integer
 
If IsEmpty(Wrkday) Then
    OtHours = 0
    Exit Function
End If
 
DayNumber = Weekday(Wrkday, vbSunday)
 
If Hours <= 0 Then OtHours = 0
 
If DayNumber > 0 Then
    If DayNumber < 8 Then
 
        If DayNumber = 1 Then
            OtHours = Hours * 2
        Else
            If Hours > 2 Then
                OtHours = 3 + ((Hours - 2) * 2)
            Else
                OtHours = Hours * 1.5
            End If
        End If
    Else
        OtHours = 0
    End If
Else
    OtHours = 0
End If
 
End Function

Thanks jonmo for your thoughts and help.

ASM
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,906
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top