Calculate total time between Dates in Hours and Minutes

Posted by matthew parkes on Stack Overflow See other posts from Stack Overflow or by matthew parkes
Published on 2012-11-13T16:00:28Z Indexed on 2012/11/13 17:01 UTC
Read the original article Hit count: 201

Filed under:
|
|

Hi

I’m trying to resolve a problem using VB and I need some assistance. I’m very new to the language (1 week). The problem is I have created a user form to show how many hours and minutes has elapsed between two different times similar to a time sheet.

The user form consists of two calendars, and under each calendar there are two text boxes; one box each to record the Hour and Minute they left and two further boxes to record the time they arrived back.

I have used the code to minus the calendars together (e.g calendar in – calendar out) then times this by 24 to indicate the hours away. Then under the calendar out I have a text box for the user to type in the hour they left. Then I minus the 24 by the Hour out e.g. if it was 24 -15 it will appear 9 ( 9 hours of that day ) then I would add that to the figure they inserted in the text box Hour in (Return Time). e.g 14. Then I would add them to together e.g. 9 + 14 = 23 and have this displayed in another text box Total Hours. Therefore it would display 23 meaning 23 hours.

I have then want to show another two text boxes to indicate minutes. One for Minutes Out then Minutes In.

I have the problem to convert these minutes for instance if it is the out time is 15:50 and the in time the next day is at 15:55 it displays as 24 (in one text box) and 105 minutes (in the other text box). I would like the minutes added to the hour and have the balance of the remaining minutes in the minute text box. This should display 24 (in one text box) and 5 (in another text box).

The ultimate aim is to get a result that shows a person was absent for a number of days, hours and minutes, eg, 2 days, 5 hours and 10 minutes. Any ideas on how I can modify my code to achieve this?

Here’s my code.

Please Help

Dim number1 As Date
Dim number2 As Date
Dim number3 As Integer
Dim number4 As Integer
Dim Number5 As Integer
Dim Number6 As Integer
Dim answer  As Integer
Dim answer2 As Integer
Dim answer3 As Integer
Dim answer4 As Integer
Dim answer5 As String

number1 = DTPicker1
number2 = DTPicker2
number3 = Txthourout
number4 = TxtHourin
Number5 = TxtMinuteout
Number6 = TxtMinuetIn


answer = number2 - number1
answer2 = answer * 24
answer3 = answer2 - number3
answer4 = answer3 + number4

answer5 = Number5 + Number6


TextBox1.Text = answer4
TextBox2.Text = answer5

End Sub

© Stack Overflow or respective owner

Related posts about excel

Related posts about excel-vba