Forum Discussion
Shrey24
Jan 04, 2020Copper Contributor
How to change macro output from 12 hour to 24 hour format?
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
Sort By
- Haytham AmairahSilver Contributor
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 SubOr 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 SubHope that helps
- zhaoguweiCopper Contributor
Haytham Amairah I copied you "Sub Test()" program and run on my Excel 2016, but it still show 12 Hour format.
- Shrey24Copper ContributorHaytham 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.- funny_octopus424Copper Contributor