Table of Contents
VBA, or Visual Basic for Applications, allows for the formatting of time in a variety of ways. This can be useful when working with time-based data or when presenting information in a specific time format. The time formatting function in VBA allows for the conversion of time values into a desired format, such as displaying the time in hours, minutes, and seconds or in a 12-hour format with AM/PM indicators.
To format time in VBA, the “Format” function is used, followed by the time value and the desired format code. For example, the code “Format(timeValue, “hh:mm:ss”)” would display the time in hours, minutes, and seconds. Other format codes include “hh:mm AM/PM” for a 12-hour format and “hh:mm:ss.000” for milliseconds.
Using the “Format” function, time values can be easily manipulated and presented in a way that is understandable and relevant to the data being analyzed. This can save time and effort when working with large sets of time-based data.
Format Time in VBA (With Examples)
You can use the Format function in VBA with the following arguments to format times in Excel in a specific manner:
- h: hours without zeros
- hh: hours with zeros
- n: minutes without zeros
- nn: minutes with zeros
- s: seconds without zeros
- ss: seconds with zeros
- AM/PM: Display AM/PM
The following example shows how to format times using VBA in practice.
Example: Format Times Using VBA
Suppose we have the following column of datetimes in Excel:

We can create the following macro to format the times in column A in specific ways and output the formatted times in columns B through E:
Sub FormatTime()
Dim i As Integer
For i = 2 To 8
Range("B" & i) = Format(Range("A" & i), "h")
Range("C" & i) = Format(Range("A" & i), "h:nn")
Range("D" & i) = Format(Range("A" & i), "h:nn:ss")
Range("E" & i) = Format(Range("A" & i), "h:nn:ss AM/PM")
Next i
End SubWhen we run this macro, we receive the following output:

Columns B through E now show each time in column A formatted in a specific way.
Note that you can also use the shortcut formats of Short Time, Medium Time and Long Time:
Sub FormatTime()
Dim i As Integer
For i = 2 To 8
Range("B" & i) = Format(Range("A" & i), "Short Time")
Range("C" & i) = Format(Range("A" & i), "Medium Time")
Range("D" & i) = Format(Range("A" & i), "Long Time")
Next i
End SubWhen we run this macro, we receive the following output:

Columns B through D now show each time in column A formatted in a specific way.
Cite this article
stats writer (2024). How can I format time in VBA?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-format-time-in-vba-and-what-are-some-examples-of-how-to-do-so/
stats writer. "How can I format time in VBA?." PSYCHOLOGICAL SCALES, 21 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-format-time-in-vba-and-what-are-some-examples-of-how-to-do-so/.
stats writer. "How can I format time in VBA?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-format-time-in-vba-and-what-are-some-examples-of-how-to-do-so/.
stats writer (2024) 'How can I format time in VBA?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-format-time-in-vba-and-what-are-some-examples-of-how-to-do-so/.
[1] stats writer, "How can I format time in VBA?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I format time in VBA?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
