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 ...
Haytham Amairah
Jan 04, 2020Silver Contributor
Hi,
Instead of Date variable, use Variant alone with the https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/format-function-visual-basic-for-applications 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
- zhaoguweiAug 27, 2020Copper Contributor
Haytham Amairah I copied you "Sub Test()" program and run on my Excel 2016, but it still show 12 Hour format.
- Shrey24Jan 05, 2020Copper 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_octopus424Oct 25, 2020Copper Contributor
- funny_octopus424Oct 25, 2020Copper Contributor,J'O nhN