Question : Help adding minutes to time value

I am trying to develop a train timetable function in Outlook using VBA. I have an array with the folowing values number of stops, "stop 1", minutes from departure, "stop 2", minutes from departure etc... So an example would be ...

vTimes = Array(9, "Station1", 0, "Station2", 26, "Station3", 56, "Station4", 73, "Station5", 85)

The code I am struggling with is that which takes the dDep (departure time) and then add the values from the array for munites after departure 0, 26, 56, 73, 85 and displays it correctly as hh:mm.

Thanks for your help.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
Sub TestTrains()

    Dim i As Integer
    Dim vTimes As Variant
    Dim dDep As Date
    Dim sStr As String

    vTimes = Array(9, "Paddington", 0, "Reading", 26, "Swindon", 56, "Chippenham", 73, "Bath Spa", 85)
    dDep = Format(InputBox("What time to depart " & vTimes(1), "Departure"), "hh:mm")
    sStr = ""
    For i = 1 To vTimes(0) Step 2
        sStr = sStr & vTimes(i) & " (" & TimeValue(dDep + (vTimes(i + 1))) & ") "
    Next i
    MsgBox (sStr)
End Sub

Answer : Help adding minutes to time value

Hi, Charltp5.

This should do it.  Pass the minutes to ConvertToTime and it'll return the hours and minutes.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
Function ConvertToTime(intMinutes As Integer) As String
    Dim intHours As Integer, intRemainder As Integer
    intHours = intMinutes / 60
    If intHours > 0 Then
        intRemainder = intMinutes - (intHours * 60)
        ConvertToTime = intHours & ":" & StrZero(intRemainder, 2)
    Else
        ConvertToTime = ":" & StrZero(intMinutes, 2)
    End If
End Function

Function ConvertToTime(intMinutes As Integer) As String
    Dim intHours As Integer, intRemainder As Integer
    intHours = intMinutes / 60
    If intHours > 0 Then
        intRemainder = intMinutes - (intHours * 60)
        ConvertToTime = intHours & ":" & StrZero(intRemainder, 2)
    Else
        ConvertToTime = ":" & StrZero(intMinutes, 2)
    End If
End Function
Random Solutions  
 
programming4us programming4us