(VBA) Converting a regular IF Statement to a VBA "FORMULA".

Ryan Saari

New Member
Joined
Aug 4, 2008
Messages
6
I need help converting this if statement I created to a "FORMULA" in vba.

This formula is used to subtract unpaid breaks from total hours worked in a day.

For Example:

J5 = 5:00:00am
H5 = 8:30:00am

Formula will conver J5 to pm, then subtrack J5 from H5 = 8.5 hours and then based on the hours worked of 8.5 subtracts 1 hour for an unpaid lunch.

=IF(J5=0, 0, IF((((J5+0.5)-H5)*24)<=4,(((J5+0.5)-H5)*24)-0,IF((((J5+0.5)-H5)*24)<6.5,(((J5+0.5)-H5)*24)-0.5,IF((((J5+0.5)-H5)*24)>=6.5,(((J5+0.5)-H5)*24)-1,IF((((J5+0.5)-H5)*24)>8.5,"OVR","")))))

My VBA code using FORMULA(START,END) results in nothing but errors.

Help!
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
I haven't actually tried to decipher your If() Formula, but here's a sample function. (it could be more concise, but at least it's easy to follow the logic, I hope). You will need to adjust the "Select Case" part of the code to actually work out your real lunch deductions.

Code:
Public Function HRSWRKD(dtmStart, dtmEnd) As Variant

'-------------------Test for Invalid Time Entry
If Not (IsNumeric(dtmStart) And IsNumeric(dtmEnd)) Then
    HRSWRKD = "ERROR" 'Not times
    Exit Function 'Exit
End If

If dtmStart > 1 Then
    HRSWRKD = "ERROR" 'over 23.99 hours
    Exit Function 'Exit
End If

If dtmEnd > 1 Then
    HRSWRKD = "ERROR" 'over 23.99 hours
    Exit Function 'Exit
End If


'-------------------Gross Hours
HRSWRKD = (dtmEnd - dtmStart) * 24


'-------------------Subtract Lunch
Select Case HRSWRKD
    Case Is <= 4
        HRSWRKD = HRSWRKD - 0.25
    Case Is <= 6.5
        HRSWRKD = HRSWRKD - 0.5
    Case Is <= 8
        HRSWRKD = HRSWRKD - 1
    Case Else
        HRSWRKD = "OVR"
End Select


End Function
'-------------------
Sub test()
Dim x
x = HRSWRKD([a1].Value, [b1].Value)
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,120
Members
451,399
Latest member
alchavar

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