How to change macro output from 12 hour to 24 hour format?

Copper Contributor

Im coding using VBA in excel... Everytime I use a Date variable to store a time value, the output is always in 12 hour clock format. I have used functions to change this but it doesnt work. How do I convert the msgbox output to 24 hour format?

5 Replies

@Shrey24

 

Hi,

 

Instead of Date variable, use Variant alone with the Format function as follows:

Sub Test()
Dim MyDate As Variant
MyDate = Format(Time, "hh:mm")
MsgBox MyDate
End Sub

 

Or without any variables like this:

MsgBox Format(Time, "hh:mm")

 

If you insist to use Date variable, try this:

Sub Test2()
Dim MyDate As Date
MyDate = Time
MsgBox WorksheetFunction.Text(MyDate, "[hh]:mm:ss")
End Sub

 

Hope that helps

@Haytham Amairah 
Thanks a lot! I have been struggling for quite a long time and I think I have finally gotten it after using the variable as a Variant. Thanks again.

@Haytham Amairah I copied you "Sub Test()" program and run on my Excel 2016, but it still show 12 Hour format.