Henry VBA
Henry VBA

Reputation: 3

User Defined Function for DateDiff in Excel Spreadh sheet

I wanted to set up a user defined function to calculate the hours worked between employee clock in and out with the following code. It worked well the VBA code window, but it did not show up under the Function bar on the spreadsheet. Any clue?

Function HoursWorked()

clockin = [b2]
clockout = [c2]

HoursWorked = Round(DateDiff("n", clockin, clockout) / 60, 2)

[d2] = HoursWorked

MsgBox HoursWorked

End Function

Upvotes: 0

Views: 219

Answers (1)

TylerDurden
TylerDurden

Reputation: 1670

how do you mean? as you can see in the picture both the funtions I have written in VBA [left of screen] appear in the function bar in excel

enter image description here

Maybe you want to try the below edited version where you don't always have to work with cells B2,C2 and D2. I entered =HoursWorkedv2(B2,C2)in cell E4

Public Function HoursWorkedv2(ByVal Time1 As Date, ByVal Time2 As Date)

clockin = Time1
clockout = Time2

HoursWorkedv2 = Round(DateDiff("n", clockin, clockout) / 60, 2)


End Function

Upvotes: 1

Related Questions