O código é este:
Option Compare Database
Option Explicit
Function dataFinalTarefa(argDataInicial As Date, argTempo As String) As Variant
'===========================================================
'Função que calcula uma data e hora final a partir de uma data
'e hora inicial somando-se uma quantidade de horas referentes
'a uma tarefa.
'
'Autor: Plinio Mabesi
'Contato: pliniomabesi@gmail.com
'Novembro - 2009
'
'===========================================================
Dim horaInicial As Double, horaFinal As Double
Dim inicioExpediente As Double, fimExpediente As Double
Dim inicioCafe As Double, fimCafe As Double
Dim inicioAlmoco As Double, fimAlmoco As Double
Dim TempoTarefa As Double
Dim totalExpediente As Double
Dim totalCafe As Double
Dim totalAlmoco As Double
Dim restante As Double
Dim numeroDias As Integer
Dim teste As Double
Dim i As Integer
'Configuração dos dados iniciais. Para personalizar
'basta alterar os valores a serem utilizados.
inicioExpediente = converteHoraDouble("07:00")
inicioCafe = converteHoraDouble("09:00")
fimCafe = converteHoraDouble("09:15")
inicioAlmoco = converteHoraDouble("11:30")
fimAlmoco = converteHoraDouble("12:30")
fimExpediente = converteHoraDouble("17:00")
TempoTarefa = converteHoraDouble(argTempo)
totalCafe = fimCafe - inicioCafe
totalAlmoco = fimAlmoco - inicioAlmoco
totalExpediente = fimExpediente - inicioExpediente - totalAlmoco - totalCafe
horaInicial = converteHoraDouble(Format(Hour(argDataInicial), "00") & ":" & Format(Minute(argDataInicial), "00"))
numeroDias = ((horaInicial + TempoTarefa - inicioExpediente) * 10000) \ ((totalExpediente + 0.0001) * 10000)
If horaInicial < inicioExpediente Or horaInicial > fimExpediente Or (horaInicial >= inicioCafe And horaInicial < fimCafe) Or (horaInicial >= inicioAlmoco And horaInicial < fimAlmoco) Then
dataFinalTarefa = "Hora inicial inválida!"
Exit Function
End If
dataFinalTarefa = argDataInicial
For i = 1 To numeroDias
Do
dataFinalTarefa = dataFinalTarefa + 1
Loop Until diaUtil(dataFinalTarefa)
Next i
horaFinal = horaInicial + TempoTarefa
If horaInicial < inicioCafe And horaFinal > inicioCafe Then
horaFinal = horaFinal + totalCafe
End If
If horaInicial < inicioAlmoco And horaFinal > inicioAlmoco Then
horaFinal = horaFinal + totalAlmoco
End If
If horaFinal > fimExpediente Then
horaFinal = horaFinal - fimExpediente
horaFinal = Round(horaFinal, 3) - Round(((horaFinal * 1000) \ (totalExpediente * 1000)) * totalExpediente, 3)
horaFinal = horaFinal + inicioExpediente
End If
If horaFinal > inicioCafe And numeroDias > 0 Then
horaFinal = horaFinal + totalCafe
If horaFinal > inicioAlmoco Then
horaFinal = horaFinal + totalAlmoco
If horaFinal > fimExpediente Then
restante = horaFinal - fimExpediente
horaFinal = inicioExpediente + restante
Do
dataFinalTarefa = dataFinalTarefa + 1
Loop Until diaUtil(dataFinalTarefa)
End If
End If
ElseIf horaFinal = inicioExpediente Then
horaFinal = fimExpediente
End If
dataFinalTarefa = CDate(Day(dataFinalTarefa) & "/" & Month(dataFinalTarefa) & "/" & Year(dataFinalTarefa) & _
" " & Fix(horaFinal) & ":" & Round((horaFinal - Fix(horaFinal)) * 60))
End Function
Function converteHoraDouble(argHora As String) As Double
Dim lngHora As Long, dblMinuto As Double
lngHora = CInt(Left(argHora, 2))
dblMinuto = CDbl(Right(argHora, 2))
dblMinuto = (dblMinuto * 100) / 60
converteHoraDouble = lngHora + dblMinuto / 100
End Function
Function converteHoraTexto(argHora As Double) As String
Dim intHora As Integer, intMinuto As Integer
intHora = Fix(argHora)
intMinuto = (argHora - intHora) * 100
intMinuto = (intMinuto * 60) / 100
converteHoraTexto = Format(intHora, "00") & ":" & Format(intMinuto, "00")
End Function
No Formulario "Ao Abrir":
Private Sub Form_Open(Cancel As Integer)
DoCmd.MoveSize , , 12 * 567, 16 * 567
End Sub