Show Buttons
Share On Facebook
Share On Twitter
Share On Google Plus
Share On Linkdin
Share On Reddit
Contact us
Hide Buttons

VBA-Excel: Date-Time Functions – TimeSerial() and TimeValue()

Time­Se­r­ial()

Description:

The Time­Se­r­ial() func­tion returns the Time type based on the para­me­ters pro­vided (Hours, Min­utes and Seconds).

Format:

DateSerial(Hour,Minutes,Seconds)

Arguments:
    • Hour
      • Manda­tory
      • Type: Numeric or Any Numeric Expression
      • Between 0 and 23

  • Min­utes
    • Manda­tory
    • Type: Numeric or Any Numeric Expression
    • If it’s greater than 60, than respec­tive num­ber will be added to the Hour, Exam­ple say 65, means Hours will be increased by one and Min­utes will be 5
  • Sec­onds
    • Manda­tory
    • Type: Numeric or Any Numeric Expression
    • If it’s greater than 60, than respec­tive num­ber will be added to the Min­utes, Exam­ple say 70, means Min­utes will be increased by one and Sec­onds will be 10

 

Exam­ple:

Function FnTimeSerial()

   Dim varHour

   Dim varMinute

   Dim varSecond

   Dim strResult

   <b>varHour = "11"</b>

 <b>    varMinute = "12"</b>

<b>      varSecond = "44"</b>

     strTime = TimeSerial(varHour, varMinute, varSecond)

   strResult = "Time is-&gt; " &amp; strTime &amp; vbCrLf

   varHour = "18"

      varMinute = "72"

    varSecond = "44"

   strTime =TimeSerial(varHour, varMinute, varSecond)

  strResult = strResult &amp; "Time is (<b>When Minutes is greater than 60</b>)-&gt; " &amp; strTime

  MsgBox strResult

End Func­tion

TimeSerial()

The Time­Se­r­ial() func­tion returns the Time type based on the para­me­ters pro­vided (Hours, Min­utes and Seconds).

______________________________________________________________________________

TimeValue()

Description:

The Dat­e­Value() func­tion con­verts string to a date which con­tains time and returns it.

Format:

TimeValue(strString)

Arguments:
  • strString
    • Manda­tory
    • Type: String
    • String that needs to con­vert into date which con­tains time. A time from 0:00:00 (12:00:00 AM) to 23:59:59 ( 11:59:59 PM).

Exam­ple:

Function FnTimeValue()

   Dim strString

   Dim strString1

   Dim strResult

   strString = "15:40:03"

   strString1 = "09:25"

  strResult = "Time Value of " & strString & " is -> " & TimeValue(strString) & vbCrLf

  strResult = strResult & "Time Value of " & strString1 & " is -> " & TimeValue(strString1)

  MsgBox strResult

End Function
TimeValue()

The Dat­e­Value() func­tion con­verts string to a date which con­tains time and returns it.

 

Happy Macro­ing :)

Sumit Jain

 

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: